数据分析: 线性回归案例

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import load_boston 
boston = load_boston()
data = boston['data']
target = boston['target']
feature_names = boston['feature_names']
from pandas import DataFrame
df =DataFrame(data=data,columns=feature_names)
df
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(data,target,test_size=56)
linear = LinearRegression()
linear.fit(X_train,y_train)
y=linear.predict(X_test)
linear.score(X_train,y_train)
linear.score(X_test,y_test)
linear.coef_
linear.intercept_
plt.figure(figsize=(3*5,5*5))
for i,feature in enumerate(feature_names):
    axes = plt.subplot(5,3,i+1)
    axes.scatter(df[feature].values,target)
    
    linear = LinearRegression()
    linear.fit(df[[feature]],target)
    x = np.linspace(df[feature].min(),df[feature].max(),100)
    w = linear.coef_[0]
    b = linear.intercept_
    y = w*x+b
    plt.plot(x,y,c='r')

    score = linear.score(df[[feature]],target)
    axes.set_title(feature + ' ' + str(score))

数据分析: 线性回归案例_第1张图片

你可能感兴趣的:(技术信息)