分类算法python程序_python3机器学习经典实例-学习笔记6-分类算法

创建一个简单的分类器首先本程序需要用到的数据包

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[i] for i in range(len(X)) if y[i]==0])

class_1 = np.array([X[i] for i in range(len(X)) if y[i]==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()

结果如下:

未完待续。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值