#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Remove dict.iteritems(), dict.iterkeys(), and dict.itervalues().
#Instead: use dict.items(), dict.keys(), and dict.values() respectively.
# 线代和矩阵操作库
from numpy import *
import operator
#路径相关的
from os import listdir
#画图的
import matplotlib
import matplotlib.pyplot as plt
#测试数据 书上代码都只有2,3个特征
def createDataSet():
group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]]) #array 遍于矩阵操作
labels = ['A', 'A', 'B', 'B']
return group, labels
# 分类器
def classify0(inx, dataSet, labels, k):
dataSetSize = dataSet.shape[0] #获得一维长度
diffMat = tile(inx, (dataSetSize,1)) - dataSet #获得二维长度
sqDiffMat = diffMat**2 #平方
sqDistances = sqDiffMat.sum(axis=1) # axis 1 代表行 0 代表 列
distances = sqDistances**0.5 #同上
sortedDistIndicies = distances.argsort() #排完序从小到大的索引值
classCount={}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1 #dir 计数
sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1), reverse = True) #迭代, 从大到小 以第一个域大小为主
return sortedClassCount[0][0]
#读数据
def file2matrix(filename):
fr = open(filename)#打开文件
arrayOLines = fr.readlines()#读取
numberOfLines = len(arrayOLines) #一维长度
returnMat = zeros((numberOfLines, 3)) # n * 3 的 0矩阵
classLabelVector = []
index = 0
for line in arrayOLines:
line = line.strip() #删除字符串前后空白符
listFromLine = line.split('\t') #以'\t' 切割
returnMat[index,:] = listFromLine[0:3]
classLabelVector.append(int(listFromLine[-1]))
index += 1
return returnMat,classLabelVector
#图形化
def picture():
fig = plt.figure()
ax = fig.add_subplot(111) #把图像分成1行1列 左到右 上到下 第1块
#ax.scatter(datingDataMat[:,1], datingDataMat[:,2])#x 为 数组1, y为数组2
ax.scatter(datingDataMat[:,1], datingDataMat[:,2], 15.0 * array(datingLabels), 15.0 * array(datingLabels)) # 形状 颜色
plt.show()
#归一化
def autoNorm(dataSet):
minVals = dataSet.min(0) #0 代表列
maxVals = dataSet.max(0)
range = maxVals -minVals
normDataSet = zeros(shape(dataSet))
m = dataSet.shape[0]
normDataSet = dataSet - tile(minVals, (m,1))
normDataSet = normDataSet/tile(range, (m,1)) #矩阵除法
return normDataSet, range, minVals
#检验分类器错误率 %10的测试数据
def datingClassTest():
hoRatio = 0.10
datingDataMat,datingLabels = file2matrix("/Users/yinfeng/Downloads/rar/machinelearninginaction/Ch02/datingTestSet2.txt") #路径啊!!!
normMat, ranges, minVals = autoNorm(datingDataMat)
m = normMat.shape[0]
numTestVecs = int(m * hoRatio)
errorCount = 0.0
for i in range(numTestVecs):
classifierResult = classify0(normMat[i,:], normMat[numTestVecs:m,:],datingLabels[numTestVecs:m], 3)
print("the classifier came back with %d, the real answer is: %d" % (classifierResult, datingLabels[i]))
if(classifierResult != datingLabels[i]): errorCount += 1.0
print("the total error rate is: %f" % (errorCount/float(numTestVecs)))
#约会网站测试函数
def classifyPerson():
resultList = ['not at all', 'in small doses', 'in large doses']
percentTats = float(input("percentage of time spent playing video games?"))
ffMiles = float(input("frequent flier miles earned per year?"))
iceCream = float(input("liters of ice cream consumed per year?"))
datingDataMat,datingLabels = file2matrix("/Users/yinfeng/Downloads/rar/machinelearninginaction/Ch02/datingTestSet2.txt")
normMat,ranges, minVals = autoNorm(datingDataMat)
inArr = array([ffMiles, percentTats, iceCream])
classifierResult = classify0((inArr-minVals)/ranges, normMat, datingLabels, 3)
print("You will probably like this person: ",
resultList[classifierResult - 1])
#把图像转换成特征值
def img2vector(filename):
returnVect = zeros((1,1024))
fr = open(filename)
for i in range(32):
lineStr = fr.readline()
for j in range(32):
returnVect[0, 32 * i + j] = int(lineStr[j])
return returnVect
#测试数字 mac os 有一个隐藏文件 .DS_Store
def handwritingClassTest():
hwLabels = []
trainingFileList = listdir("/Users/yinfeng/Downloads/rar/machinelearninginaction/Ch02/digits/trainingDigits")
m = len(trainingFileList)
#print(trainingFileList[1])
trainingMat = zeros((m,1024))
for i in range(m):
if(trainingFileList[i] == ".DS_Store"): continue
fileNameStr = trainingFileList[i]
fileStr = fileNameStr.split('.')[0] #切割函数
classNumStr = int(fileStr.split('_')[0])
hwLabels.append(classNumStr)
trainingMat[i,:] = img2vector("/Users/yinfeng/Downloads/rar/machinelearninginaction/Ch02/digits/trainingDigits/%s" % fileNameStr)
testFileList = listdir('/Users/yinfeng/Downloads/rar/machinelearninginaction/Ch02/digits/testDigits')
errorCount = 0.0
mTest = len(testFileList)
for i in range(mTest):
if(testFileList[i] == ".DS_Store"): continue
fileNameStr = testFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
vectorUnderTest = img2vector('/Users/yinfeng/Downloads/rar/machinelearninginaction/Ch02/digits/testDigits/%s' % fileNameStr)
classifierResult = classify0(vectorUnderTest, trainingMat, hwLabels, 3)
print("the classifier came back with %d, the real answer is: %d" % (classifierResult, classNumStr))
if (classifierResult != classNumStr): errorCount += 1.0
print("\nthe total number of errors is %d" % errorCount)
print("\nthe total error rate is: %f" % (errorCount/float(mTest)))
机器学习实战 kNN算法
最新推荐文章于 2024-04-01 22:48:41 发布