资源简介
《机器学习实战》源代码Python3
代码片段和文件信息
‘‘‘
Created on Sep 16 2010
kNN: k Nearest Neighbors
Input: inX: vector to compare to existing dataset (1xN)
dataSet: size m data set of known vectors (NxM)
labels: data set labels (1xM vector)
k: number of neighbors to use for comparison (should be an odd number)
Output: the most popular class label
@author: pbharrin
‘‘‘
import numpy as np
import operator
from os import listdir
def classify0(inX dataSet labels k):
dataSetSize = dataSet.shape[0]
diffMat = np.tile(inX (dataSetSize 1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances**0.5
sortedDistIndicies = distances.argsort()
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel 0) + 1
sortedClassCount = sorted(classCount.items() key=operator.itemgetter(1) reverse=True)
return sortedClassCount[0][0]
def createDataSet():
group = np.array([[1.0 1.1] [1.0 1.0] [0 0] [0 0.1]])
labels = [‘A‘ ‘A‘ ‘B‘ ‘B‘]
return group labels
def file2matrix(filename):
love_dictionary = {‘largeDoses‘:3 ‘smallDoses‘:2 ‘didntLike‘:1}
fr = open(filename)
arrayOLines = fr.readlines()
numberOfLines = len(arrayOLines) #get the number of lines in the file
returnMat = np.zeros((numberOfLines 3)) #prepare matrix to return
classLabelVector = [] #prepare labels return
index = 0
for line in arrayOLines:
line = line.strip()
listFromLine = line.split(‘\t‘)
returnMat[index :] = listFromLine[0:3]
if(listFromLine[-1].isdigit()):
classLabelVector.append(int(listFromLine[-1]))
else:
classLabelVector.append(love_dictionary.get(listFromLine[-1]))
index += 1
return returnMat classLabelVector
def autoNorm(dataSet):
minVals = dataSet.min(0)
maxVals = dataSet.max(0)
ranges = maxVals - minVals
normDataSet = np.zeros(np.shape(dataSet))
m = dataSet.shape[0]
normDataSet = dataSet - np.tile(minVals (m 1))
normDataSet = normDataSet/np.tile(ranges (m 1)) #element wise divide
return normDataSet ranges minVals
def datingClassTest():
hoRatio = 0.50 #hold out 10%
datingDataMat datingLabels = file2matrix(‘datingTestSet2.txt‘) #load data setfrom file
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)))
print(errorC
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\
文件 1045 2017-12-25 12:43 machinelearninginaction3x-master\.gitignore
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\EXTRAS\
文件 514 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\EXTRAS\README.txt
文件 1988 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\EXTRAS\createDist.py
文件 2094 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\EXTRAS\createDist2.py
文件 557 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\EXTRAS\createFirstPlot.py
文件 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\EXTRAS\testSet.txt
文件 239 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\README.txt
文件 34725 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\datingTestSet.txt
文件 26067 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\datingTestSet2.txt
文件 739988 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\digits.zip
文件 5230 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\kNN.py
文件 600 2017-12-25 12:43 machinelearninginaction3x-master\Ch02\kNNTest.py
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch03\
文件 84 2017-12-25 12:43 machinelearninginaction3x-master\Ch03\classifierStorage.txt
文件 771 2017-12-25 12:43 machinelearninginaction3x-master\Ch03\lenses.txt
文件 3848 2017-12-25 12:43 machinelearninginaction3x-master\Ch03\treePlotter.py
文件 4021 2017-12-25 12:43 machinelearninginaction3x-master\Ch03\trees.py
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\EXTRAS\
文件 514 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\EXTRAS\README.txt
文件 928 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\EXTRAS\create2Normal.py
文件 445 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\EXTRAS\monoDemo.py
文件 7250 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\bayes.py
文件 15141 2017-12-25 12:43 machinelearninginaction3x-master\Ch04\email.zip
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch05\
目录 0 2017-12-25 12:43 machinelearninginaction3x-master\Ch05\EXTRAS\
文件 514 2017-12-25 12:43 machinelearninginaction3x-master\Ch05\EXTRAS\README.txt
文件 1233 2017-12-25 12:43 machinelearninginaction3x-master\Ch05\EXTRAS\plot2D.py
............此处省略111个文件信息
- 上一篇:pygame之《飞机大战》
- 下一篇:3D打印机控制软件Cura源码
相关资源
- Python3.5.2的IDLE汉化版计算机等级考试
- python3.5 百度ai人脸识别
- python3实现的国密SM2+SM3
- Python-使用DeepFakes实现YouTube视频自动换
- Introduction to machine learning with python (
- python新浪微博爬虫,爬取微博和用户
- Python-一系列高品质的动漫人脸数据集
- Python-Insightface人脸检测识别的最小化
- 非线性回归Python代码
- 093 2018北风网人工智能视频(完结)转
- python的色情图片识别
- python100道面试题及解答全部答案 pyc
- #python3.3关于Tk中的Treeview使用方法
- Python3.x+Pyqt5实现界面编程浏览网页
- 贝叶斯网络程序
- 《机器学习实战》Python3代码
- Python3学习笔记
- Python3.7.2中文文档-标准库-通用操作系
- Python3.7.2中文文档-标准库-Python数据类
- python3基础教程第三版高清
- Python-自然场景文本检测PSENet的一个
- Python-在特征金字塔网络FPN的Pytorch实现
- Python-PyTorch实时多人姿态估计项目的实
- Python-用PyTorch10实现FasterRCNN和MaskRCNN比
- Python-心脏核磁共振MRI图像分割
- Python-基于YOLOv3的行人检测
- Python-RLSeq2Seq用于SequencetoSequence模型的
- Python-PyTorch对卷积CRF的参考实现
- Python-高效准确的EAST文本检测器的一个
- Python-pytorch实现的人脸检测和人脸识别
评论
共有 条评论