Python机器学习(2)-Matplotlib数据可视化

719 阅读3分钟

导入numpy和matplotlib

import numpy as np
import matplotlib.pyplot as plt

回执正弦函数

import numpy as np
import matplotlib.pyplot as plt

# 在0~10之间生成一百个点
x = np.linspace(0,10,100)
# 对x中的每个点做sin
y = np.sin(x)
# 以x为横轴,y为纵轴绘制,x和y的关系
plt.plot(x,y)
# plt.show()

image.png

同时在一个画布中绘制cos和sin曲线

cosy = np.cos(x)
siny = y.copy()
plt.plot(x,siny);plt.plot(x,cosy)

image.png

指定线条颜色和样式

指定线条颜色为空色,线条样式为--

plt.plot(x,siny,color = "red",linestyle = "--")

image.png

调节坐标系范围

sin和cos的值域范围在-1到1,我们刚刚指定的x的范围为0~10,matplotlib根据参数范围,自适应的形式,绘制了上面的图形。我们也可以通过手动指定坐标系的范围控制绘制的图形。

  1. 调节x坐标系的范围为[-5~15],y的坐标系范围为[0~1]
plt.plot(x,siny);plt.xlim(-5,15),plt.ylim(0,1)

image.png

  1. 通过axis同时调节x,y坐标系范围 指定x坐标系范围为[-2~2],y坐标系范围为[-1,11]
plt.plot(x,siny);plt.axis([-1,11,-2,2])

image.png

添加图示,标题和x,y坐标轴含义

plt.plot(x,siny,label="sin(x)");
plt.plot(x,cosy,label = "cos(x)");
plt.xlabel("x");
plt.ylabel("y");
plt.title('sin&cos');
plt.legend()
plt.show()

image.png

绘制散点图

plt.scatter(x,siny)

image.png

生成符合正态分布的散点图

x和y分别生成从0到1,一百个正态分布的点

x = np.random.normal(0,1,100)
y = np.random.normal(0,1,100)
plt.scatter(x,y)

image.png

设置散点透明度

设置不透明度为0.1

x = np.random.normal(0,1,10000)
y = np.random.normal(0,1,10000)
plt.scatter(x,y,alpha = 0.3)

image.png

读取数据和简单的数据探索

引入需要使用的包

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

查看鸢尾花数据集

# 引入鸢尾花数据集
iris = datasets.load_iris()
# 查看数据集有哪些信息
iris.keys()
#out: dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])

# 鸢尾花数据集信息
iris.data.shape
# (150, 4) 表示有150个数据,每条数据有4个特征

# 查看每个向量中的每个特征是什么含义
iris.feature_names
# out: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

# 鸢尾花的判定结果
iris.target

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

iris.target_names 
#out: array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),target中的0,1,2分别表示
# setosa,versicolor,virginica种类

鸢尾花数据可视化

因为鸢尾花的一个向量有4个特征,属于四维空间。无法使用图形表示,我们可以两个两个向量的形式绘制。

# 获取所有的行数据集,列数据集取前两列
x = iris.data[:,:2]
x.shape
# (150,2)
plt.scatter(x[:,0],x[:,1])

image.png

鸢尾花每个类别分别绘制

y = iris.target
plt.scatter(x[y==0,0],x[y==0,1],color = 'red');
plt.scatter(x[y==1,0],x[y==1,1],color = 'blue');
plt.scatter(x[y==2,0],x[y==2,1],color = 'green')
plt.show()

image.png

通过对不同种类的鸢尾花进行可视化后,发现蓝色种类和绿色种类的鸢尾花的前两列特征不是非常的明显,我们可以看一下后面两列特征对于鸢尾花种类的区分。

x = iris.data[:,2:]
plt.scatter(x[y==0,0],x[y==0,1],color = 'red');
plt.scatter(x[y==1,0],x[y==1,1],color = 'blue');
plt.scatter(x[y==2,0],x[y==2,1],color = 'green')

image.png