boston房价数据集探索,加载数据集、查看数据集的基本信息、数据集划分
时间: 2024-06-09 21:09:49 浏览: 10
好的,我可以帮你完成这个任务。首先,你需要将数据集下载到本地并解压缩。可以从以下链接中下载数据集:https://archive.ics.uci.edu/ml/machine-learning-databases/housing/
接下来,你需要安装 Pandas 和 Scikit-learn 库。你可以使用以下命令安装:
```
pip install pandas scikit-learn
```
然后,你可以使用以下代码加载数据集:
```python
import pandas as pd
# 加载数据集
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data'
names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']
dataset = pd.read_csv(url, delim_whitespace=True, names=names)
```
接下来,你可以使用以下代码查看数据集的基本信息:
```python
# 查看数据集的形状
print(dataset.shape)
# 查看数据集的前5行
print(dataset.head())
# 查看数据集的统计信息
print(dataset.describe())
```
最后,你需要将数据集划分为训练集和测试集。你可以使用 Scikit-learn 库中的 `train_test_split` 函数来完成划分:
```python
from sklearn.model_selection import train_test_split
# 将数据集划分为训练集和测试集
X = dataset.drop('MEDV', axis=1)
y = dataset['MEDV']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
```
经过以上步骤,你已经完成了数据集的加载、数据集的基本信息查看以及数据集的划分。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)