拥抱时代--AI(3)

python语言为了研究机器学习专门发展起来一套框架,并且这个框架是开源的,它就是scikit-learn。它主要实现数据预处理,分类,回归,降维,模型选择等最常用的机器学习算法。

0 回顾一下梯度下降的原理

梯度下降算法是最常用的一种求最优解的算法,其原理即偏导构成的向量方向即其梯度方向,下面给出一维梯度算法的模拟

start=0
buchang=0.1

for i in range(100):
    yy=2*start*start+3*start
    start=start-(4*start+3)*buchang
    yx=2*start*start+3*start
    if yy<yx:
        break
print(start)
 

1 安装scikit-learn

在使用scikit-learn之前,我们先安装scikit-learn库。pip install scikit-learn,当然可以加载国内镜像。不换镜像也没有关系,也没有用多久即安装完成。

在此处我们假设一定的数据,

x=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],

y=[2.4587219227057, 6.837784075810438, 13.578197534706062, 14.250972327881273, 10.314208469267715, 15.908826534890443, 14.19863772096425, 16.923432852757983, 25.391880908139342, 24.59929510475269, 22.167255816427108, 25.85103609909175, 26.34869617678909, 26.71003378861379, 35.44845345177977, 37.47726764911904, 41.747008693730976, 41.926991564559444, 36.57429634055646, 38.953741650002186, 49.57882606034161, 42.92543769469588, 53.0196671157195, 51.14200542464646, 56.637331077241036, 59.29248583040243, 59.71221758493881, 55.420495803489956, 63.294829432985395, 62.44812471692443]

根据数据生成的图形为:

此处数据的模拟代码如下,感兴趣的小伙伴可以直接拿去运行模拟出上述效果:

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import random
x=[]
y=[]
for i in range(0,30,1):
    x.append(i)
    y.append(2*i+random.random()*10)
print(x);
print(y);
plt.figure(figsize=(20,20))
plt.scatter(x,y)
plt.show()

从图中可以看出他的预测曲线应该用线性回归最好,此时如果用前面讲解的线性回归算法自己计算可能需要很多过程,此时知道原理即可,用scikit-learn很快即可实现其回归函数,利用scikit-learn对上述数据做线性回归分析得到回归图形对比如下:

参考代码大家可以 直接复制做简单模拟,

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import random
from sklearn.linear_model import LinearRegression 
x=[]
y=[]
for i in range(0,30,1):
    x.append(i)
    y.append(2*i+random.random()*10)

plt.figure(figsize=(20,20))
plt.scatter(x,y)
#plt.show()
lr_model=LinearRegression()
#此时需要将x,和y
x=np.array(x)
 
x=x.reshape(-1,1) # 将原数组重塑为二维数组,-1为模糊控制,否则里面的数字乘积要等于总个数
y=np.reshape(y,(-1,1))
#print(y) #np实现和上面x一样的效果
lr_model.fit(x,y) #训练数据
y_yc=lr_model.predict(x)
#根据拟合得到的函数计算x对应的值
#print(y_yc)
zuo=plt.subplot(1,2,1)
zuo.scatter(x,y)
zhong=plt.subplot(1,2,2)
zhong.plot(x,y_yc,y)

plt.show()


2 使用scikit-learn 进行线性模拟

如果需要对新数据进行预测,则传递进新数据即可,如yy=lr_model.predict([[31]]),得到[[67.2463329]],这个数据和我们当时构造数据的思路y=2*x+random()*10预测结果在合理范围。

可以通过a=lr_model.coef_
b=lr_model.intercept_

求得线性函数的两个参数。
print("获得函数为:y=",a,"x+",b)

得到的准确函数为 :y= [[1.97294361]] x+ [4.96100185]

在此预测的函数怎么做出科学的评价呢?

from sklearn.metrics import mean_squared_error,r2_score,

这两个评价对应的公式为:

MSE值越小越好,R2越趋向于1函数越好,这个道理从上一博客讨论中即可知晓。运行效果如下图:

3 本博文全部运行代码:

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import random
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error,r2_score
x=[]
y=[]
for i in range(0,30,1):
    x.append(i)
    y.append(2*i+random.random()*10)

plt.figure(figsize=(20,20))
plt.scatter(x,y)
#plt.show()
lr_model=LinearRegression()
#此时需要将x,和y
x=np.array(x)
 
x=x.reshape(-1,1) # 将原数组重塑为二维数组,-1为模糊控制,否则里面的数字乘积要等于总个数
y=np.reshape(y,(-1,1))
#print(y) #np实现和上面x一样的效果
lr_model.fit(x,y) #训练数据
y_yc=lr_model.predict(x)
#根据拟合得到的函数计算x对应的值
#print(y_yc)
zuo=plt.subplot(1,2,1)
zuo.scatter(x,y)
zhong=plt.subplot(1,2,2)
zhong.plot(x,y_yc,y)

yy=lr_model.predict([[31]])
print(yy)
a=lr_model.coef_
b=lr_model.intercept_
print("获得函数为:y=",a,"x+",b)
Mse=mean_squared_error(y,y_yc)
R2=r2_score(y,y_yc)
print("");
print(Mse)
print(R2)
 


如果有疑问可以在评论区留言讨论。欢迎持续关注,一起加油进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值