写点什么

如何用 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:012800

评论

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

最新版LangChain4j发布!终于修复了这个恶心的问题

王磊

DApp开发:开启去中心化应用新时代

区块链软件开发推广运营

交易所开发 dapp开发 链游开发 公链开发 代币开发

YashanDB 知识库|如何判断表空间是否启用了加密?

数据库砖家

数据库

探索如何提升自动化测试的效率--代码评审和脚本生成

夏兮。

AI 自动化测试 端到端测试新范式

《算法导论(第4版)》阅读笔记:p11-p13

codists

算法

英特尔持续推进核心制程和先进封装技术创新,分享最新进展

E科讯

YashanDB|崖山共享集群(YAC):从怀疑到认可,技术细节决定产品成色

数据库砖家

数据库

YashanDB|select 0.00 的返回类型居然变了?警惕 JDBC 下的类型映射差异!

数据库砖家

数据库

YashanDB 知识库|MySQL 的 GROUP BY 语句迁移到崖山时报错?原来是这个规范不同!

数据库砖家

数据库

区块链内容创作全攻略:海报、白皮书与视频的视觉化革命

区块链软件开发推广运营

交易所开发 dapp开发 链游开发 公链开发 代币开发

Java 字符串拼接性能实测:基于 JMH 的微基准测试

歆晨技术笔记

智源研究院发布开源中文互联网语料库CCI 4.0 新增高质量英文数据与合成数据

智源研究院

Mint Blockchain 与 Chainlink 达成战略合作:赋能跨链创新

NFT Research

blockchain web3

Arthas jad(字节码文件反编译成源代码 )

刘大猫

监控 Arthas 监控工具 jad 字节码文件

自研时序大模型讲解(4月29日)直播回顾

Apache IoTDB

黑龙江密码测评你需要知道的基本知识

黑龙江陆陆信息测评部

LED虚拟生产:革新影视制作的新技术

Dylan

虚拟 LED LED display LED显示屏 LED屏幕

DeFi开发系统软件开发:技术架构与生态重构

区块链软件开发推广运营

交易所开发 dapp开发 链游开发 公链开发 代币开发

YashanDB|使用 Kettle 同步 PostgreSQL 数据时报错:pg_hba.conf 无法识别连接?

数据库砖家

数据库

AI 大赛丨总奖池 50 万元!「1000 AIdea 应用计划」等你来战!

声网

【AI】DeepWiki 页面转换成 Markdown 保存 - Chrome 扩展

非晓为骁

chrome AI DeepWiki Extension

HTTP接口数据也能定时同步入湖?用DolphinScheduler×SeaTunnel快速搞定!

白鲸开源

大数据 开源 Apache DolphinScheduler 任务调度

英特尔以系统级代工模式促进生态协同,助力客户创新

E科讯

YashanDB|UNDO 表空间持续增长怎么处理?一文教你排查+优化

数据库砖家

数据库

公链钱包开发:技术逻辑与产品设计实践

区块链软件开发推广运营

交易所开发 dapp开发 链游开发 代币开发 交易所开发公链开发

智源研究院发布开源中文互联网语料库CCI 4.0 新增高质量英文数据与合成数据

智源研究院

YashanDB 知识库|MySQL 迁移后 CHAR 字段多出空格?问题可能出在这里

数据库砖家

数据库

YashanDB|Kettle 同步大表报错 “无法创建 Java 虚拟机”?别忘了调整内存

数据库砖家

数据库

提升研发运维效能:Pacvue 泊客电商的 GenAI 技术实践

亚马逊云科技 (Amazon Web Services)

jd 商品评论Api接口 全解析指南

代码忍者

个人评论 JD

当AI智能体通过稳定币自主交易和结算,如何利好DeCloud?

PowerVerse

AI 加密 去中心化 稳定币 DeCloud

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