Evernote Export
body,td { font-family: 微软雅黑; font-size: 10pt }
机器学习的运行步骤
1.导入数据
没什么注意的,成功导入数据集就可以了,打印看下数据的标准格式就行
用个info和describe
2.分析数据
这里要详细分析数据的内容,看看缺省值和数据的特征,主要是为了看到数据的特征,并且人肉分析一下特征值对目标值的大约影响,嗯,就是这样
然后开始划分数据,将数据分为两个部分,一个数据的特征值(features),一个是数据的目标值(target)
这里要用到数据的基本操作,有数据清洗和数据整理等内容。
重点:数据的分割,这里就需要将数据集(如果只有一个数据集)分割为两个部分,一个是训练集,一个是测试集
这里使用的是
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features,prices,test_size=0.2,random_state=10)
3.模型衡量标准
这里稍微有点懵,因为这一步并不会在接下来马上使用,而是在过程中,你需要评估的一种方式,我这里把这种方式应该是作为一个模块化编程的方式来学习的,而不是部分交互式编程的方式
- 使用 sklearn.metrics 中的 r2_score 来计算 y_true 和 y_predict的R2值,作为对其表现的评判。
- 将他们的表现评分储存到score变量中。
设立模型的衡量标准主要是为了确定你的模型拟合程度
def performance_metric(y_true, y_predict):
"""计算并返回预测值相比于预测值的分数"""
from sklearn.metrics import r2_score
score = r2_score(y_true, y_predict)
return score
4.分析模型表现
这里应该是有对算法的模型选型的过程,这里跳过的原因是因为这一章我们是站在已经有数据模型的方向去考虑问题的,所以跳过了模型的选型和测试的环节,实际上这里应该是最花时间的地方,需要你调参的地方也会很多。
它们是一个决策树模型在不同最大深度下的表现。每一条曲线都直观得显示了随着训练数据量的增加,模型学习曲线的在训练集评分和验证集评分的变化,评分使用决定系数R2。曲线的阴影区域代表的是该曲线的不确定性(用标准差衡量)
5.选择最优参数
选择最优参数的时候也是在已经确定了模型以后的简单调参了,就是选择了决策树的最大深度
6.做出预测
没什么说的,就是带入新的数据进行模拟运算
%23%23%23%20%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E7%9A%84%E8%BF%90%E8%A1%8C%E6%AD%A5%E9%AA%A4%0A%23%23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%86%E6%95%B0%E6%8D%AE%E5%88%86%E4%B8%BA%E4%B8%A4%E4%B8%AA%E9%83%A8%E5%88%86%EF%BC%8C%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E7%9A%84%E7%89%B9%E5%BE%81%E5%80%BC(features)%EF%BC%8C%E4%B8%80%E4%B8%AA%E6%98%AF%E6%95%B0%E6%8D%AE%E7%9A%84%E7%9B%AE%E6%A0%87%E5%80%BC(target)%0A%E8%BF%99%E9%87%8C%E8%A6%81%E7%94%A8%E5%88%B0%E6%95%B0%E6%8D%AE%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%EF%BC%8C%E6%9C%89%E6%95%B0%E6%8D%AE%E6%B8%85%E6%B4%97%E5%92%8C%E6%95%B0%E6%8D%AE%E6%95%B4%E7%90%86%E7%AD%89%E5%86%85%E5%AE%B9%E3%80%82%0A**%E9%87%8D%E7%82%B9%EF%BC%9A%E6%95%B0%E6%8D%AE%E7%9A%84%E5%88%86%E5%89%B2%EF%BC%8C%E8%BF%99%E9%87%8C%E5%B0%B1%E9%9C%80%E8%A6%81%E5%B0%86%E6%95%B0%E6%8D%AE%E9%9B%86(%E5%A6%82%E6%9E%9C%E5%8F%AA%E6%9C%89%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E9%9B%86)%E5%88%86%E5%89%B2%E4%B8%BA%E4%B8%A4%E4%B8%AA%E9%83%A8%E5%88%86%EF%BC%8C%E4%B8%80%E4%B8%AA%E6%98%AF%E8%AE%AD%E7%BB%83%E9%9B%86%EF%BC%8C%E4%B8%80%E4%B8%AA%E6%98%AF%E6%B5%8B%E8%AF%95%E9%9B%86**%0A%3E%E8%BF%99%E9%87%8C%E4%BD%BF%E7%94%A8%E7%9A%84%E6%98%AF%0A%60%60%60python%0Afrom%20sklearn.model_selection%20import%20train_test_split%0AX_train%2C%20X_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(features%2Cprices%2Ctest_size%3D0.2%2Crand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9A%84%E8%AF%84%E5%88%A4%E3%80%82%0A*%20%E5%B0%86%E4%BB%96%E4%BB%AC%E7%9A%84%E8%A1%A8%E7%8E%B0%E8%AF%84%E5%88%86%E5%82%A8%E5%AD%98%E5%88%B0score%E5%8F%98%E9%87%8F%E4%B8%AD%E3%80%82%0A**%E8%AE%BE%E7%AB%8B%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%A1%A1%E9%87%8F%E6%A0%87%E5%87%86%E4%B8%BB%E8%A6%81%E6%98%AF%E4%B8%BA%E4%BA%86%E7%A1%AE%E5%AE%9A%E4%BD%A0%E7%9A%84%E6%A8%A1%E5%9E%8B%E6%8B%9F%E5%90%88%E7%A8%8B%E5%BA%A6**%0A%60%60%60python%0Adef%20performance_metric(y_true%2C%20y_predict)%3A%0A%20%20%20%20%22%22%22%E8%AE%A1%E7%AE%97%E5%B9%B6%E8%BF%94%E5%9B%9E%E9%A2%84%E6%B5%8B%E5%80%BC%E7%9B%B8%E6%AF%94%E4%BA%8E%E9%A2%84%E6%B5%8B%E5%80%BC%E7%9A%84%E5%88%86%E6%95%B0%22%22%22%0A%20%20%20%20from%20%20sklearn.metrics%20import%20r2_score%0A%20%20%20%20score%20%3D%20r2_score(y_true%2C%20y_predict)%0A%0A%20%20%20%20return%20score%0A%60%60%60%0A%23%23%23%23%204.%E5%88%86%E6%9E%90%E6%A8%A1%E5%9E%8B%E8%A1%A8%E7%8E%B0%0A%E8%BF%99%E9%87%8C%E5%BA%94%E8%AF%A5%E6%98%AF%E6%9C%89%E5%AF%B9%E7%AE%97%E6%B3%95%E7%9A%84%E6%A8%A1%E5%9E%8B%E9%80%89%E5%9E%8B%E7%9A%84%E8%BF%87%E7%A8%8B%EF%BC%8C%E8%BF%99%E9%87%8C%E8%B7%B3%E8%BF%87%E7%9A%84%E5%8E%9F%E5%9B%A0%E6%98%AF%E5%9B%A0%E4%B8%BA%E8%BF%99%E4%B8%80%E7%AB%A0%E6%88%91%E4%BB%AC%E6%98%AF%E7%A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%E7%AE%97
原文地址:https://www.cnblogs.com/pandaboy1123/p/10240196.html