3 F: V2 ]& H) Q/ l# t5 Z) o F分类算法python程序_python3机器学习经典实例-学习笔记6-分类算法( C6 r- p4 Z/ Q' m J O/ H
创建一个简单的分类器首先本程序需要用到的数据包 import numpy as np import matplotlib.pyplot as plt 补充:python中的list和array的不同之处 python中的list是python的内置数据类型,list中的数据类不必相同的,而array的中的类型必须全部相同。在list中的数据类型保存的是数据的存放的地址,简单的说就是指针,并非数据,这样保存一个list就太麻烦了,例如list1=[1,2,3,'a']需要4个指针和四个数据,增加了存储和消耗cpu。 numpy中封装的array有很强大的功能,里面存放的都是相同的数据类型生成二维输入的数据array和标签labels # input data X = np.array([[3,1], [2,5], [1,8], [6,4], [5,2], [3,5], [4,7], [4,-1]]) # labels y = [0, 1, 1, 0, 0, 1, 1, 0]基于y的值标签将输入数据class_0 和class_1 分为两类 # separate the data into classes based on 'y' class_0 = np.array([X for i in range(len(X)) if y==0]) class_1 = np.array([X for i in range(len(X)) if y==1])将分好的class_0 和class_1 进行作图,并以不同的形状进行数据的标记。结果如下图。 # plot input data plt.figure() plt.scatter(class_0[:,0], class_0[:,1], color='black', marker='s') plt.scatter(class_1[:,0], class_1[:,1], color='black', marker='x')![]() 这里是自己生成的一条直线,并不是依据分类的节点的坐标。也就是说本例只是让我们简单的知道分类的形式。画线斜率为1的直线 # draw the separator line line_x = range(10) line_y = line_x 将分类的结果进行图像显示: # plot labeled data and separator line plt.figure() plt.scatter(class_0[:,0], class_0[:,1], color='black', marker='s') plt.scatter(class_1[:,0], class_1[:,1], color='black', marker='x') plt.plot(line_x, line_y, color='black', linewidth=3) plt.show() 结果如下:![]() 未完待续。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
* T, V1 G! j/ t) C" K+ ? a- K7 r, `
|