OceaBase开发者大会落地上海!4月20日共同探索数据库前沿趋势!报名戳 了解详情
写点什么

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

公众号推荐:

2024 年 1 月,InfoQ 研究中心重磅发布《大语言模型综合能力测评报告 2024》,揭示了 10 个大模型在语义理解、文学创作、知识问答等领域的卓越表现。ChatGPT-4、文心一言等领先模型在编程、逻辑推理等方面展现出惊人的进步,预示着大模型将在 2024 年迎来更广泛的应用和创新。关注公众号「AI 前线」,回复「大模型报告」免费获取电子版研究报告。

AI 前线公众号
2021-05-20 16:012103

评论

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

分布式缓存、消息系统和异步架构

架构5班杨娟Jessie

极客大学架构师训练营

week2作业

「深度解析」AI训练之数据缓存

焱融科技

人工智能 AI 存储 焱融科技 数据缓存

架构师训练营 - 第 5 课总结 -20200704- 技术选型

👑👑merlan

负载均衡 缓存 分布式数据库 架构设计 消息队列

架构师训练营第五周课后总结

Cloud.

就餐卡系统设计

springBoot集成rabbitmq并实现延时队列

生命在于折腾

RabbitMQ

一致性哈希算法实现

老姜

week05 学习总结 分布式缓存&消息队列&负载

Z冰红茶

一致性hash的理解与实现

dongge

Week 05 命题作业

卧石漾溪

极客大学架构师训练营

Week 5 作业

Shawn

使用@AutoConfigureBefore调整配置顺序竟没生效?

YourBatman

Java Spring Boot @AutoConfigureBefore

架构师训练营第五章作业

叮叮董董

小师妹学JVM之:cache line对代码性能的影响

程序那些事

JVM 小师妹 性能调优 cache line 签约计划第二季

啃碎并发(二):Java线程的生命周期

猿灯塔

超详细!一文带你了解 LVS 负载均衡集群!

JackTian

Linux 负载均衡 运维 LVS 服务器集群

架构师第5周总结

老姜

Java 线程池中的线程复用是如何实现的?

武培轩

Java 程序员 后端 线程池 源码解析

用进废退,增加能力熟练度与经验值,让你的技能再次精进。

叶小鍵

一次非常有意思的 SQL 优化经历: 从 30248.271s 到 0.001s

Java小咖秀

MySQL 面试 后端 经验分享 优化逻辑

游戏夜读 | 关卡设计的难点

game1night

第五周作业

Linuxer

极客大学架构师训练营

一致性Hash算法以及Java代码实现

架构5班杨娟Jessie

极客大学架构师训练营

架构师训练营第五章总结

叮叮董董

一致性 hash 算法

Z冰红茶

一致性Hash算法

【Python】__name__ 是什么?

Leetao

Python Python基础

联想来酷广谱化生存:后疫情时代的"硬核品牌"启示录

Geek_116789

数据分析师成长体系漫谈--数据埋点

analysis-lion

数据分析 数据采集 埋点

Ceph数据恢复初探

焱融科技

焱融科技 文件存储 分布式存储 数据恢复 Ceph

干货 | 如何评估Kubernetes持久化存储方案

焱融科技

Kubernetes 容器 云原生 k8s

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