【机器学习】梯度下降预测波士顿房价


前言

梯度下降算法学习。

一、数据集介绍

波士顿房价数据集:波士顿房价数据集,用于线性回归预测

二、预测房价代码

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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值