数据分布
使用到了seaborn
介绍:
Seaborn 是基于 Matplotlib 核心库进行了更高级的 API 封装,可以让你轻松地画出更漂亮的图形。而 Seaborn 的漂亮主要体现在配色更加舒服、以及图形元素的样式更加细腻。
安装:
安装 pip3 install seaborn
seaborn.lmplot() 是一个非常有用的方法,它会在绘制二维散点图时,自动完成回归拟合
sns.lmplot() 里的 x, y 分别代表横纵坐标的列名,
data= 是关联到数据集,
hue=*代表按照 species即花的类别分类显示,
fit_reg=是否进行线性拟合。
案例:
%matplotlib inline
# 内嵌绘图
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# 把数据转换成dataframe的格式
iris_d = pd.DataFrame(iris['data'], columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width'])
iris_d['Species'] = iris.target
def plot_iris(iris, col1, col2):
sns.lmplot(x = col1, y = col2, data = iris, hue = "Species", fit_reg = False)
plt.xlabel(col1)
plt.ylabel(col2)
plt.title('鸢尾花种类分布图')
plt.show()
plot_iris(iris_d, 'Petal_Width', 'Sepal_Length')
数据集的划分
机器学习一般的数据集会划分为两个部分:
训练数据:用于训练,构建模型
测试数据:在模型检验时使用,用于评估模型是否有效
划分比例:
训练集:70% 80% 75%
测试集:30% 20% 25%
数据集划分api
sklearn.model_selection.train_test_split(arrays, *options)
x 数据集的特征值
y 数据集的标签值,目标值
test_size 测试集的大小,一般为float
random_state 随机数种子,不同的种子会造成不同的随机采样结果。相同的种子采样结果相同。
return 测试集特征训练集特征值值,训练标签,测试标签(默认随机取)
案例
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
**#1、获取鸢尾花数据集**
iris = load_iris()
**#对鸢尾花数据集进行分割**
**#训练集的特征值x_train 测试集的特征值x_test 训练集的目标值y_train 测试集的目标值y_test,顺序不能乱**
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
print("x_train:\n", x_train.shape)
**随机数种子**
x_train1, x_test1, y_train1, y_test1 = train_test_split(iris.data, iris.target, random_state=6)
x_train2, x_test2, y_train2, y_test2 = train_test_split(iris.data, iris.target, random_state=6)
print("如果随机数种子不一致:\n", x_train == x_train1)
print("如果随机数种子一致:\n", x_train1 == x_train2)```
本文介绍了如何利用Seaborn库查看数据分布,并展示了Seaborn.lmplot()方法在二维散点图上的应用,包括数据集的分类显示和线性拟合。同时,讨论了机器学习中数据集的常见划分方法,通常分为训练集和测试集,比例如70%训练,30%测试,并提到了数据集划分的API。
1459

被折叠的 条评论
为什么被折叠?



