加州房价数据集提取代码
时间: 2023-12-23 21:03:53 浏览: 24
以下是获取加州房价数据集的代码:
```python
import os
import tarfile
from six.moves import urllib
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = "datasets/housing"
HOUSING_URL = DOWNLOAD_ROOT + HOUSING_PATH + "/housing.tgz"
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
if not os.path.isdir(housing_path):
os.makedirs(housing_path)
tgz_path = os.path.join(housing_path, "housing.tgz")
urllib.request.urlretrieve(housing_url, tgz_path)
housing_tgz = tarfile.open(tgz_path)
housing_tgz.extractall(path=housing_path)
housing_tgz.close()
fetch_housing_data()
```
这段代码将下载并提取加州房价数据集,保存在名为`housing`的文件夹中。
相关问题
提取加州房价数据集代码
以下是提取加州房价数据集的Python代码:
```python
import os
import tarfile
from six.moves import urllib
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/"
HOUSING_PATH = "datasets/housing"
HOUSING_URL = DOWNLOAD_ROOT + HOUSING_PATH + "/housing.tgz"
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
if not os.path.isdir(housing_path):
os.makedirs(housing_path)
tgz_path = os.path.join(housing_path, "housing.tgz")
urllib.request.urlretrieve(housing_url, tgz_path)
housing_tgz = tarfile.open(tgz_path)
housing_tgz.extractall(path=housing_path)
housing_tgz.close()
fetch_housing_data()
```
这段代码将从GitHub下载加州房价数据集(housing.tgz),并将其提取到名为“datasets/housing”的文件夹中。
加州房价数据集怎么获取
加州房价数据集可以从多个来源获取,以下是其中的一些途径:
1. 加州大学欧文分校机器学习库:该机器学习库提供了一个名为California Housing Prices的数据集,该数据集包含了1990年加州各地区的房价数据。可以通过以下链接获取该数据集:https://archive.ics.uci.edu/ml/datasets/California+Housing+Prices
2. Kaggle:Kaggle是一个数据科学竞赛平台,提供了许多数据集供用户下载,其中包括加州房价数据集。可以通过以下链接获取该数据集:https://www.kaggle.com/camnugent/california-housing-prices
3. Scikit-Learn:Scikit-Learn是一个机器学习库,提供了许多数据集供用户使用,其中包括加州房价数据集。可以通过以下代码获取该数据集:
``` python
from sklearn.datasets import fetch_california_housing
california_housing = fetch_california_housing()
```
以上是获取加州房价数据集的一些方法,你可以根据自己的需求选择其中的一种方式。