import sklearn
#全部行都能输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
from sklearn.neighbors import KNeighborsClassifier
x=[[0],[1],[2],[3]] # 2维
y = [0,0,1,1]
# 实例化API
estimator = KNeighborsClassifier(n_neighbors=2)
# 使用fit方法进行训练
estimator.fit(x,y)
KNeighborsClassifier(n_neighbors=2)
# 预测
estimator.predict([[1]])
estimator.predict([[100]])
estimator.predict([[-1]])
array([0])
array([1])
array([0])
1.sklearn
优势:
1.文档多,且规范
2.包含算法多
3.实现起来容易
2.sklearn中包含内容
分类、回归、聚类
特征工程
模型选择、调优
3.KNN中的api
sklearn.neighbors.KNeighborsClassifier(n_neighbors=5)
参数:
n_neighbors -- 选定参考几个邻居
4.机器学习中实现的过程
1.实例化一个估计器
2.使用fit方法进行训练
1. 欧氏距离
2. 曼哈顿距离(城市街区距离)
3. 切比雪夫距离
4. 闵可夫斯基距离(Minkowski Distance) Lp范数
5. 标准化欧氏距离:在计算过程中加入标准差,对量刚数据进行处理
6. 余弦距离:cos思想
7. 汉明距离(Hamming Distance)
两个等长字符串s1与s2的汉明距离为:将其中一个变为另外一个所需要作的最小字符替换次数
8. 杰卡德距离: 通过交并集进行统计
9. 马氏距离:通过样本分布进行计算
容易受到异常点的影响
受到样本均衡的问题
近似误差 – 过拟合 – 在训练集上表现好,测试集表现不好
估计误差好才是真的好
# 导入数据
from sklearn.datasets import load_iris,fetch_20newsgroups
# 1. 数据集获取
# 1.1 小数据集获取
iris = load_iris()
print(iris)
# 1.2 大数据集获取
# news = fetch_20newsgroups()
# print(news)
print('数据集中的特征值是:\n',iris.data)
print('数据集中的目标值是:\n',iris['target'])
print('数据集中的特征值名字是:\n',iris.feature_names)
print('数据集中的目标值名字是:\n',iris.target_names)
print('数据集的描述:\n',iris.DESCR)
### 2.3 数据可视化
- 创建一些图进行可视化,以查看不同类别是如何通过特征来区分的
- Seaborn:画图更加美观舒适
- 安装:pip install seaborn
- seaborn.lmplot()-- 是一种非常有用的方法,在绘制二维散点图时可以自动完成拟合
- sns.lmplot(x,y,data,hue= ,fit_reg= ) -- 里的x,y代表横纵坐标的列名
- data= -- 具体数据集
- hue=* -- 目标值是什么,即代表按照species即花的类别分类显示
- fit_reg= -- 是否进行线性拟合
```python
%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# 把数据转换成dataframe的格式
iris_d = pd.DataFrame(data=iris.data, columns=['sepal length', 'sepal width', 'petal length', 'petal width'])
iris_d['Species']= iris.target
# print(iris_d)
sepal length sepal width petal length petal width Species
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
.. ... ... ... ... ...
145 6.7 3.0 5.2 2.3 2
146 6.3 2.5 5.0 1.9 2
147 6.5 3.0 5.2 2.0 2
148 6.2 3.4 5.4 2.3 2
149 5.9 3.0 5.1 1.8 2
[150 rows x 5 columns]
def plot_iris(data,col1,col2):
sns.lmplot(x=col1,y=col2,data=data)
plt.show()
plot_iris(iris_d,'sepal length','sepal width')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1y1ksBFZ-1665913904350)(output_39_0.png)]
def plot_iris(data,col1,col2):
sns.lmplot(x=col1,y=col2,data=data,hue='Species') # 按Species类别分
plt.show()
plot_iris(iris_d,'sepal length','sepal width')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMobPddo-1665913904352)(output_40_0.png)]
def plot_iris(data,col1,col2):
sns.lmplot(x=col1,y=col2,data=data,hue='Species',fit_reg=False) # 不需要拟合曲线
plt.title('iris')
plt.xlabel(col1)
plt.ylabel(col2)
plt.show
plot_iris(iris_d,'sepal length','sepal width')
plot_iris(iris_d, 'sepal width', 'petal length') # 可以有6种组合
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NmkZTyVD-1665913904353)(output_41_0.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GtnnoIAn-1665913904354)(output_41_1.png)]
sklearn.model_selection.train_test_split(arrays,*options)
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(iris_d, iris.target)
print('训练集的特征值是:\n',x_train)
print('测试集的特征值是:\n',x_test)
print('训练集的目标值是:\n',y_train)
print('测试集的目标值是:\n',y_test)
训练集的特征值是:
sepal length sepal width petal length petal width Species
104 6.5 3.0 5.8 2.2 2
103 6.3 2.9 5.6 1.8 2
131 7.9 3.8 6.4 2.0 2
36 5.5 3.5 1.3 0.2 0
110 6.5 3.2 5.1 2.0 2
.. ... ... ... ... ...
108 6.7 2.5 5.8 1.8 2
24 4.8 3.4 1.9 0.2 0
142 5.8 2.7 5.1 1.9 2
76 6.8 2.8 4.8 1.4 1
85 6.0 3.4 4.5 1.6 1
[112 rows x 5 columns]
测试集的特征值是:
sepal length sepal width petal length petal width Species
16 5.4 3.9 1.3 0.4 0
128 6.4 2.8 5.6 2.1 2
129 7.2 3.0 5.8 1.6 2
139 6.9 3.1 5.4 2.1 2
2 4.7 3.2 1.3 0.2 0
125 7.2 3.2 6.0 1.8 2
94 5.6 2.7 4.2 1.3 1
82 5.8 2.7 3.9 1.2 1
58 6.6 2.9 4.6 1.3 1
45 4.8 3.0 1.4 0.3 0
22 4.6 3.6 1.0 0.2 0
44 5.1 3.8 1.9 0.4 0
113 5.7 2.5 5.0 2.0 2
3 4.6 3.1 1.5 0.2 0
34 4.9 3.1 1.5 0.2 0
116 6.5 3.0 5.5 1.8 2
81 5.5 2.4 3.7 1.0 1
65 6.7 3.1 4.4 1.4 1
35 5.0 3.2 1.2 0.2 0
31 5.4 3.4 1.5 0.4 0
54 6.5 2.8 4.6 1.5 1
124 6.7 3.3 5.7 2.1 2
93 5.0 2.3 3.3 1.0 1
145 6.7 3.0 5.2 2.3 2
14 5.8 4.0 1.2 0.2 0
96 5.7 2.9 4.2 1.3 1
37 4.9 3.6 1.4 0.1 0
92 5.8 2.6 4.0 1.2 1
63 6.1 2.9 4.7 1.4 1
17 5.1 3.5 1.4 0.3 0
146 6.3 2.5 5.0 1.9 2
114 5.8 2.8 5.1 2.4 2
71 6.1 2.8 4.0 1.3 1
23 5.1 3.3 1.7 0.5 0
50 7.0 3.2 4.7 1.4 1
39 5.1 3.4 1.5 0.2 0
148 6.2 3.4 5.4 2.3 2
78 6.0 2.9 4.5 1.5 1
训练集的目标值是:
[2 2 2 0 2 2 1 1 2 0 2 1 0 2 1 1 1 2 0 1 0 1 1 0 1 2 1 1 0 1 2 1 0 1 2 1 1
2 0 0 0 0 2 0 1 2 2 2 0 0 0 0 0 1 2 1 1 2 2 2 2 0 2 2 0 2 0 0 2 1 2 2 2 0
1 0 0 0 2 1 2 1 2 1 0 2 0 0 1 0 1 2 1 1 0 1 1 2 0 0 1 1 0 2 1 2 0 2 0 2 1
1]
测试集的目标值是:
[0 2 2 2 0 2 1 1 1 0 0 0 2 0 0 2 1 1 0 0 1 2 1 2 0 1 0 1 1 0 2 2 1 0 1 0 2
1]
print('训练集的目标值形状:\n',y_train.shape)
print('测试集的目标值形状:\n',y_test.shape) # 112:38,如果想改变训练集和测试机比重
训练集的目标值形状:
(112,)
测试集的目标值形状:
(38,)
x_train, x_test, y_train, y_test = train_test_split(iris_d, iris.target, test_size=0.5)
print('训练集的特征值是:\n',x_train)
print('测试集的特征值是:\n',x_test)
print('训练集的目标值形状:\n',y_train.shape)
print('测试集的目标值形状:\n',y_test.shape) # 75:75
训练集的目标值形状:
(75,)
测试集的目标值形状:
(75,)
# 随机数种子一样时,结果一样
x_train1, x_test1, y_train1, y_test1 = train_test_split(iris_d, iris.target, test_size=0.2,random_state=2)
x_train2, x_test2, y_train2, y_test2 = train_test_split(iris_d, iris.target, test_size=0.2,random_state=22)
x_train3, x_test3, y_train3, y_test3 = train_test_split(iris_d, iris.target, test_size=0.2,random_state=22)
print('测试集的目标值是:\n',y_test1)
print('测试集的目标值是:\n',y_test2)
print('测试集的目标值是:\n',y_test3)
测试集的目标值是:
[0 0 2 0 0 2 0 2 2 0 0 0 0 0 1 1 0 1 2 1 1 1 2 1 1 0 0 2 0 2]
测试集的目标值是:
[0 2 1 2 1 1 1 2 1 0 2 1 2 2 0 2 1 1 2 1 0 2 0 1 2 0 2 2 2 2]
测试集的目标值是:
[0 2 1 2 1 1 1 2 1 0 2 1 2 2 0 2 1 1 2 1 0 2 0 1 2 0 2 2 2 2]
通过一些转换函数将特征数据转换成更加适合算法模型的特征数据过程:归一化、标准化
sklearn.preprocessing
通过对原始数据进行变换把数据映射到【0,1】之间
x’=(x-min) / (max-min)
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
# data = pd.read_csv('./data/dating.txt')
print(data)
# 1.归一化
# 1.1实例化一个转化器
transfer =MinMaxScaler(feature_range=(0,10))
# 1.2调用fit_transfrom方法
minmax_data = transfer.fit_transform(data[['milage','Liters','Consumtime']])
print('经过归一化数据处理之后的数据为:\n',minmax_data)
通过对原始数据进行数据变换到均值为0,标准差为1范围内
X'= (x-mean) / σ
- sklearn.preprocessing.StandardScaler()
- 处理之后每列来说所有数据都聚集在均值为0附近,标准差为1
- StandardScaler.fit_transform(X)
X:numpy array数据[n_samples,n_features]
- 返回值:转换后的形状相同的array
from sklearn.preprocessing import StandardScaler
# 2.标准化
# 2.1实例化一个转换器
transfer = StandardScaler()
# 2.2调用fit_transfrom方法
standard_data = transfer.fit_transform(data[['milage','Liters','Consumtime']])
print('经过标准化处理之后的数据为:\n',standard_data)
sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, algorithm=‘auto’)
1.获取数据集
2.数据基本处理
3.特征工程
4.机器学习
5.模型评估
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
# 数据异常值处理(这里数据收集的比较好,省略)
# 2.1 数据分割
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22, test_size=0.2)
# 3.1 实例化一个转换器
transfer = StandardScaler()
# 3.2 调用fit_transform方法
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 4.1 实例化一个估计器
estimator = KNeighborsClassifier(n_neighbors=5)
# 4.2 模型训练
estimator.fit(x_train, y_train)
KNeighborsClassifier()
# 5.1 输出预测值
y_pre = estimator.predict(x_test)
print('测试集的预测值是:\n',y_pre)
print('预测值和真实值对比:\n',y_pre == y_test)
测试集的预测值是:
[0 2 1 1 1 1 1 1 1 0 2 1 2 2 0 2 1 1 1 1 0 2 0 1 1 0 1 1 2 1]
预测值和真实值对比:
[ True True True False True True True False True True True True
True True True True True True False True True True True True
False True False False True False]
# 5.2 输出准确率
ret = estimator.score(x_test,y_test)
print('准确率是:\n',ret)
准确率是:
0.7666666666666667
问题:那么这个只是对于参数得出更好的结果,那么怎么选择或者调优参数呢?
通常情况下,有很多参数时需要手动指定的(如K-邻近算法中的K值),这种叫超参数。但是手动过程繁杂,所以需要对模型预设几种超参数组合。
每组超参数都采用交叉验证来进行评估,最后选出最优参数组合模型
超参数:sklearn中需要手动指定的参数叫做超参数
网格搜索:就是把这些超参数的值,通过字典的形式传递进去,然后进行选择最优值。
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
estimator = KNeighborsClassifier(n_neighbors = 1)
# 调用交叉验证网格搜索模型
param_grid={'n_neighbors':[1,3,5]}
estimator = GridSearchCV(estimator,param_grid=param_grid,cv=10,n_jobs=-1)
estimator.fit(x_train,y_train)
GridSearchCV(cv=10, estimator=KNeighborsClassifier(n_neighbors=1), n_jobs=-1,
param_grid={'n_neighbors': [1, 3, 5]})
y_pre = estimator.predict(x_test)
print('预测值是:\n',y_pre)
print('预测值和真实值对比:\n',y_pre == y_test)
预测值是:
[0 2 2 2 1 0 0 1 2 0 0 0 2 1 2 2 1 2 2 2 0 1 2 0 0 1 0 0 1 0]
预测值和真实值对比:
[ True True True False True True True True True True True True
True True True True True True True True True True True True
True True True True True True]
ret = estimator.score(x_test,y_test)
print('准确率是:\n',ret)
准确率是:
0.9666666666666667
# 其他评价指标
print('最好的模型:\n',estimator.best_estimator_)
print('最好的结果:\n',estimator.best_score_)
print('整体的模型结果:\n',estimator.cv_results_)
最好的模型:
KNeighborsClassifier()
最好的结果:
0.9333333333333332
整体的模型结果:
{'mean_fit_time': array([0.00119724, 0.00099754, 0.00089726]), 'std_fit_time': array([3.99462801e-04, 1.78416128e-06, 2.99092610e-04]), 'mean_score_time': array([0.00189519, 0.00179517, 0.0013967 ]), 'std_score_time': array([0.00053735, 0.00059831, 0.00048877]), 'param_n_neighbors': masked_array(data=[1, 3, 5],
mask=[False, False, False],
fill_value='?',
dtype=object), 'params': [{'n_neighbors': 1}, {'n_neighbors': 3}, {'n_neighbors': 5}], 'split0_test_score': array([0.91666667, 0.91666667, 0.91666667]), 'split1_test_score': array([1., 1., 1.]), 'split2_test_score': array([0.66666667, 0.58333333, 0.66666667]), 'split3_test_score': array([0.91666667, 0.91666667, 0.91666667]), 'split4_test_score': array([0.91666667, 1. , 1. ]), 'split5_test_score': array([0.91666667, 0.91666667, 0.91666667]), 'split6_test_score': array([0.91666667, 1. , 1. ]), 'split7_test_score': array([1., 1., 1.]), 'split8_test_score': array([0.91666667, 0.91666667, 0.91666667]), 'split9_test_score': array([0.91666667, 1. , 1. ]), 'mean_test_score': array([0.90833333, 0.925 , 0.93333333]), 'std_test_score': array([0.08700255, 0.1204736 , 0.09718253]), 'rank_test_score': array([3, 2, 1])}