写点什么

如何用 Python 构建机器学习模型?

  • 2021-05-20
  • 本文字数:3137 字

    阅读完需:约 10 分钟

如何用Python构建机器学习模型?

本文,我们将通过 Python 语言包,来构建一些机器学习模型。

构建机器学习模型的模板


该 Notebook 包含了用于创建主要机器学习算法所需的代码模板。在 scikit-learn 中,我们已经准备好了几个算法。只需调整参数,给它们输入数据,进行训练,生成模型,最后进行预测。

1.线性回归


对于线性回归,我们需要从 sklearn 库中导入 linear_model。我们准备好训练和测试数据,然后将预测模型实例化为一个名为线性回归 LinearRegression 算法的对象,它是 linear_model 包的一个类,从而创建预测模型。之后我们利用拟合函数对算法进行训练,并利用得分来评估模型。最后,我们将系数打印出来,用模型进行新的预测。


# Import modulesfrom sklearn import linear_model
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted_variable
x_test = test_dataset_precictor_variables
# Create linear regression objectlinear = linear_model.LinearRegression()
# Train the model with training data and check the scorelinear.fit(x_train, y_train)linear.score(x_train, y_train)
# Collect coefficientsprint('Coefficient: \n', linear.coef_)print('Intercept: \n', linear.intercept_)
# Make predictionspredicted_values = linear.predict(x_test)
复制代码

2.逻辑回归


在本例中,从线性回归到逻辑回归唯一改变的是我们要使用的算法。我们将 LinearRegression 改为 LogisticRegression。


# Import modulesfrom sklearn.linear_model import LogisticRegression
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted_variable
x_test = test_dataset_precictor_variables
# Create logistic regression objectmodel = LogisticRegression()
# Train the model with training data and checking the scoremodel.fit(x_train, y_train)model.score(x_train, y_train)
# Collect coefficientsprint('Coefficient: \n', model.coef_)print('Intercept: \n', model.intercept_)
# Make predictionspredicted_vaues = model.predict(x_teste)
复制代码


3.决策树


我们再次将算法更改为 DecisionTreeRegressor:


# Import modulesfrom sklearn import tree
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted_variable
x_test = test_dataset_precictor_variables
# Create Decision Tree Regressor Objectmodel = tree.DecisionTreeRegressor()
# Create Decision Tree Classifier Objectmodel = tree.DecisionTreeClassifier()
# Train the model with training data and checking the scoremodel.fit(x_train, y_train)model.score(x_train, y_train)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


4.朴素贝叶斯


我们再次将算法更改为 DecisionTreeRegressor:


# Import modulesfrom sklearn.naive_bayes import GaussianNB
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Create GaussianNB objectmodel = GaussianNB()
# Train the model with training data model.fit(x_train, y_train)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


5.支持向量机


在本例中,我们使用 SVM 库的 SVC 类。如果是 SVR,它就是一个回归函数:


# Import modulesfrom sklearn import svm
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Create SVM Classifier object model = svm.svc()
# Train the model with training data and checking the scoremodel.fit(x_train, y_train)model.score(x_train, y_train)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


6.K- 最近邻


在 KneighborsClassifier 算法中,我们有一个超参数叫做 n_neighbors,就是我们对这个算法进行调整。


# Import modulesfrom sklearn.neighbors import KNeighborsClassifier
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Create KNeighbors Classifier Objects KNeighborsClassifier(n_neighbors = 6) # default value = 5
# Train the model with training datamodel.fit(x_train, y_train)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


7.K- 均值


# Import modulesfrom sklearn.cluster import KMeans
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Create KMeans objects k_means = KMeans(n_clusters = 3, random_state = 0)
# Train the model with training datamodel.fit(x_train)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


8.随机森林


# Import modulesfrom sklearn.ensemble import RandomForestClassifier
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Create Random Forest Classifier objects model = RandomForestClassifier()
# Train the model with training data model.fit(x_train, x_test)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


9.降维


# Import modulesfrom sklearn import decomposition
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Creating PCA decomposition objectpca = decomposition.PCA(n_components = k)
# Creating Factor analysis decomposition objectfa = decomposition.FactorAnalysis()
# Reduc the size of the training set using PCAreduced_train = pca.fit_transform(train)
# Reduce the size of the training set using PCAreduced_test = pca.transform(test)
复制代码


