前言
梯度下降算法学习。
一、数据集介绍
波士顿房价数据集:波士顿房价数据集,用于线性回归预测
二、预测房价代码
1.引入库
from sklearn.linear_model import LinearRegression as LR
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston as boston
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
import numpy as np
from sklearn.metrics import mean_squared_error
2.数据
def preprocess():
# get the dataset of boston
X = boston().data
y = boston().target
name_data = boston().feature_names
# draw the figure of relationship between feature and price
plt.figure(figsize=(20,20))
for i in range(len(X[0])):
plt.subplot(5, 3, i + 1)
plt.scatter(X[:, i], y, s=20)
plt.title(name_data[i])
plt.show()
# 删除相关性较低的特征
# X = np.delete(X, [0, 1, 3, 4, 6, 7, 8, 9, 11], axis=1)
# normalization
for i in range(len(X[0])):
X[:, i] = (X[:, i] - X[:, i].min()) / (X[:, i].max() - X[:, i].min())
# split into test and train
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, y, test_size=0.3, random_state=10)
return Xtrain, Xtest, Ytrain, Ytest, X
def lr(