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)的更多相关文章

  1. 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  2. 吴裕雄 python 机器学习——KNN分类KNeighborsClassifier模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  3. 吴裕雄 python 机器学习-KNN算法(1)

    import numpy as np import operator as op from os import listdir def classify0(inX, dataSet, labels, ...

  4. 吴裕雄 python 机器学习——半监督学习LabelSpreading模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...

  5. 吴裕雄 python 机器学习——半监督学习标准迭代式标记传播算法LabelPropagation模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...

  6. 吴裕雄 python 机器学习——分类决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

  7. 吴裕雄 python 机器学习——回归决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

  8. 吴裕雄 python 机器学习——线性判断分析LinearDiscriminantAnalysis

    import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...

  9. 吴裕雄 python 机器学习——逻辑回归

    import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...

随机推荐

  1. Java - 22 Java 多态

    Java 多态 多态是同一个行为具有多个不同表现形式或形态的能力. 多态性是对象多种表现形式的体现. 比如我们说"宠物"这个对象,它就有很多不同的表达或实现,比如有小猫.小狗.蜥蜴 ...

  2. CentOS7.4安装部署openstack [Liberty版] (一)

    一.OpenStack简介 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack是一个 ...

  3. Tomcat启动报错:Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies

    错误代码如下: Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for ...

  4. selenium元素定位Xpath,Contains,CssSelector

    最近有人问到定位问题,基本上我用以下三个方法可解决,但不同的项目使用方法不一样.以下为自己所用的简单记录说明 1.Xpath 经常使用且最能解决问题的定位 driver.findElement(By. ...

  5. 五大排序算法(Python)

    冒泡排序 冒泡排序通常是在CS入门课程中教的,因为它清楚地演示了排序是如何工作的,同时又简单易懂.冒泡排序步骤遍历列表并比较相邻的元素对.如果元素顺序错误,则交换它们.重复遍历列表未排序部分的元素,直 ...

  6. HDU1847 Good Luck in CET-4 Everybody 博弈 SG函数

    题意:给定n张牌,两个人轮流摸牌,每次摸牌张数为2的幂次,问先手胜还是后手胜  n≤1000 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 # ...

  7. 关于QT_Creator不能在线调试问题

    电脑:W7+64位,QT:5_7_0(vs2015版本) 用QTcreator进行在线调试时出现找不到“engine...”,原因是没有在线调试软件 CDB下载地址:http://msdn.micro ...

  8. django, tornado

    django  由多线程写的 tornaod  由epoll机制

  9. Exchange Tech Issues 参考网站

    Exchange Tech Issues: https://www.experts-exchange.com/ DAG部署: http://blog.51cto.com/4096415/958671

  10. 《算法》第三章部分程序 part 3

    ▶ 书中第三章部分程序,加上自己补充的代码,红黑树 ● 红黑树,大部分方法与注释与二叉树相同 package package01; import java.util.NoSuchElementExce ...