Machine learning algorithm-LinearRegression() of scikit-learn realizes linear regression

Many scikit-learn regression estimators use the LIBLINEAR library, which has advanced optimization algorithms and more effective code optimization methods for non-standard variables

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.linear_model import LinearRegression

df = pd.read_csv('xxx\\housing.data.txt',
                 header=None,
                 sep='\s+')

df.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS',
              'NOX', 'RM', 'AGE', 'DIS', 'RAD',
              'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']
print(df.head())
X = df[['RM']].values
y = df['MEDV'].values

slr = LinearRegression()
slr.fit(X, y)
y_pred = slr.predict(X)
# LinearRegression将方程分为两个部分存放,coef_存放回归系数,intercept_则存放截距,
# 因此要查看方程,就是查看这两个变量的取值
print(slr.coef_)
print('Slope: %.3f' % slr.coef_[0])
print('Intercept: %.3f' % slr.intercept_)

# 观察线性回归与训练数据的吻合程度
def lin_regplot(X, y, model):
    # s:指定散点图点的大小,默认为20,通过传入新的变量,实现气泡图的绘制
    # c:指定散点图点的颜色,默认为蓝色
    # edgecolors:设置散点边界线的颜色
    plt.scatter(X, y, c='steelblue', edgecolor='white', s=70)
    plt.plot(X, model.predict(X), color='black', lw=2)
    return

lin_regplot(X, y, slr)
plt.xlabel('Average number of rooms [RM]')
plt.ylabel('Price in $1000s [MEDV]')

#plt.savefig('images/10_07.png', dpi=300)
plt.show()

Guess you like

Origin blog.csdn.net/fgg1234567890/article/details/113431731