机器学习(五) 鸢尾花案例调优
Python 机器学习
2018年3天快速入门python机器学习【黑马程序员】
机器学习(五) 鸢尾花案例调优
1.交叉验证及帮助我们选择最优的k值(在KNN算法调优中)
需要导入GirdSearchCV (网格,交叉)
# 加入网格搜索与交叉验证 # 参数准备 param_dict = {'n_neighbors': [1, 3, 5, 7, 9, 11]} estimator = GridSearchCV(estimator, param_grid=param_dict, cv=10) estimator.fit(x_train, y_train) 12345 3.2 输出 最佳参数和最佳结果等
# 最佳参数:best_params_ print("最佳参数:n", estimator.best_params_) # 最佳结果:best_score_ print("最佳结果:n", estimator.best_score_) # 最佳估计器:best_estimator_ print("最佳估计器:n", estimator.best_estimator_) # 交叉验证结果:cv_results_ #print("交叉验证结果:n", estimator.cv_results_) 12345678
最佳参数: {'n_neighbors': 3} 最佳结果: 0.9553030303030303 最佳估计器: KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=None, n_neighbors=3, p=2, weights='uniform') 123456789
最佳结果和准确率有一定的差别是因为划分出了验证集,在验证集中的结果
4.补充在调用的时候体现距离的算法
进入KNeighborsClassifier的源码
minkowski是明科夫斯基距离
下面是全部的代码
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 def knn_iris(): ''' 用KNN算法对鸢尾花进行分类 :return: ''' # 1) 获取数据 iris = load_iris() # 2) 划分数据集 x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=6) # 3) 特征工程 : 标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.transform(x_test) # 4) KNN算法预估器 estimator = KNeighborsClassifier(n_neighbors=3) estimator.fit(x_train, y_train) # 5) 模型评估 # 方法1: 直接比对真实值和预测值 y_predict = estimator.predict(x_test) print('y_predict:n', y_predict) print('直接比对真实值和预测值:n', y_test == y_predict) # 方法2: 计算准确率 score = estimator.score(x_test, y_test) print('准确率为:n', score) return None def knn_iris_gscv(): ''' 用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证 :return: ''' # 1) 获取数据 iris = load_iris() # 2) 划分数据集 x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22) # 3) 特征工程 : 标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.transform(x_test) # 4) KNN算法预估器 estimator = KNeighborsClassifier() # 加入网格搜索与交叉验证 # 参数准备 param_dict = {'n_neighbors': [1, 3, 5, 7, 9, 11]} estimator = GridSearchCV(estimator, param_grid=param_dict, cv=10) estimator.fit(x_train, y_train) # 5) 模型评估 # 方法1: 直接比对真实值和预测值 y_predict = estimator.predict(x_test) print('y_predict:n', y_predict) print('直接比对真实值和预测值:n', y_test == y_predict) # 方法2: 计算准确率 score = estimator.score(x_test, y_test) print('准确率为:n', score) # 最佳参数:best_params_ print("最佳参数:n", estimator.best_params_) # 最佳结果:best_score_ print("最佳结果:n", estimator.best_score_) # 最佳估计器:best_estimator_ print("最佳估计器:n", estimator.best_estimator_) # 交叉验证结果:cv_results_ print("交叉验证结果:n", estimator.cv_results_) return None if __name__ == '__main__': # 代码1:用KNN算法对鸢尾花进行分类 # knn_iris() # 代码2:用KNN算法对鸢尾花进行分类,添加网格搜索和交叉验证 knn_iris_gscv()
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283相关知识
机器学习案例——鸢尾花数据集分析
[Python机器学习]鸢尾花分类 机器学习应用
机器学习鸢尾花数据集
机器学习案例:鸢尾花分类——基于Scikit
[机器学习基础][笔记] 一、鸢尾花分类
【机器学习】鸢尾花分类
机器学习入门——鸢尾花问题
机器学习项目实战
机器学习算法
探索机器学习的起点:鸢尾花数据集
网址: 机器学习(五) 鸢尾花案例调优 https://www.huajiangbk.com/newsview768427.html
上一篇: cc2530:<3>ADC采集光 |
下一篇: 图像增强——传统算法伽马校正实现 |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039