写点什么

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

评论

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

5.图学习【参考资料2】-知识补充与node2vec代码注解

汀丶人工智能

图神经网络 11月月更

引迈信息低代码怎么样?靠谱吗?

优秀

低代码 低代码平台

阿里 CTO 程立:今年双 11,全面深度用云

云布道师

云计算 阿里巴巴 天猫

龙蜥理事长马涛荣获 “2022 年度开源人物”

OpenAnolis小助手

开源 操作系统 龙蜥社区 理事长 2022云栖大会

vue实战-完全掌握Vue自定义指令

yyds2026

Vue

解读数仓常用模糊查询的优化方法

华为云开发者联盟

数据库 后端 华为云

[力扣] 剑指 Offer 第一天 - 包含min函数的栈

陈明勇

Go 数据结构与算法 力扣 11月月更

质量评估模型助力风险决策水平提升

百度Geek说

机器学习 企业号十月 PK 榜 智能测试 质量评估模型

可防离职员工冒用身份,合合信息名片全能王与钉钉用数字名片打造安全“围栏”

合合技术团队

人工智能 大数据 钉钉 合合信息 名片

启科量子 QuSprout 或将启动开源计划

启科量子开发者官方号

人工智能 框架 算力 超算 #量子计算

经常被问到的react-router实现原理详解

夏天的味道123

React

技术分享| Etcd如何实现分布式负载均衡及分布式通知与协调

anyRTC开发者

分布式 etcd 通知 式负载均衡 协调

字节跳动基于ClickHouse优化实践之“资源隔离”

字节跳动数据平台

大数据 Clickhouse

传统 Web 框架部署与迁移

阿里巴巴云原生

阿里云 Serverless 云原生

一汽集团数字化转型细节分析:明确如何转型事半功倍

雨果

数字化转型

Apache EventMesh事件驱动分布式运行时

EventMesh布道师

Serverless Faas EDA workflow eventmesh

商业智能工具BI口碑解读:Quick BI为何连续入选魔力象限?

夏日星河

软件测试丨测试大咖漫谈如何搞定软件质量?

测试人

软件测试 软件质量 自动化测试 测试开发

Java Web(十)Filter和Listener

浅辄

javaWeb filter listener 11月月更

详细解读 React useCallback & useMemo

夏天的味道123

React

国产数据库肇始之独具特色的场景需求

亚信AntDB数据库

数据库 AntDB 国产数据库 AntDB数据库

OpenHarmony集成OCR三方库实现文字提取

OpenHarmony开发者

OpenHarmony

李白:你的模型权重很不错,可惜被我没收了

OneFlow

人工智能 深度学习 模型

会用postman不算牛,会用Eolink才是真的牛

陈橘又青

API

wallys-WiFi-5-outdoor-Access-point-IPQ4019/4029-industrial wireless AP

Cindy-wallys

IPQ4019 ipq4029

大麦 Android 选座场景性能优化全解析

阿里巴巴终端技术

android 性能优化 客户端

CANN 6.0来了,硬核技术抢先看

华为云开发者联盟

人工智能 华为云 昇腾 CANN 6.0

如何使用ModelBox快速提升AI应用性能

华为云开发者联盟

人工智能 华为云 ModelBox

数据中台选型必读(五):中台建设本质就是构建企业的公共数据层

雨果

数据中台

详解React的Transition工作原理原理

夏天的味道123

React

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