多元线性回归(波士顿房价预测)
导入模块
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')
获取数据
df = pd.read_csv('housing-data.txt', sep='\s+', header=0)
X = df.iloc[:, :-1].values
y = df['MEDV'].values
# 将数据分成训练集(0.7)和测试集(0.3)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
训练模型
lr = LinearRegression()
# 训练模型
lr.fit(X_train, y_train)
# 预测训练集数据
y_train_predict = lr.predict(X_train)
# 预测测试集数据
y_test_predict = lr.predict(X_test)
可视化