機(jī)器學(xué)習(xí)實(shí)戰(zhàn)篇 (k近鄰算法)
k近鄰算法:通過測(cè)量不同特征值之間的距離進(jìn)行分類
優(yōu)點(diǎn):精度高,對(duì)異常值不敏感署海,無數(shù)據(jù)輸入假定吗购。
缺點(diǎn):計(jì)算復(fù)雜度高,空間復(fù)雜度高砸狞。
計(jì)算公式
分類器的代碼實(shí)現(xiàn)
import numpy as np
from collections import Counter
def classify0(inx, dataset, labels, k=1):
##預(yù)處理(此處的輸入labels是帶有具體分類內(nèi)容的list),inx和dataset都numpy對(duì)象
if k <= 0:
k = 1
try:
y = inx.shape[1]
except:
inx.shape=(-1, inx.shape[0])
##計(jì)算歐氏距離
num_test = inx.shape[0]
num_train = dataset.shape[0]
dists = np.zeros((num_test, num_train))
dists = np.multiply(np.dot(inx, dataset.T), -2)
inx_sq = np.sum(np.square(inx), axis=1, keepdims=True)
dataset_sq = np.sum(np.square(dataset), axis=1)
dists = np.add(dists, inx_sq)
dists = np.add(dists, dataset_sq)
dists = np.sqrt(dists)
###獲取標(biāo)簽
result = []
per_line_labels=[]
sort_arg = dists.argsort()[:,:k]
for line in sort_arg:
per_line_labels = [labels[index] for index in line]
result.append(Counter(per_line_labels).most_common(1)[0][0])
return result
實(shí)例1 利用K-近鄰算法改進(jìn)約會(huì)網(wǎng)站的配對(duì)效果
數(shù)據(jù)集下載 http://pan.baidu.com/s/1geMv2mf
1.從文件中讀取數(shù)據(jù)轉(zhuǎn)化為可計(jì)算的numpy對(duì)象
def file1matrix(filename):
###從文件中讀取數(shù)據(jù)并轉(zhuǎn)為可計(jì)算的numpy對(duì)象
dataset = []
labels = []
with open(filename,'r') as f:
for line in f:
line = line.strip().split('\t')
labels.append(line.pop())
dataset.append(line)
dataset = np.array(dataset, dtype=np.float32)
return dataset, labels
2.將數(shù)據(jù)可視化
def convert(labels):
label_names = list(set(labels))
labels = [label_names.index(label) for label in labels]
return label_names,labels
def draw(dataset, labels, label_names):
labels = [ i+1 for i in labels] ###下標(biāo)加1捻勉,繪色
from matplotlib import pyplot as plt
from matplotlib import font_manager
zhfont = font_manager.FontProperties(fname='C:\\Windows\\Fonts\\msyh.ttc')
plt.figure(figsize=(8, 5), dpi=80)
ax = plt.subplot(111)
# ax.scatter(dataset[:,1], dataset[:,2], 15.0*np.array(labels), 15.0*np.array(labels))
# plt.show()
type1_x = []
type1_y = []
type2_x = []
type2_y = []
type3_x = []
type3_y = []
for i in xrange(len(labels)):
if labels[i] == 1:
type1_x.append(dataset[i][0])
type1_y.append(dataset[i][1])
if labels[i] == 2:
type2_x.append(dataset[i][0])
type2_y.append(dataset[i][1])
if labels[i] == 3:
type3_x.append(dataset[i][0])
type3_y.append(dataset[i][1])
ax.scatter(type1_x, type1_y, color = 'red', s = 20)
ax.scatter(type2_x, type2_y, color = 'green', s = 20)
ax.scatter(type3_x, type3_y, color = 'blue', s = 20)
plt.xlabel(u'飛行里程數(shù)', fontproperties=zhfont)
plt.ylabel(u'視頻游戲消耗時(shí)間', fontproperties=zhfont)
ax.legend((label_names[0], label_names[1], label_names[2]), loc=2, prop=zhfont)
plt.show()
3.歸一化特征值 (這里介紹兩種方法)
####由于數(shù)據(jù)中飛行里程數(shù)特征值與其他的特征值差距較大,對(duì)計(jì)算結(jié)果會(huì)產(chǎn)生非常大的影響刀森,所以將特征值轉(zhuǎn)化為0到1區(qū)間內(nèi)的值
def autoNorm0(dataset):
if not isinstance(dataset, np.ndarray):
dataset = np.array(dataset,dtype=np.float32)
###歸一化特征值 newvalue = (oldvalue - min) / (max - min)
minVals = dataset.min(0)
maxVals = dataset.max(0)
ranges = maxVals - minVals
dataset = dataset - minVals
dataset = dataset / ranges
return dataset
def autoNorm1(dataset):
###歸一化特征值 newvalue = (oldvalue - 均值) / 標(biāo)準(zhǔn)差 ----->推薦使用這種方法
if not isinstance(dataset, np.ndarray):
dataset = np.array(dataset,dtype=np.float32)
mean = dataset.mean(0)
std = dataset.std(0)
dataset = dataset - mean
dataset = dataset / std
return dataset
4.編寫測(cè)試代碼
def datingTest():
##隨機(jī)選取測(cè)試集和訓(xùn)練集
filename = 'datingTestSet.txt'
dataset, labels = file1matrix(filename)
dataset = autoNorm1(dataset)
train_length = int(dataset.shape[0] * 0.9)
test_length = dataset.shape[0] - train_length
from random import sample
all_index = sample(range(dataset.shape[0]), dataset.shape[0])
train_index = all_index[:train_length]
test_index = all_index[-test_length:]
train_dataset = dataset[train_index, :]
train_labels = []
test_dataset = dataset[test_index, :]
test_labels = []
for index in train_index:
train_labels.append(labels[index])
for index in test_index:
test_labels.append(labels[index])
##訓(xùn)練并計(jì)算錯(cuò)誤率
test_result = classify0(test_dataset, train_dataset, train_labels, k=3)
error = 0
for res in zip(test_result, test_labels):
if res[0] != res[1]:
error += 1
print 'error accaury:%f' % (float(error) / len(test_labels))
實(shí)例2 識(shí)別手寫數(shù)字
1.讀取文件數(shù)據(jù)并轉(zhuǎn)化為可計(jì)算的numpy對(duì)象
import os
def imgVector(filename):
vect = []
with open(filename,'r') as f:
for line in f:
line = line.strip()
vect += [float(n) for n in line]
number = os.path.split(filename)[-1].split('_')[0]
return np.array(vect, dtype=np.float32), number
def all_imgVector(directory):
filelist = os.listdir(directory)
vects = []
labels = []
for filename in filelist:
vect, label= imgVector(os.path.join(directory, filename))
vects.append(vect)
labels.append(label)
return np.array(vects, dtype=np.float32), labels
2.編寫測(cè)試代碼
def handwritingClassTest():
test_dir = 'digits\\testDigits'
train_dir = 'digits\\trainingDigits'
train_dataset, train_labels = all_imgVector(train_dir)
test_dataset, test_labels = all_imgVector(test_dir)
result_labels = classify0(test_dataset, train_dataset, train_labels, k=3)
error = 0
for res in zip(result_labels, test_labels):
if res[0] != res[1]:
error += 1
print 'error accaury:%f' % (float(error) / len(test_labels))