ML3: sklearn 线性回归

import numpy as np 
import sklearn.linear_model as lm 
import sklearn.metrics as sm 
import matplotlib.pyplot as mp 


X,Y = [],[]
with open('dd.txt','r') as f:
    for line in f.readlines():
        data = [float(substr)
        for substr in line.split(',')]
        x.append(data[:-1])
        y.append(data[-1])
    x = np.array(x)
    y = np.array(y)
    #创建线性回归器
    model = lm.LinearRegression()
    #训练线性回归其
    model.fit(x,y)
    #预测输出
    pred_y = model.predict(x)
    mp.figure(num='Linear Regerssion',facecolor='lightgray')
    mp.title('Linear Regerssion',fontsize = 20)
    mp.xtable('x',fontsize=14)
    mp.ylable('y',fontsize = 14)
    mp.tick_params(labelsize=10)
    mp.grid(linestyle=':')
    mp.scatter(x,y,c='dodgerblue',s = 60,label='Sample')
    mp.plot(x,pred_y,c='orangered',label='Prediction')
    mp.legend()
    mp.show()

猜你喜欢

转载自blog.csdn.net/weixin_38246633/article/details/80582101