
本文,我们将通过 Python 语言包,来构建一些机器学习模型。
构建机器学习模型的模板
该 Notebook 包含了用于创建主要机器学习算法所需的代码模板。在 scikit-learn 中,我们已经准备好了几个算法。只需调整参数,给它们输入数据,进行训练,生成模型,最后进行预测。
1.线性回归
对于线性回归,我们需要从 sklearn 库中导入 linear_model。我们准备好训练和测试数据,然后将预测模型实例化为一个名为线性回归 LinearRegression 算法的对象,它是 linear_model 包的一个类,从而创建预测模型。之后我们利用拟合函数对算法进行训练,并利用得分来评估模型。最后,我们将系数打印出来,用模型进行新的预测。
# Import modules
from sklearn import linear_model
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_train = train_dataset_predicted_variable
x_test = test_dataset_precictor_variables
# Create linear regression object
linear = linear_model.LinearRegression()
# Train the model with training data and check the score
linear.fit(x_train, y_train)
linear.score(x_train, y_train)
# Collect coefficients
print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
# Make predictions
predicted_values = linear.predict(x_test)
2.逻辑回归
在本例中,从线性回归到逻辑回归唯一改变的是我们要使用的算法。我们将 LinearRegression 改为 LogisticRegression。
# Import modules
from sklearn.linear_model import LogisticRegression
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_train = train_dataset_predicted_variable
x_test = test_dataset_precictor_variables
# Create logistic regression object
model = LogisticRegression()
# Train the model with training data and checking the score
model.fit(x_train, y_train)
model.score(x_train, y_train)
# Collect coefficients
print('Coefficient: \n', model.coef_)
print('Intercept: \n', model.intercept_)
# Make predictions
predicted_vaues = model.predict(x_teste)
3.决策树
我们再次将算法更改为 DecisionTreeRegressor:
# Import modules
from sklearn import tree
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_train = train_dataset_predicted_variable
x_test = test_dataset_precictor_variables
# Create Decision Tree Regressor Object
model = tree.DecisionTreeRegressor()
# Create Decision Tree Classifier Object
model = tree.DecisionTreeClassifier()
# Train the model with training data and checking the score
model.fit(x_train, y_train)
model.score(x_train, y_train)
# Make predictions
predicted_values = model.predict(x_test)
4.朴素贝叶斯
我们再次将算法更改为 DecisionTreeRegressor:
# Import modules
from sklearn.naive_bayes import GaussianNB
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Create GaussianNB object
model = GaussianNB()
# Train the model with training data
model.fit(x_train, y_train)
# Make predictions
predicted_values = model.predict(x_test)
5.支持向量机
在本例中,我们使用 SVM 库的 SVC 类。如果是 SVR,它就是一个回归函数:
# Import modules
from sklearn import svm
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_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 score
model.fit(x_train, y_train)
model.score(x_train, y_train)
# Make predictions
predicted_values = model.predict(x_test)
6.K- 最近邻
在 KneighborsClassifier 算法中,我们有一个超参数叫做 n_neighbors,就是我们对这个算法进行调整。
# Import modules
from sklearn.neighbors import KNeighborsClassifier
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_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 data
model.fit(x_train, y_train)
# Make predictions
predicted_values = model.predict(x_test)
7.K- 均值
# Import modules
from sklearn.cluster import KMeans
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_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 data
model.fit(x_train)
# Make predictions
predicted_values = model.predict(x_test)
8.随机森林
# Import modules
from sklearn.ensemble import RandomForestClassifier
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_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 predictions
predicted_values = model.predict(x_test)
9.降维
# Import modules
from sklearn import decomposition
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Creating PCA decomposition object
pca = decomposition.PCA(n_components = k)
# Creating Factor analysis decomposition object
fa = decomposition.FactorAnalysis()
# Reduc the size of the training set using PCA
reduced_train = pca.fit_transform(train)
# Reduce the size of the training set using PCA
reduced_test = pca.transform(test)
10.梯度提升和 AdaBoost
# Import modules
from sklearn.ensemble import GradientBoostingClassifier
# Create training and test subsets
x_train = train_dataset_predictor_variables
y_train = train_dataset_predicted variable
x_test = test_dataset_precictor_variables
# Creating Gradient Boosting Classifier object
model = 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 predictions
predicted_values = model.predict(x_test)
我们的工作将是把这些算法中的每一个块转化为一个项目。首先,定义一个业务问题,对数据进行预处理,训练算法,调整超参数,获得可验证的结果,在这个过程中不断迭代,直到我们达到满意的精度,做出理想的预测。
原文链接:
更多内容推荐
00. 发刊词
2023-10-17
22. 项目工程搭建
2023-09-30
从人工智能算法聊开去 |InfoQ《极客有约》
你喜爱的短视频、音乐App的“猜你喜欢”……我们对算法的依赖已经超出想象,但你真的了解算法吗?
vivo 故障定位平台的探索与实践
本文基于故障定位项目的实践,围绕根因定位算法的原理进行展开介绍。
2023-01-09
20.OpenFeign 工程案例搭建
2023-09-29
拯救你的算法!GitHub 上神仙项目手把手带你刷算法,Star 数已破 110k
前不久在 GitHub 出现了一个手把手带你刷 算法的项目:fucking-algorithm。该项目此前在 GitHub 开源后,连续多次霸榜 GitHub Trending 首页,用了一周Star数便突破 110k,受欢迎程度由此可见一斑:
2021-09-14
算法 ---- 字符串
StringBuilder sb = new StringBuilder();for (String aStrArr : strAtr) {sb.append(aStrArr);}String result = sb.toString();if ('0' == result.charAt(0)) {result = "0";}return result;}}
2021-11-07
5. 字符串模式匹配 KMP 算法
2023-09-27
12. 结果集处理:Open 与 OpenDB
2023-09-26
2022 年顶级机器学习算法和 Python 库
这些算法之所以与众不同,是因为它们包含了一些在其它算法中并不普遍的优点。
时间复杂度和空间复杂度
算法,即解决问题的方法。同一个问题,使用不同的算法,虽然得到的结果相同,但是耗费的时间和资源是不同的。就比如要拧一个螺母,使用扳手还是钳子是有区别的,虽然使用钳子也能拧螺母,但是没有扳手好用。“条条大路通罗马”,解决问题的算法有多种,这就需
2022-12-12
算法题每日一练 --- 第 12 天:算式 900
小明的作业本上有道思考题:
2022-07-28
算法题每日一练:矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地算法 。
2023-05-01
PGL 图学习之图神经网络 ERNIESage、UniMP 进阶模型 [系列八]
PGL图学习之图神经网络ERNIESage、UniMP进阶模型[系列八]
2022-11-26
算法题每日一练: 青蛙跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n (0 <= n <= 100)级的台阶总共有多少种跳法。
2023-04-26
【刷题记录】15. 三数之和
重温算法 - 三数之和
2022-07-19
学术论坛第七期:基于统计的预测算法
学术论坛第七期:基于统计的预测算法 本次学术论坛我们邀请了云智慧算法实习生、北京科技大学硕士在读生朱同学为我们简要介绍几类轻量级的统计预测算法模型,其中包括ARMA模型、ARCH模型、HoltWinters模型、facebook提出的prophet模型以及树模型。
2022-02-18
3.2 面试技巧和面试题精讲
2023-09-27
看完了阿里大牛的 Leetcode 刷题笔记, 我成功拿到了字节跳动的 offer
关于算法刷题的困惑和疑问也经常听朋友们提及,不管是找工作笔试面试白板试进大厂,还是研究生参加初试复试机试,数据结构和算法都是绕不过去的坎,刷题就成了很多人的需求,今天推荐两个大佬的算法刷题笔记,让你毫不费力的搞定算法、拿下大厂offer !
2021-11-25
推荐阅读
大模型训练:数据与算法的关键融合
2023-10-17
SPFA 算法:实现原理及其应用
2023-05-04
直播回放|AI 绘画发展脉络与 LoRA 模型训练实战
2023-11-23
CART 算法解密:从原理到 Python 实现
2023-11-24
27|模型工程(三):低成本领域模型方案,小团队怎么做大模型?
2023-10-20
强化学习从基础到进阶 - 常见问题和面试必知必答 [8]:近端策略优化(proximal policy optimization,PPO)算法
2023-06-28
14. 点检制的“八定”
2023-10-17
电子书

大厂实战PPT下载
换一换 
邓艳琴(Clara) | 极客邦科技 会议主编
向阳 | 云杉网络 研发 VP
马学宁 | 国投瑞银基金管理有限公司 信息技术部副总监/首席架构师
评论