一、机器学习入门——简单线性回归 (柑橘与柠檬啊
项目链接:https://www.heywhale.com/mw/project/5a66c1cd75dc1d5eca320c48
#加载库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#建立数据集
from collections import OrderedDict
examDict={
'学习时间':[0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,
2.50,2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50],
'分数': [10, 22, 13, 43, 20, 22, 33, 50, 62,
48, 55, 75, 62, 73, 81, 76, 64, 82, 90, 93]
}
examOrderDict=OrderedDict(examDict)
exam=pd.DataFrame(examOrderDict)
-
collections
Python collections 包提供了好多集合模块 -
OrderedDict
OrderedDict 和 dict 基本一样,唯一的不同是 dict 没有保存键值对的先后顺序,而 OrderedDict 保存了键值对的先后顺序 -
collections 说明链接:https://blog.youkuaiyun.com/shangboerds/article/details/82177181
#通过画图判断是否相关性
#从dataframe中把标签和特征导出来
exam_X = exam['学习时间']
exam_Y = exam['分数']
#绘制散点图
plt.scatter(exam_X, exam_Y, color = 'green')
#设定X,Y轴标签和title
plt.ylabel('scores')
plt.xlabel('times')
plt.title('exam data')
plt.show()
#分割数据,一个train 一个test
from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(exam_X,
exam_Y,
train_size = 0.8)
#这里可以简单的看一下分割后的结果
X_train.head()
X_train.shape
#可以发现训练集是16行一列的数据,测试集是四行一列,符合切分比例
- sklearn使用教程
https://www.jianshu.com/p/6ada34655862
sklearn官方:https://scikit-learn.org/stable/
讲解简洁: https://blog.youkuaiyun.com/u014248127/article/details/78885180 - train_test_split函数:
可以在样本数据集中随机的选取测试集与训练集,随机客观的划分数据,减少人为因素
完整模板:
train_X,test_X,train_y,test_y =train_test_split(train_data,train_target,test_size=0.3,random_state=5)
参数解释:
**train_data:**待划分样本数据
**train_target:**待划分样本数据的结果(标签)
**test_size:**测试数据占样本数据的比例,若整数则样本数量
**random_state:**设置随机数种子,保证每次都是同一个随机数。若为0或不填,则每次得到数据都不一样
倒入模型
#首先,改变一下数组的形状
X_train = X_train.values.reshape(-1, 1)
X_test = X_test.values.reshape(-1, 1)
#从skl中导入线性回归的模型
from sklearn.linear_model import LinearRegression
#创建一个模型
model = LinearRegression()
#训练一下
model.fit(X_train, Y_train)
-
reshape(-1,1)什么意思
先前我们不知道z的shape属性是多少,但是想让z变成只有一列,行数不知道多少,通过z.reshape(-1,1)
,Numpy自动计算出有12行,新的数组shape属性为(16, 1),与原来的(4, 4)配套。
https://blog.youkuaiyun.com/wld914674505/article/details/80460042 -
linearRegression模型:
https://blog.youkuaiyun.com/walk_power/article/details/82924363
#因为线性回归一般方程为y = a+bx
#b为斜率,a为截距
#截距用intercept_方法获得
#斜率用model.coef_方法获得
a = model.intercept_
b = model.coef_
a = float(a)
b = float(b)
print('该模型的简单线性回归方程为y = {} + {} * x'.format(a, b))
评估模型
#绘制散点图
plt.scatter(exam_X, exam_Y, color = 'green', label = 'train data')
#设定X,Y轴标签和title
plt.ylabel('scores')
plt.xlabel('times')
#绘制最佳拟合曲线
Y_train_pred = model.predict(X_train)
plt.plot(X_train, Y_train_pred, color = 'black', label = 'best line')
#来个图例
plt.legend(loc = 2)
plt.show()
计算R^2
model.score(X_test, Y_test)
二、机器学习入门之逻辑回归分类
https://www.heywhale.com/mw/project/5a69ad00afceb51770d627fb
逻辑回归更好理解
从创建数据集开始
#创见数据集
examDict={
'学习时间':[0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,
2.50,2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50],
'考试结果': [0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
}
examOrderDict=OrderedDict(examDict)
exam=pd.DataFrame(examOrderDict)
exam.head()
exam_X = exam['学习时间']
exam_Y = exam['考试结果']
#导入包
import matplotlib.pyplot as plt
#绘制散点图
plt.scatter(exam_X, exam_Y, color = 'green')
#设定X,Y轴标签和title
plt.ylabel('scores')
plt.xlabel('times')
plt.title('exam data')
plt.show()
拆分
exam_X = exam['学习时间']
exam_Y = exam['考试结果']
#导入包
import matplotlib.pyplot as plt
#绘制散点图
plt.scatter(exam_X, exam_Y, color = 'green')
#设定X,Y轴标签和title
plt.ylabel('scores')
plt.xlabel('times')
plt.title('exam data')
plt.show()
建模训练
#因为特征只有一个,所以要改变一下数据形状
X_train = X_train.values.reshape(-1, 1)
X_test = X_test.values.reshape(-1, 1)
#导入
from sklearn.linear_model import LogisticRegression
#创建模型
model = LogisticRegression()
#训练
model.fit(X_train, Y_train)
测试
model.score(X_test, Y_test)
model.predict_proba(3)
array([[ 0.2846885, 0.7153115]])
当我们给定输入一个特征的时候,可以返回其概率值,返回的第一个是其为0的概率值,第二个是为1的概率值。根据我们的决策面的结果,也就是x=3时候我们的logistic的函数值就是其为1的概率值。当这个值大于0.5的时候,我们做出决策,认为它的值为1,当这个概率值小于0.5的时候,我们做出决策,认为它的值为0。
在这里,我们发现,这个值为0.715,所以在此模型下,我们做出决策,认为值为1,通过上文我们定义的字典数据,可以发现,我们的决策是正确的。