<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/gdrive')
!pip install -U scikit-learn
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing(as_frame=True)
data, target = housing.data, housing.target

print(housing.DESCR)

target *= 1000
target.head()
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state=0)
regressor.fit(data, target)
from sklearn.metrics import mean_absolute_error

predicted_target = regressor.predict(data)

score = mean_absolute_error(predicted_target, target)

print(f'On average, our regressor makes an error of  {score:.3f} k$')
from sklearn.model_selection import train_test_split

data_train, data_test, target_train, target_test = train_test_split(
    data, target, random_state=0, shuffle=True
)
regressor.fit(data_train, target_train)
target_predicted=regressor.predict(data_test)

score = mean_absolute_error(target_predicted, target_test)
print(f'On average, our regressor make an error of {score:.3f} k$')
from sklearn.model_selection import ShuffleSplit, cross_validate

CV = ShuffleSplit(n_splits=6, test_size=0.3, random_state=0)
cv_result = cross_validate(regressor, data, target, cv=CV, scoring='neg_mean_absolute_error' )

import pandas as pd
cv_result = pd.DataFrame(cv_result)
cv_result.head(5)
cv_result['test_error'] = -cv_result['test_score']
cv_result.head(2)
import matplotlib.pyplot as plt

cv_result['test_error'].plot.hist(bins=10, edgecolor='red', density=True)
plt.xlabel('Mean Absolute Error (k$)')
_=plt.title('Test Error Distribution')

print(f"The standard deviation of the testing error is: "
      f"{cv_result['test_error'].std():.2f} k$")
print(f"The mean cross-validated testing error is: "
      f"{cv_result['test_error'].mean():.2f} k$")
target.plot.hist(bins=10, edgecolor="black", density=True)
plt.xlabel("Median House Value (k$)")
_ = plt.title("Target distribution")

print(f"The standard deviation of the target is: {target.std():.2f} k$")
cv_result = cross_validate(regressor, data, target, return_estimator=True, cv=CV)
cv_result
len(cv_result["estimator"])
from sklearn.model_selection import cross_val_score

scores = cross_val_score(regressor, data, target)
scores.mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1191304347826087, 'fraction_numerical': 0.009130434782608696, 'mean_word_length': 7.650375939849624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '642628', 'license': 'MIT', 'n_tokens_mistral': 816, 'n_tokens_neox': 781, 'n_words': 171, 'path': 'M2_Overfitting_and_underfitting.ipynb', 'repo_name': 'HamidMandi/Automaton', 'size': '2300'}