一、要求
boston 房價數據是機器學習中著名的基礎數據集,包含 506 條記錄,每條記錄包含房
屋的 13 條屬性,房價信息屬性 MEDV 在 boston.target 中,具體(翻譯成中文) 可通過如下語句查看:
print(boston.DESCR)
各屬性的中文解釋如下:
- CRIM 城鎮人均犯罪率
- ZN 占地面積超過 25,000 平方尺的住宅用地比例
- INDUS 城鎮中非商業用地比例
- CHAS Charles River 虛擬變量(如果邊界是河流則為 1;否則為 0)
- NOX 一氧化氮濃度
- RM 每棟住宅平均房間數
- AGE 1940 年前建成的自住房屋比例
- DIS 距五個波士頓就業中心的加權距離
- RAD 距離高速公路的便利指數- TAX 每 10,000 美元的全額房產稅率
- PTRATIO 城鎮中學生教師比例
- B 城鎮中黑人比例
- LSTAT 人口中低收入階層比例
- MEDV 自住房房價中位數
完成如下數據處理和分析任務:
(1)在一張畫布上,畫出每個變量與房價變化的散點圖,并詳細分析各個變量和房價
之間的關系。
(2)計算變量和房價的相關系數(相關系數的函數 df.corr())。
(3)建立所有變量和房價的線性回歸模型,寫出模型表達式,并分析模型的顯著性。
(4)將系數檢驗結果不顯著的變量去掉,重新建立線性模型。
(5)選擇與房價的相關系數大于等于 0.5 的變量,作為模型的自變量,房價作為因變
量,建立線性回歸模型,并將房價預測值和真實值繪制成折線圖。
二、代碼
from
sklearn
import
datasets
import
pandas
as
pd
import
matplotlib
.
pyplot
as
plt
#
from
scipy
.
misc
import
factorial 依賴scipy 而且為
1.2
.0
版本
#此文件依賴pillow
boston
=
pd
.
read_csv
(
'./dataset/boston_house.csv'
)
df
=
pd
.
DataFrame
(
boston
)
#
print
(
df
.
iloc
[
:
,
-
1
]
)
#相關系數大于
0.5
x_has
=
[
]
y_predict
=
[
]
plt
.
figure
(
1
)
#第一題
plt
.
rcParams
[
'font.sans-serif'
]
=
'SimHei'
plt
.
rcParams
[
'axes.unicode_minus'
]
=
False
for
i
in
range
(
13
)
:
plt
.
subplot
(
7
,
2
,
i
+
1
)
plt
.
scatter
(
df
.
iloc
[
:
,
i
]
,
df
.
iloc
[
:
,
-
1
]
,
marker
=
'o'
,
c
=
'g'
)
# x
,
y
,
,
green
#第二題
#
print
(
type
(
df
.
columns
[
1
]
)
)
dfi
=
df
[
[
df
.
columns
[
i
]
,
df
.
columns
[
-
1
]
]
]
print
(
'\n'
,
dfi
.
corr
(
)
,
dfi
.
corr
(
)
.
iloc
[
0
,
1
]
,
'\n'
)
#
print
(
dfi
.
corr
(
)
.
iloc
[
0
,
1
]
)
#第三題
import
numpy
as
np
from
sklearn
.
linear_model
import
LinearRegression
x_linear
=
df
.
iloc
[
:
,
i
]
.
values
.
reshape
(
-
1
,
1
)
#將DataFrame轉為array格式,通過values 屬性
y_linear
=
df
.
iloc
[
:
,
-
1
]
.
values
.
reshape
(
-
1
,
1
)
##
reshape
(
-
1
,
1
)
功能
#
print
(
x_linear
,
type
(
x_linear
)
)
lreg
=
LinearRegression
(
)
lreg
.
fit
(
x_linear
,
y_linear
)
message0
=
'一元線性回歸方程為: '
+
'\ty'
+
'='
+
str
(
lreg
.
intercept_
[
0
]
)
+
' + '
+
str
(
lreg
.
coef_
[
0
]
[
0
]
)
+
'*x'
import
scipy
.
stats
as
stats
n
=
len
(
x_linear
)
y_prd
=
lreg
.
predict
(
x_linear
)
if
dfi
.
corr
(
)
.
iloc
[
0
,
1
]
>
0.5
:
x_has
.
append
(
i
)
y_predict
.
append
(
y_prd
)
Regression
=
sum
(
(
y_prd
-
np
.
mean
(
y_linear
)
)
**
2
)
# 回歸
Residual
=
sum
(
(
y_linear
-
y_prd
)
**
2
)
# 殘差
R_square
=
Regression
/
(
Regression
+
Residual
)
# 相關性系數
R
^
2
F
=
(
Regression
/
1
)
/
(
Residual
/
(
n
-
2
)
)
#
F
分布
#取a
=
0.05
if
stats
.
pearsonr
(
x_linear
,
y_linear
)
[
1
]
[
0
]
<
0.05
:
ms1_1
=
'顯著'
else
:
ms1_1
=
'不顯著'
message1
=
'顯著性檢測(p值檢測):'
+
str
(
stats
.
pearsonr
(
x_linear
,
y_linear
)
[
1
]
[
0
]
)
+
ms1_1
print
(
message0
,
'\n'
,
message1
)
#第四題
print
(
x_has
,
y_predict
)
plt
.
show
(
)
#第五題
plt
.
figure
(
2
)
for
i
in
range
(
len
(
x_has
)
)
:
plt
.
plot
(
df
.
iloc
[
:
,
-
1
]
.
values
,
marker
=
'o'
,
c
=
'g'
)
plt
.
plot
(
y_predict
[
i
]
,
marker
=
'o'
,
c
=
'r'
)
plt
.
show
(
)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
