【发布时间】:2019-03-15 19:55:02
【问题描述】:
我正在尝试在 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》一书中的一个示例
【问题讨论】:
-
如果你运行
os.listdir(),你能看到datasets文件夹吗? -
欢迎来到 Stack Overflow!你能在你的帖子中用纯文本写出你的代码吗?如果您在运行某些代码时遇到问题,我们通常喜欢尝试自己运行相同的代码,看看是否存在明显的问题。还请尝试明确说明出了什么问题,以及您期望的行为。查看guidelines for how to ask good questions on Stack Overflow。快乐的小径!
-
请将代码作为文本而不是链接到图片。
-
@mkrieger1 抱歉,我将代码添加为文本。
-
@JordanSinger 感谢您的建议和抱歉,我将代码添加为文本。
标签: python machine-learning dataset jupyter-notebook