<jupyter_start><jupyter_text>Fetch Data<jupyter_code>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):
    """Creates datasets/housing folder in the workspace, 
    downloads the housing.tgz file, and extracts
    the housing.csv from it in this newly created folder."""
    # if the folder does not exist, create it.
    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()<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>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()<jupyter_output><empty_output><jupyter_text>See Big Picture<jupyter_code>housing.head()
housing.info()
housing["ocean_proximity"].value_counts()
housing.describe()
%matplotlib inline
import matplotlib.pyplot as plt

housing.hist(bins=50, figsize=(20,20))

plt.show()<jupyter_output><empty_output><jupyter_text>Create a Test Set<jupyter_code>import numpy as np
def split_train_test(data, test_ratio):
    shuffled_indices = np.random.permutation(len(data))
    #print(shuffled_indices)
    test_set_size = int(len(data) * test_ratio)
    test_indices = shuffled_indices[:test_set_size]
    train_indices = shuffled_indices[test_set_size:]
    return data.iloc[train_indices], data.iloc[test_indices]
train_set, test_set = split_train_test(housing, 0.2)
print(len(train_set))
print(len(test_set))

print('raio of test_set to total number of samples: ', len(test_set)/(len(test_set) + len(train_set)))
   train_set.iloc[0,0]
train_set.iloc[0]
train_set.iloc[:,0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1156015037593985, 'fraction_numerical': 0.006109022556390977, 'mean_word_length': 5.889967637540453, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '435399', 'license': 'MIT', 'n_tokens_mistral': 801, 'n_tokens_neox': 781, 'n_words': 127, 'path': 'ch2_housing_price_prediciton.ipynb', 'repo_name': 'kbigdelysh/geron-ml-book', 'size': '2128'}