from sklearn.datasets import load_boston
报错
ImportError:
`load_boston` has been removed from scikit-learn since version 1.2.
原因是:
因为数据集涉及种族问题,所以在sklearn 1.2版本中被移除。
解决方法其实在报错的地方就给了:
import numpy as np
import pandas as pd
data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep=r"\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]
其中data就是这个数据集(除了最后一列),如下:
[[6.3200e-03 1.8000e+01 2.3100e+00 ... 1.5300e+01 3.9690e+02 4.9800e+00]
[2.7310e-02 0.0000e+00 7.0700e+00 ... 1.7800e+01 3.9690e+02 9.1400e+00]
[2.7290e-02 0.0000e+00 7.0700e+00 ... 1.7800e+01 3.9283e+02 4.0300e+00]
...
[6.0760e-02 0.0000e+00 1.1930e+01 ... 2.1000e+01 3.9690e+02 5.6400e+00]
[1.0959e-01 0.0000e+00 1.1930e+01 ... 2.1000e+01 3.9345e+02 6.4800e+00]
[4.7410e-02 0.0000e+00 1.1930e+01 ... 2.1000e+01 3.9690e+02 7.8800e+00]]
target是数据集最后一列。太长了,这里不予展示全部:
24. 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 15. 18.9 21.7 20.4
18.2 19.9 23.1 17.5 20.2 18.2 13.6 19.6 15.2 14.5 15.6 13.9 16.6 14.8..........
代码解释:
data是一个NumPy数组,是通过水平叠加(np.hstack)raw_df 中的数据而创建的。在这个操作中,选择了raw_df 中的奇数行(raw_df.values[::2, :])和偶数行的前两列(raw_df.values[1::2, :2])并将它们水平叠加在一起。
target是一个NumPy数组,它由raw_df 中的偶数行的第三列(raw_df.values[1::2, 2])组成。这通常表示目标或标签数据,用于监督学习任务,如回归分析。在这里,target 包含了原始数据集中每两行的第三列数据。
data包含了特征数据,而 target 包含了目标数据。
数据集部分截图:
可以通过以下网址之一获得这个数据集:
http://lib.stat.cmu.edu/datasets/boston
https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data
数据集描述:
描述:提供了与波士顿地区不同社区相关的一些特征,以及这些社区的房价中位数。
目的:房价预测。评估各种回归算法,如线性回归、决策树回归、随机森林回归等,以了解不同特征如何影响房价,以及如何建立准确的房价预测模型。
规格:data(506, 13) target(506,)
14个属性的描述:
1- CRIM 犯罪率;per capita crime rate by town
2- ZN proportion of residential land zoned for lots over 25,000 sq.ft.
3- INDUS 非零售商业用地占比;proportion of non-retail business acres per town
4- CHAS 是否临Charles河;Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
5- NOX 氮氧化物浓度;nitric oxides concentration (parts per 10 million)
6- RM 房屋房间数;average number of rooms per dwelling
7- AGE 房屋年龄;proportion of owner-occupied units built prior to 1940
8- DIS 和就业中心的距离;weighted distances to five Boston employment centres
9- RAD 是否容易上高速路;index of accessibility to radial highways
10- TAX 税率;full-value property-tax rate per $10,000
11- PTRATIO 学生人数比老师人数;pupil-teacher ratio by town
12- B 城镇黑人比例计算的统计值;1000(Bk - 0.63)^2 where Bk is the proportion of black people by town
13- LSTAT 低收入人群比例;% lower status of the population
14- MEDV 房价中位数;Median value of owner-occupied homes in $1000's
23.10.25更新:
导入数据集的方法:
boston = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :3]])
print(boston.shape)
输出(506, 14)
PCA与属性子集的选择:
import numpy as np
import pandas as pd
from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression
from sklearn.decomposition import PCA
data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep=r"\s+", skiprows=22, header=None) # 跳过了文件的前22行,没有使用文件的第一行作为列名
# data包含了特征数据,而target包含了目标数据。
# 因为原数据集是从23行开始,每两行对应一行的数据。
# 所以data选择raw_df中的奇数行(raw_df.values[::2, :])和偶数行的前两列(raw_df.values[1::2, :2])
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
# target由raw_df 中的偶数行的第三列(raw_df.values[1::2, 2])组成
target = raw_df.values[1::2, 2]
print(data.shape)
pca = PCA()
pca.fit(data)
print(pca.explained_variance_ratio_) # 输出方差百分比
pca = PCA(3)
pca.fit(data)
low_d = pca.transform(data) # 降维
print(low_d.shape)
names = ["CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "LSTAT", "MEDV"]
IR = LinearRegression()
rfe = RFE(IR, n_features_to_select=1)
rfe.fit(data, target)
print(sorted(zip(map(lambda x: round(x, 4), rfe.ranking_), names)))
至此就over了,至于对此数据集的研究,请参考其他文章了~