python - 在 jupyter notebook 中加载数据集
问题描述
我正在尝试在 jupyter notebook 中下载和加载数据集,但出现问题,代码如下:
import os
import tarfile
from six.moves import urllib
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/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()
import pandas as pd
def load_housing_data(housing_path=HOUSING_PATH):
csv_path = os.path.join(housing_path, "housing.csv")
return pd.read_csv(csv_path)
housing = load_housing_data()
housing.head()
运行上面的代码后,我得到了这个错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-5-6a9011700846> in <module>
----> 1 housing = load_housing_data()
2 housing.head()
<ipython-input-4-4d0bff7b3608> in load_housing_data(housing_path)
2 def load_housing_data(housing_path=HOUSING_PATH):
3 csv_path = os.path.join(housing_path, "housing.csv")
----> 4 return pd.read_csv(csv_path)
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, doublequote, delim_whitespace, low_memory, memory_map, float_precision)
676 skip_blank_lines=skip_blank_lines)
677
--> 678 return _read(filepath_or_buffer, kwds)
679
680 parser_f.__name__ = name
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
438
439 # Create the parser.
--> 440 parser = TextFileReader(filepath_or_buffer, **kwds)
441
442 if chunksize or iterator:
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
785 self.options['has_index_names'] = kwds['has_index_names']
786
--> 787 self._make_engine(self.engine)
788
789 def close(self):
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
1012 def _make_engine(self, engine='c'):
1013 if engine == 'c':
-> 1014 self._engine = CParserWrapper(self.f, **self.options)
1015 else:
1016 if engine == 'python':
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
1706 kwds['usecols'] = self.usecols
1707
-> 1708 self._reader = parsers.TextReader(src, **kwds)
1709
1710 passed_names = self.names is None
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()
FileNotFoundError: File b'datasets/housing/housing.csv' does not exist
我尝试手动下载数据并将 .CSV 文件添加到同一文件夹中,它适用于以下代码:
import pandas as pd
import numpy as np
pd.read_csv('housing.csv', delimiter = ',')
我的问题是关于第一次编码有什么问题?如果有人能解释一下,我将不胜感激。顺便说一句,我使用的是 Mac 10.14。
注意:该编码是“Hands on Machine Learning with Scikit Learn and Tensorflow”一书中的一个示例
解决方案
def fetch_housing_data()
没有被调用,所以没有目录或下载的文件。你需要fetch_housing_data()
调用def load_housing_data
像这样:
def load_housing_data(housing_path=HOUSING_PATH):
# missing function call to fetch the data
fetch_housing_data()
csv_path = os.path.join(housing_path, "housing.csv")
return pd.read_csv(csv_path)
推荐阅读
- java - JSON response Not Working, don't know the issue
- c# - CD 中的两个 azure 功能管道部署到一个应用程序服务不工作
- html2canvas - HTML2CANVAS 累积画布
- python - 如何将每个数据帧行值存储在字符串中并执行 URL
- python - 为什么在这种情况下事件总是 pygame.MOUSEBUTTONDOWN ?
- c# - IndexOutOfRange when player reaches maxLevel
- java - My Java Scanner wont stop running. I was trying make a method that alphabetically sorts a string but the scanner wont stop scanning
- python - 我无法在 python 中将行插入到我的 psycopg2 脚本中
- azure-service-fabric - 如何修复 Service Fabric 运行状况错误“在运行状况存储中找不到实体”。
- vim - VIM - 使用包管理器安装 vim-sexp-mappings-for-regular-people (vim 8.1)