这是学习机器学习算法实战这本书时,写的代码实战。让自己对各个算法有更直观的了解,不能一直不写啊。不管简单还是不简单都亲自一行一行的敲一遍啊。
具体的源码和和数据链接:https://pan.baidu.com/s/1G2S2pb5gfBnxGNNTFgTkEA 密码:fov0
# -*- coding: utf-8 -*-
# author: Yufeng Song
from numpy import*
import operator
import matplotlib
import matplotlib.pyplot as plt
import os
def createDataSet():
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
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
sqDistance = sqDiffMat.sum(axis=1)#
distances = sqDistance**0.5
sortedDistIndices = distances.argsort()
classCount={}
for i in range(k):
votelabel = labels[sortedDistIndices[i]]
classCount[votelabel] = classCount.get(votelabel,0)+1
#sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
sortedClassCount = sorted(classCount.items(),key=lambda item:item[1],reverse=True)
return sortedClassCount[0][0]
def file2matrix(filename):
fr = open(filename,"r")
arrayOfLines =fr.readlines()
numberOfLines = len(arrayOfLines)
# print(numberOfLines)
# returnMat = zeros(numberOfLines,3)
returnMat = zeros((numberOfLines,3))#里面有个小括号,别忘了啊
# a=eye(3) 单位矩阵
# print(a)
# print(returnMat)
classLabelVector = []
index = 0
for line in arrayOfLines:
line = line.strip() #删除左右两边的空格指定空格,默认是空字符串啊,lstrip(),rstrip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]#他是个二维数组所以要加这个啊
classLabelVector.append(int(listFromLine[-1]))
index += 1
return returnMat,classLabelVector
# def file2matrix(filename):
# fr = open(filename)
# numberOfLines = len(fr.readlines()) #get the number of lines in the file
# returnMat = zeros((numberOfLines,3)) #prepare matrix to return
# classLabelVector = [] #prepare labels return
# fr = open(filename)
# index = 0
# for line in fr.readlines():
# line = line.strip()
# listFromLine = line.split('\t')
# returnMat[index,:] = listFromLine[0:3]
# classLabelVector.append(int(listFromLine[-1]))
# index += 1
# return returnMat,classLabelVector
def autoNorm(dataSet):
minVals = dataSet.min(0)#选取列的最小值,而不是行的最小值
maxVals = dataSet.max(0)
ranges=maxVals-minVals
normDataSet = zeros(shape(dataSet))
m = dataSet.shape[0]
normDataSet = dataSet - tile(minVals,(m,1))
normDataSet = normDataSet/tile(ranges,(m,1))
return normDataSet,ranges,minVals
def datingClassTest():
hoRatio = 0.10
datingDataMat,datingLabels=file2matrix('datingTestSet2.txt')
normMat,ranges,minVals=autoNorm(datingDataMat)
print(normMat.shape)#(1000,3)
m = normMat.shape[0]
numTestVecs = int(m*hoRatio)#100
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 samll doses','in large doses']
percentTats = float(input("percentage of time spent playing video games?"))#python3不支持raw_input
ffMiles = float(input("frequent flier miles earned per year?"))
iceCream = float(input("liters of ice cream consumed per year?"))
datingDataMat,datingLabels = file2matrix("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
def handwritingClassTest():
hwlabels = []
trainingFileList = os.listdir('trainingDigits')
m = len(trainingFileList)
trainingMat = zeros((m,1024))
for i in range(m):
fileNameStr = trainingFileList[i]#0_0.txt
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
hwlabels.append(classNumStr)
trainingMat[i,:] = img2vector('trainingDigits/%s' %fileNameStr)
testFileList = os.listdir('testDigits')
errorCount = 0.0
mTest = len(testFileList)
for i in range(mTest):
fileNameStr = testFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
vectorUnderTest = img2vector('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
print("\nthe total number of errors is: %d" % errorCount)
print("\nthe total error rate is: %f" %(errorCount/float(mTest)))
if __name__ == '__main__':
# group,labels = createDataSet()
# print (classify0([0,0],group,labels,3))
# print(file2matrix("datingTestSet2.txt"))
# datingDataMat,datingLabels=file2matrix("datingTestSet2.txt")
# normMat,ranges,minVals = autoNorm(datingDataMat)
# fig = plt.figure()
# ax = fig.add_subplot(211)#111,与121是左右的关系啊 这几个参数要弄明白啊
# ax2 = fig.add_subplot(212)
# # ax.scatter(datingDataMat[:,1],datingDataMat[:,2])
# # ax.scatter(datingDataMat[:,1],datingDataMat[:,2],15.0*array(datingLabels),15.0*array(datingLabels))
# ax.scatter(datingDataMat[:,0],datingDataMat[:,1],15.0*array(datingLabels),15.0*array(datingLabels))
# ax2.scatter(normMat[:,0],normMat[:,1],15.0*array(datingLabels),15.0*array(datingLabels))
# plt.show()
# normMat,ranges,minVals = autoNorm(datingDataMat)
# print(normMat,ranges,minVals)
# y=pp.DS.Transac_open # 设置y轴数据,以数组形式提供
#res=[1,2,3]
# x=len(res) # 设置x轴,以y轴数组长度为宽度
# x=range(x) # 以0开始的递增序列作为x轴数据
# plt.plot(x,res) # 只提供x轴,y轴参数,画最简单图形
# plt.show()
# datingClassTest()
# classifyPerson()
# testVector = img2vector('testDigits/0_13.txt')
# print(testVector[0,0:31])
handwritingClassTest()