ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.12k stars 12.91k forks source link

Chapter#02 FileNotFoundError #645

Closed ejzkhan closed 2 years ago

ejzkhan commented 2 years ago

Hi

I am new to ML and recently bought a book and trying to understand example mention in book but getting an error. i am using it through window pyCharm IDE i use first

import os import tarfile import urllib import urllib.request import pandas as pd

DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/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): os.makedirs(housing_path, exist_ok=True) 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()

then def load_housing_data(housing_path=HOUSING_PATH): csv_path = os.path.join(housing_path, "housing.csv") return pd.read_csv(csv_path)

at last housing = load_housing_data() housing.head()

I am getting below error. i will appreciate help from anyone

FileNotFoundError Traceback (most recent call last) C:\Users\EJAZKH~1\AppData\Local\Temp/ipykernel_18456/1729155196.py in ----> 1 housing = load_housing_data() 2 housing.head()

C:\Users\EJAZKH~1\AppData\Local\Temp/ipykernel_18456/1587917879.py in load_housing_data(housing_path) 1 def load_housing_data(housing_path=HOUSING_PATH): 2 csv_path = os.path.join(housing_path, "housing.csv") ----> 3 return pd.read_csv(csv_path)

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\util_decorators.py in wrapper(*args, *kwargs) 309 stacklevel=stacklevel, 310 ) --> 311 return func(args, **kwargs) 312 313 return wrapper

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\parsers\readers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 584 kwds.update(kwds_defaults) 585 --> 586 return _read(filepath_or_buffer, kwds) 587 588

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\parsers\readers.py in _read(filepath_or_buffer, kwds) 480 481 # Create the parser. --> 482 parser = TextFileReader(filepath_or_buffer, **kwds) 483 484 if chunksize or iterator:

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\parsers\readers.py in init(self, f, engine, **kwds) 809 self.options["has_index_names"] = kwds["has_index_names"] 810 --> 811 self._engine = self._make_engine(self.engine) 812 813 def close(self):

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\parsers\readers.py in _make_engine(self, engine) 1038 ) 1039 # error: Too many arguments for "ParserBase" -> 1040 return mapping[engine](self.f, **self.options) # type: ignore[call-arg] 1041 1042 def _failover_to_python(self):

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py in init(self, src, **kwds) 49 50 # open handles ---> 51 self._open_handles(src, kwds) 52 assert self.handles is not None 53

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\parsers\base_parser.py in _open_handles(self, src, kwds) 220 Let the readers open IOHandles after they are done with their potential raises. 221 """ --> 222 self.handles = get_handle( 223 src, 224 "r",

c:\users\ejaz khan\documents\python-games\venv\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options) 700 if ioargs.encoding and "b" not in ioargs.mode: 701 # Encoding --> 702 handle = open( 703 handle, 704 ioargs.mode,

FileNotFoundError: [Errno 2] No such file or directory: 'datasets\housing\housing.csv'

ejzkhan commented 2 years ago

Solved. i didn't call the function fetch_housing_data()