波士顿房价 lr回归

import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
from collections import Counter

from sklearn.datasets import load_iris
plt.rcParams['font.sans-serif'] = ['STFangsong']
plt.rcParams['axes.unicode_minus'] = False
%config InlineBackend.figure_format = 'svg'
# y_hat = a0 + a1*x1

# L = (a0 + a1*1.91 - 86)^2 + (a0 + a1*2.03 - 113)^2 + (a0 + a1*2.03 - 112)^2
# L'(a0)  =2(a0 + a1*1.91 - 86) + 2(a0 + a1*2.03 - 113)+2(a0 + a1*2.03 - 112) =0
#L'(a1) = 2(a0 + a1*1.91 - 86)*1.91 + 2(a0 + a1*2.03 - 113)*2.03+2(a0 + a1*2.03 - 112)*2.03 =0
# a0 = 103
# a1 = 0.12

# y_hat = 103+0.12*x1


# 截距项  X1 X2 X3 X4 Y 
# 标准化 0-1 标准化缩放  
# MinMaxScaler    # StandardScaler

# MinMaxScaler  : Xi - Xmin/(Xmax - Xmin)
# StandardScaler  : Xi -Xmean/ Xstd

# 模型训练过程
# 1. X,Y
# 2. 划分数据集 xtrain,xtest,ytrain,ytest ,数据相关性检查
# 3. xtrain  xtest 标准化:计算xtrain 的mean std 用作x计算标准化的依据
# 4. 模型训练
# 5. 模型评估:R^2
from sklearn.datasets import load_boston
# boston 房价数据集
boston = load_boston()
x = boston.data
y = boston.target
# data = pd.DataFrame(boston.data, columns = boston.feature_names)
data = pd.DataFrame(x, columns = boston.feature_names)
data
CRIMZNINDUSCHASNOXRMAGEDISRADTAXPTRATIOBLSTAT
00.0063218.02.310.00.5386.57565.24.09001.0296.015.3396.904.98
10.027310.07.070.00.4696.42178.94.96712.0242.017.8396.909.14
20.027290.07.070.00.4697.18561.14.96712.0242.017.8392.834.03
30.032370.02.180.00.4586.99845.86.06223.0222.018.7394.632.94
40.069050.02.180.00.4587.14754.26.06223.0222.018.7396.905.33
..........................................
5010.062630.011.930.00.5736.59369.12.47861.0273.021.0391.999.67
5020.045270.011.930.00.5736.12076.72.28751.0273.021.0396.909.08
5030.060760.011.930.00.5736.97691.02.16751.0273.021.0396.905.64
5040.109590.011.930.00.5736.79489.32.38891.0273.021.0393.456.48
5050.047410.011.930.00.5736.03080.82.50501.0273.021.0396.907.88

506 rows × 13 columns

# 查看特征相关性
import seaborn as sns

sns.heatmap(data.corr(),center = 0)
<AxesSubplot:>

在这里插入图片描述

# 划分数据集

from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size = 0.3)
# 标准化
from sklearn.preprocessing import MinMaxScaler   # 0-1缩放
from sklearn.preprocessing import StandardScaler   # 标准化缩放
std = StandardScaler().fit(xtrain)
# 实例化 + 数据导入:std:xtrain(mean,std)
xtrain_ = std.transform(xtrain)
xtest_ = std.transform(xtest)
from sklearn.linear_model import LinearRegression
# 训练
lr = LinearRegression().fit(xtrain_,ytrain)
lr.coef_   # 参数[a1,a2,a3...am]  m个特征
array([-1.12267664,  0.97439663, -0.3459865 ,  0.41380091, -2.10474456,
        2.43778686, -0.22264589, -3.56271303,  3.34309993, -2.51830569,
       -1.93298784,  0.7963353 , -3.7752077 ])
lr.intercept_   # 截距项 ao
22.541525423728853
lr.score(xtest_,ytest)
0.7705065219692282
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值