首页
视频
资源
登录
原
python 选择和训练模型(三)
1713
人阅读
2023/3/30 11:43
总访问:
2612895
评论:
0
收藏:
0
手机
分类:
AI
![](https://img.tnblog.net/arcimg/hb/55a6e3fdaa9846cb81829fd20288e216.jpg) >#python 选择和训练模型(三) [TOC] tn2>请根据上一篇结合。 训练和评估训练集 ------------ tn2>使用sklearn的LinearRegression(线性回归)、DecisionTreeRegressor(决策数)、 ### 线性回归 ![](https://img.tnblog.net/arcimg/hb/abe87d5259f74404af36cbf581d60713.png) tn2>首先添加两句话,在预测房价之前 ```python housing = strat_train_set.drop("median_house_value", axis=1) housing_labels = strat_train_set["median_house_value"].copy() ``` tn2>然后在末尾添加如下代码进行RMSE的计算。 ```python from sklearn.linear_model import LinearRegression # 创建实例 lin_reg = LinearRegression() # 训练 lin_reg.fit(housing_prepared, housing_labels) # 找训练集里面后5条数据 some_data = housing.iloc[:5] some_labels = housing_labels.iloc[:5] # 用模型转换一下 some_data_prepared = full_pipeline.transform(some_data) print("Predictions:",lin_reg.predict(some_data_prepared)) print("Labels:",list(some_labels)) ``` ![](https://img.tnblog.net/arcimg/hb/51be628eab85437e94ff37147bf19c04.png) tn2>我们可以看到预测值与实际值还是有一定的差距,RMSE还需要取平方根。 ```python from sklearn.metrics import mean_squared_error # 接收一个numpy数组 housing_predictions = lin_reg.predict(housing_prepared) # 计算均方误差 lin_mse = mean_squared_error(housing_labels,housing_predictions) # 计算均方根误差 lin_rmse = np.sqrt(lin_mse) lin_rmse ``` ![](https://img.tnblog.net/arcimg/hb/6e67215df37443b286cdb76610094886.png) tn2>误差6.8w。 ### 决策数 ```python from sklearn.tree import DecisionTreeRegressor tree_reg = DecisionTreeRegressor() tree_reg.fit(housing_prepared, housing_labels) housing_predictions = tree_reg.predict(housing_prepared) tree_mse = mean_squared_error(housing_labels,housing_predictions) tree_rmse = np.sqrt(tree_mse) tree_rmse ``` tn2>误差得出来是0 ![](https://img.tnblog.net/arcimg/hb/25d4b139c205461b89fa5c23c01b8448.png) ### 使用交叉验证来更好的评估 tn2>一个选择是sklearn的K-fold交叉验证功能。 原理:先将训练集分割成十个不同的子集,每一个子集分割成一个fold。然后通过决策树模型进行十次训练与评估,每次挑选一个进行评估,九个进行训练,产生的结果就是一个包含十次结果的数组。 ```python from sklearn.model_selection import cross_val_score scores = cross_val_score(tree_reg, housing_prepared, housing_labels,scoring="neg_mean_squared_error", cv=10) tree_rmse_scores = np.sqrt(-scores) # 帮助函数 def display_scores(scores): # 源数据 print("Scores:", scores) # 平均数 print("Mean:", scores.mean()) # 标准差 print("Standard deviation:", scores.std()) display_scores(tree_rmse_scores) ``` ![](https://img.tnblog.net/arcimg/hb/e19596f3d9c14d8496ba427a80df511a.png) tn2>通过它我们看到它比线性回归模型还要糟糕。 ### 随机森林回归 ```python from sklearn.ensemble import RandomForestRegressor forest_reg = RandomForestRegressor() forest_reg.fit(housing_prepared, housing_labels) housing_predictions = forest_reg.predict(housing_prepared) forest_mse = mean_squared_error(housing_labels, housing_predictions) forest_rmse = np.sqrt(forest_mse) forest_rmse ``` ![](https://img.tnblog.net/arcimg/hb/282540529d4f4390bdb725037166978a.png) ### 交叉验证 ```python scores = cross_val_score(forest_reg, housing_prepared, housing_labels,scoring="neg_mean_squared_error", cv=10) forest_rmse_scores = np.sqrt(-scores) display_scores(forest_rmse_scores) ``` ![](https://img.tnblog.net/arcimg/hb/cf3d9b8eafe843f7955b100f93f6b0e6.png) 调优模型 ------------ tn2>GirdSearchCV、随机搜索、集成方法、分析最佳模型和误差、使用测试集来评估系统。 ### 超参数Hyperparameter tn2>模型参数。例如 y = ax + b,通过训练得到的参数 超参数:需要人为设定。是开始学习过程之前设置值的参数,而不是通过训练得到的参数数据。 ### GridSearchCV tn2>可使用GridSearchCV来搜索超参数 你只需要告诉它你要进行实验的超参数是什么,以及需要尝试的值,它会使用交叉验证来评估超参数值的所有可能组合。 ### 随机搜索 tn2>如果超参数的搜索范围较大,可选择RandomizedSearchCV ### 集成方法 tn2>还有一种调优方法是将表现最优的模型组合起来,通常比单一模型好。 ### 使用测试集来评估系统 ```python X_test = strat_test_set.drop("median_house_value",axis=1) y_test = strat_test_set["median_house_value"].copy() X_test_prepared = full_pipeline.transform(X_test) # 随机森林 final_predictions = forest_reg.predict(X_test_prepared) final_mse = mean_squared_error(y_test, final_predictions) final_rmse = np.sqrt(final_mse) final_rmse ``` ![](https://img.tnblog.net/arcimg/hb/6785c3cd447a4ece85f038bb222b927d.png) 启动、监控、维护你的系统 ------------ tn2>将模型部署到生产环境:一种方法是保存训练好的模型(使用joblib),包括预处理和流水线,然后在生产环境中加载该模型,并调用prepare()方法来预测。
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
1篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术