生成随机产生的训练数据

来源:互联网 发布:计算机二进制算法 编辑:程序博客网 时间:2024/06/05 02:52
生成随机产生的训练数据
#createDist.py
from numpy import *
import matplotlibimport matplotlib.pyplot as pltfrom matplotlib.patches import Rectanglen = 1000 #number of points to createxcord = zeros((n))#create a shuzu,length=nycord = zeros((n))markers =[]#used to give different date with different sizecolors =[]#used to give different date with different colorfw = open('testSet.txt','w')#open a file in a write wayfor i in range(n):#give n=1000 date diferent value    [r0,r1] = random.standard_normal(2)#create a list whitch has two elements    myClass = random.uniform(0,1)#used to classify    if (myClass <= 0.16):        fFlyer = 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, random.uniform(0.0, 1.7), classLabel))#put the value in a filefw.close()#close the opend filefig = plt.figure()#make one figureax = fig.add_subplot(2,1,1)#the part of the figuretype1 = ax.scatter([-10000], [-10], s=20, c='red')#make a scater picture,s=size,c=color,whitch can be iterable maker='o'type2 = ax.scatter([-10000], [-15], s=30, c='green')type3 = ax.scatter([-10000], [-20], s=5, c='black')ax.legend([type1, type2, type3], ["Class 1", "Class 2", "Class 3"], loc=2)#ax=fig.add_subplot(2,1,2)#the sencond part of the figureax.scatter(xcord,ycord, c=colors, s=markers)#x,y,color,size,all of them can be iterable#ax.axis([-5000,100000,-2,25])plt.xlabel('Frequent Flyier Miles Earned Per Year')plt.ylabel('Percentage of Body Covered By Tatoos')plt.show()