注意matplotlib中画散点图的scatter函数的参数c的用法
c的值不可以是列向量
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
# 1. 数据生成
N = 100
x = np.random.uniform(low=-10, high=10, size=(N, 2))
y = np.dot(x, [[5], [-3]])
y[y < 0] = 0
y[y > 0] = 1
y = y.astype(np.int)
# print(y)
'''
[[0]
[0]
[0]
[0]
[1]
[0]
...
'''
# 2. 可视化
plt.scatter(x=x[:, 0], y=x[:, 1], s=10, c=y) # s:点的大小 c:点的颜色
plt.xlabel('x1')
plt.ylabel('x2')
plt.show()
此图本程序运行不出来的,此图是上面scatter函数不写参数c的时候运行出来的,但是那样图中两个类别的点是同一种颜色,想换成两种颜色,就得加参数c
报错
ValueError: 'c' argument has 100 elements, which is not acceptable for use with 'x' with size 100, '
把下面一行代码
plt.scatter(x=x[:, 0], y=x[:, 1], s=10, c=y) # s:点的大小 c:点的颜色
改为下面代码即可
plt.scatter(x=x[:, 0], y=x[:, 1], s=10, c=np.squeeze(y)) # s:点的大小 c:点的颜色