10.梯度提升和 AdaBoost


# Import modulesfrom sklearn.ensemble import GradientBoostingClassifier
# Create training and test subsetsx_train = train_dataset_predictor_variablesy_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Creating Gradient Boosting Classifier objectmodel = GradientBoostingClassifier(n_estimators = 100, learning_rate = 1.0, max_depth = 1, random_state = 0)
# Training the model with training data model.fit(x_train, x_test)
# Make predictionspredicted_values = model.predict(x_test)
复制代码


我们的工作将是把这些算法中的每一个块转化为一个项目。首先,定义一个业务问题,对数据进行预处理,训练算法,调整超参数,获得可验证的结果,在这个过程中不断迭代,直到我们达到满意的精度,做出理想的预测。


原文链接:


https://levelup.gitconnected.com/10-templates-for-building-machine-learning-models-with-notebook-282c4eb0987f

2021-05-20 16:012589

评论

发布
暂无评论
发现更多内容

Go 并发编程-goroutine 初体验

Rayjun

Go 语言 goroutine

找出 SAP OData service出错根源的小技巧

汪子熙

SAP Fiori SAP UI5 OData

利用Chrome的Heap Snapshot功能分析一个时间段内的内存占用率

汪子熙

JavaScript chrome

高性能 JavaScriptの五 -- 快响应用户界面

空城机

JavaScript 大前端 5月日更

如何将BSP应用配置成Fiori Launchpad上的一个tile

汪子熙

SAP abap Fiori SAP UI5 bsp

自我复盘

lenka

5月日更

一个查看 SAP UI5 控件所有公有方法的小技巧

汪子熙

JavaScript SAP SAP UI5

Shell脚本-简单爬虫

追风的少年

再谈前端性能监控及4个最佳工具分享

devpoint

大前端 sentry

C4C Cloud Application Studio做ABSL开发的一些性能方面的最佳实践

汪子熙

Cloud CRM SAP C4C

Authorization object在哪些ABAP代码里使用到

汪子熙

CRM SAP abap Netweaver

架构实战营-作业四

大可

基于Mac的手动搭建WordPress个人站点的方法

三掌柜

5月日更

☕【Java技术之旅】从底层角度去认识线程的原理

洛神灬殇

Java 线程 Thread 线程协作 5月日更

模块四-千万级学生管理系统试卷存储方案

华仔架构训练营

MySQL主从网络延迟解决方案

运维研习社

MySQL 运维 主从同步 5月日更

和另一半过不一样的520

小英

520单身福利 520 单身福利

Python 持久化 - 文件

若尘

文件 持久化 持久化存储 5月日更

设计千万级学生管理系统的考试试卷存储方案

9527

🚄【Redis 干货领域】帮你完全搞定 Cluster 原理(实践篇)

洛神灬殇

redis redis集群 redis cluster 5月日更 redis架构

架构实战营模块4作业

Vic

架构实战营

Inner Join, Left Outer Join和Association的区别

汪子熙

SAP abap ST05

SAP Fiori Launchpad Tile点击后跳转的调试技巧

汪子熙

JavaScript SAP Fiori SAP UI5

Go 并发编程 — 深入浅出 sync.Pool ,围观最全的使用姿势,理解最深刻的原理

奇伢云存储

并发编程 云存储 Go 语言

开发中Docker常用容器记录

Docker

SAP Fiori 应用 Footerbar 区域按钮的高亮显示逻辑

汪子熙

JavaScript SAP Fiori SAP UI5

SAP 不同 ABAP 系统里同一 Customizing activity 的显示差异分析

汪子熙

CRM SAP ERP abap 定制化

到底哪种类型的错误信息会阻止business transaction的保存

汪子熙

CRM SAP abap

另一种方式实现事务码SE16里的结果集修改

汪子熙

JavaScript SAP Fiori

如何根据日志来了解一个请求经历了什么?

我爱娃哈哈😍

软件架构 架构设计 架构实战

如何在SAP CRM WebClient UI里创建HANA Live Report

汪子熙

CRM SAP WebClient UI

如何用Python构建机器学习模型?_AI&大模型_Anello_InfoQ精选文章