吴裕雄 python 机器学习-KNN(2)
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle n = 1000 #number of points to create
xcord = np.zeros((n))
ycord = np.zeros((n))
markers =[]
colors =[]
fw = open('D:\\LearningResource\\machinelearninginaction\\Ch02\\EXTRAS\\testSet.txt','w') for i in range(n):
[r0,r1] = np.random.standard_normal(2)
myClass = np.random.uniform(0,1)
if (myClass <= 0.16):
fFlyer = np.random.uniform(22000, 60000)
tats = 3 + 1.6*r1
markers.append(20)
colors.append(2.1)
classLabel = 1 #'didntLike'
print(("%d, %f, class1") % (fFlyer, tats))
elif ((myClass > 0.16) and (myClass <= 0.33)):
fFlyer = 6000*r0 + 70000
tats = 10 + 3*r1 + 2*r0
markers.append(20)
colors.append(1.1)
classLabel = 1 #'didntLike'
print(("%d, %f, class1") % (fFlyer, tats))
elif ((myClass > 0.33) and (myClass <= 0.66)):
fFlyer = 5000*r0 + 10000
tats = 3 + 2.8*r1
markers.append(30)
colors.append(1.1)
classLabel = 2 #'smallDoses'
print(("%d, %f, class2") % (fFlyer, tats))
else:
fFlyer = 10000*r0 + 35000
tats = 10 + 2.0*r1
markers.append(50)
colors.append(0.1)
classLabel = 3 #'largeDoses'
print(("%d, %f, class3") % (fFlyer, tats))
if (tats < 0):
tats =0
if (fFlyer < 0):
fFlyer =0
xcord[i] = fFlyer
ycord[i]=tats
fw.write("%d\t%f\t%f\t%d\n" % (fFlyer, tats, np.random.uniform(0.0, 1.7), classLabel)) fw.close() fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xcord,ycord, c=colors, s=markers)
type1 = ax.scatter([-10], [-10], s=20, c='red')
type2 = ax.scatter([-10], [-15], s=30, c='green')
type3 = ax.scatter([-10], [-20], s=50, c='blue')
ax.legend([type1, type2, type3], ["Class 1", "Class 2", "Class 3"], loc=2)
ax.axis([-5000,100000,-2,25])
plt.xlabel('Frequent Flyier Miles Earned Per Year')
plt.ylabel('Percentage of Body Covered By Tatoos')
plt.show()
...................................................
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle n = 1000 #number of points to create
xcord1 = []; ycord1 = []
xcord2 = []; ycord2 = []
xcord3 = []; ycord3 = []
markers =[]
colors =[]
fw = open('D:\\LearningResource\\machinelearninginaction\\Ch02\\EXTRAS\\testSet.txt','w') for i in range(n):
[r0,r1] = np.random.standard_normal(2)
myClass = np.random.uniform(0,1)
if (myClass <= 0.16):
fFlyer = np.random.uniform(22000, 60000)
tats = 3 + 1.6*r1
markers.append(20)
colors.append(2.1)
classLabel = 1 #'didntLike'
xcord1.append(fFlyer)
ycord1.append(tats)
elif ((myClass > 0.16) and (myClass <= 0.33)):
fFlyer = 6000*r0 + 70000
tats = 10 + 3*r1 + 2*r0
markers.append(20)
colors.append(1.1)
classLabel = 1 #'didntLike'
if (tats < 0):
tats =0
if (fFlyer < 0):
fFlyer =0
xcord1.append(fFlyer)
ycord1.append(tats)
elif ((myClass > 0.33) and (myClass <= 0.66)):
fFlyer = 5000*r0 + 10000
tats = 3 + 2.8*r1
markers.append(30)
colors.append(1.1)
classLabel = 2 #'smallDoses'
if (tats < 0):
tats =0
if (fFlyer < 0):
fFlyer =0
xcord2.append(fFlyer)
ycord2.append(tats)
else:
fFlyer = 10000*r0 + 35000
tats = 10 + 2.0*r1
markers.append(50)
colors.append(0.1)
classLabel = 3 #'largeDoses'
if (tats < 0): tats =0
if (fFlyer < 0): fFlyer =0
xcord3.append(fFlyer)
ycord3.append(tats)
fw.write("%d\t%f\t%f\t%d\n" % (fFlyer, tats, np.random.uniform(0.0, 1.7), classLabel)) fw.close()
fig = plt.figure()
ax = fig.add_subplot(111)
# ax.scatter(xcord,ycord, c=colors, s=markers)
type1 = ax.scatter(xcord1, ycord1, s=20, c='red')
type2 = ax.scatter(xcord2, ycord2, s=30, c='green')
type3 = ax.scatter(xcord3, ycord3, s=50, c='blue')
ax.legend([type1, type2, type3], ["Did Not Like", "Liked in Small Doses", "Liked in Large Doses"], loc=2)
ax.axis([-5000,100000,-2,25])
plt.xlabel('Frequent Flyier Miles Earned Per Year')
plt.ylabel('Percentage of Time Spent Playing Video Games')
plt.show()
import numpy as np
import matplotlib
import matplotlib.pyplot as plt def file2matrix(filename):
fr = open(filename)
returnMat = []
classLabelVector = [] #prepare labels return
for line in fr.readlines():
line = line.strip()
listFromLine = line.split('\t')
returnMat.append([float(listFromLine[0]),float(listFromLine[1]),float(listFromLine[2])])
classLabelVector.append(int(listFromLine[-1]))
return np.array(returnMat),np.array(classLabelVector) fig = plt.figure()
ax = fig.add_subplot(111)
datingDataMat,datingLabels = file2matrix('D:\\LearningResource\\machinelearninginaction\\Ch02\\datingTestSet2.txt')
#ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
ax.scatter(datingDataMat[:,1], datingDataMat[:,2], 15.0*np.array(datingLabels), 15.0*np.array(datingLabels))
ax.axis([-2,25,-0.2,2.0])
plt.xlabel('Percentage of Time Spent Playing Video Games')
plt.ylabel('Liters of Ice Cream Consumed Per Week')
plt.show()
吴裕雄 python 机器学习-KNN(2)的更多相关文章
- 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型
import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...
- 吴裕雄 python 机器学习——KNN分类KNeighborsClassifier模型
import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...
- 吴裕雄 python 机器学习-KNN算法(1)
import numpy as np import operator as op from os import listdir def classify0(inX, dataSet, labels, ...
- 吴裕雄 python 机器学习——半监督学习LabelSpreading模型
import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...
- 吴裕雄 python 机器学习——半监督学习标准迭代式标记传播算法LabelPropagation模型
import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...
- 吴裕雄 python 机器学习——分类决策树模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...
- 吴裕雄 python 机器学习——回归决策树模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...
- 吴裕雄 python 机器学习——线性判断分析LinearDiscriminantAnalysis
import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...
- 吴裕雄 python 机器学习——逻辑回归
import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...
随机推荐
- CS229 6.7 Neurons Networks whitening
PCA的过程结束后,还有一个与之相关的预处理步骤,白化(whitening) 对于输入数据之间有很强的相关性,所以用于训练数据是有很大冗余的,白化的作用就是降低输入数据的冗余,通过白化可以达到(1)降 ...
- MySQL库操作,表操作,数据操作。
数据库服务器:本质就是一台计算机,该计算机上安装有数据库管理软件的服务端,供客户端访问使用. 1数据库管理系统RDBMS(本质就是一个C/S架构的套接字),关系型数据库管理系统. 库:(文件夹)- ...
- Spring获取application.properties
方法一:@Value获取属性值 首先在application.properties中添加属性值 app.name=MyApp app.description=${app.name} is a Spri ...
- mySQL InnoDB 的性能问题讨论
https://ncisoft.iteye.com/blog/34676 https://www.douban.com/note/245895324/ MySQL最为人垢病的缺点就是缺乏事务的支持,M ...
- 介绍Collection框架的结构;Collection 和 Collections的区别
介绍Collection框架的结构:Collection 和 Collections的区别 集合框架: Collection:List列表,Set集 Map:Hashtable,HashMap,Tre ...
- mysql-5.5.50-winx64
1.help 2.Service 3.Configure 4.User 5.design last 1.获取帮助文档 cd C:\Program Files\mysql\mysql-5.5.50-wi ...
- Eureka 消费方
创建服务消费者 1.pom文件添加eureka的起步依赖 2.配置文件添加eureka.client相关配置 3.启动类注解@EnableDiscoveryClient 启动类: 启动后.
- redis的键命令
键的命令 查找键,参数支持正则 KEYS pattern 判断键是否存在,如果存在返回1,不存在返回0 EXISTS key [key ...] 查看键对应的value的类型 TYPE key 删除键 ...
- 【python】python与正则 re的主要用到的方法列举
[直接上代码] #coding=utf-8#1.先将正则表达式的字符串形式编译为Pattern实例 #2.使用Pattern实例处理文本并获得匹配结果 #3.最后使用Match实例获得消息,进行其他操 ...
- 11.采集手机端app企查查上司公司数据(未成功)
---恢复内容开始--- 采集企查查手机端app数据: 1.首先手机端安装app并usb连接电脑端,fiddler监控手机请求数据对数据进行分析抓取. 手机端界面与fiddler界面参照: 2.对获取 ...