写点什么

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

评论

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

如何开发引入小程序插件

Geek_99967b

小程序插件

企业数字化转型之路,从这里开始

天翼云开发者社区

数字化转型 云存储

7000+字图文并茂解带你深入理解java锁升级的每个细节

华为云开发者联盟

Java 开发 华为云

systemd-resolved 开启 debug 日志

程序员与厨子

ubuntu 运维 DNS systemd-resolved

华为云ModelArts文本分类–外卖评论

逝缘~

深度学习 华为云 7月月更

微服务链路风险分析

阿泽🧸

7月月更 链路风险分析

一朵云开启智慧交通新未来

天翼云开发者社区

区块链 大数据 物联网

一文读懂简单查询代价估算

华为云开发者联盟

数据库 后端 查询引擎

Ubuntu 20.04 安装 Chisel

贾献华

7月月更

集合处理的利器

技术小生

java8 7月月更

不要再手动批量替换了,使用python AST模块批量替换

阿呆

Python AST 批量替换

分布式算法入门之 Paxos 算法

宇宙之一粟

Basic paxos 7月月更

XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西

雨果

云服务 xaas DaaS 本地服务

Spring你牛个啥,我承认刚才说话我声音有点大

zxhtom

7月月更

牛客java选择题每日打卡Day7

京与旧铺

7月月更

MMAP

北洋

Andriod 7月月更

使用 RepositoryProvider简化父子组件的传值

岛上码农

flutter ios 安卓 移动端开发 7月月更

【刷题记录】1. 两数之和

WangNing

7月月更

场景化面试:关于分布式锁的十问十答

面试官问

分布式锁

开创人工智能产业新未来!7月8日昇思生态论坛与你相约广州

科技热闻

AI金榜题名时,MLPerf榜单的份量究竟有多重?

脑极体

国内低代码开发平台靠谱的都有哪些?

AIRIOT

低代码 物联网 低代码,项目开发

【愚公系列】2022年7月 Go教学课程 004-Go代码注释

愚公搬代码

7月月更

让开发效率飞速提升的跨端方案

Geek_99967b

小程序 跨端 小程序容器

如何组织一场实战攻防演练

穿过生命散发芬芳

攻防演练 7月月更

Java方向~~0基础小白如何快速脱离0offer的苦海!

KEY.L

7月月更

从 1.5 开始搭建一个微服务框架——调用链追踪 traceId

悟空聊架构

日志 链路追踪 traceId 悟空聊架构 7月月更

鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!

天翼云开发者社区

服务器 弹性扩容

中文版Postman?功能真心强大!

Liam

Java 开发者工具 Postman 后端开发 程序员进阶

刷个算法,结果第一题就蚌埠住了~~

为自己带盐

算法 力扣 7月月更

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