<jupyter_start><jupyter_text>2.5.4 Feature Scaling  2.5.5 PipeLine<jupyter_code>from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

num_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy = "median")),
    ("attribs_adder", CombinedAttributesAdder()),
    ("std_scaler", StandardScaler())
])

housing_num_tr = num_pipeline.fit_transform(housing_num)
housing_num_tr
from sklearn.compose import ColumnTransformer

num_attribs = list(housing_num)
cat_attribs = ["ocean_proximity"]

full_pipeline = ColumnTransformer([
    ("num", num_pipeline, num_attribs),
    ("cat", OneHotEncoder(), cat_attribs)
])

housing_prepared = full_pipeline.fit_transform(housing)
housing_prepared
housing_prepared.shape<jupyter_output><empty_output><jupyter_text>2.6 Model Selection & Training  2.6.1 Train and Evaluate in the Training Set<jupyter_code>from sklearn.linear_model import LinearRegression

lin_reg = LinearRegression()
lin_reg.fit(housing_prepared, housing_labels)
some_data = housing.iloc[:5]
some_labels = housing_labels.iloc[:5]
some_data_prepared = full_pipeline.transform(some_data)

print("예측:", lin_reg.predict(some_data_prepared))
print("레이블:", list(some_labels))
from sklearn.metrics import mean_squared_error

housing_predictions = lin_reg.predict(housing_prepared)
lin_mse = mean_squared_error(housing_labels, housing_predictions)
lin_rmse = np.sqrt(lin_mse)
lin_rmse
from sklearn.tree import DecisionTreeRegressor

tree_reg = DecisionTreeRegressor()
tree_reg.fit(housing_prepared, housing_labels)
housing_predictions = tree_reg.predict(housing_prepared)
tree_mse = mean_squared_error(housing_labels, housing_predictions)
tree_rmse = np.sqrt(tree_mse)
tree_rmse<jupyter_output><empty_output><jupyter_text>2.6.2 Evaluate by using Cross Validation<jupyter_code>from sklearn.model_selection import cross_val_score

scores = cross_val_score(tree_reg, housing_prepared, housing_labels,
                        scoring="neg_mean_squared_error", cv=10)
tree_rmse_scores = np.sqrt(-scores)
def display_scores(scores):
    print("score:", scores)
    print("mean:", scores.mean())
    print("std:", scores.std())
    
display_scores(tree_rmse_scores)
lin_scores = cross_val_score(lin_reg, housing_prepared, housing_labels,
                        scoring="neg_mean_squared_error", cv=10)
lin_rmse_scores = np.sqrt(-lin_scores)
display_scores(lin_rmse_scores)
from sklearn.ensemble import RandomForestRegressor

# forest_reg = RandomForestRegressor()
# forest_reg.fit(housing_prepared, housing_labels)
# forest_reg_predictions = forest_reg.predict(housing_prepared)
# forest_mse = mean_squared_error(forest_reg_predictions, housing_labels)
# forest_rmse = np.sqrt(forest_mse)

# forest_rmse
# forest_scores = cross_val_score(forest_reg, housing_prepared, housing_labels,
#                         scoring="neg_mean_squared_error", cv=10)
# forest_rmse_scores = np.sqrt(-forest_scores)

# display_scores(forest_rmse_scores)<jupyter_output><empty_output><jupyter_text>2.7 Detailed Model Tuning<jupyter_code>from sklearn.model_selection import GridSearchCV

param_grid = [
    {'n_estimators' : [3, 10, 10], 'max_features' : [2, 4, 6, 8]},
    {'bootstrap' : [False], 'n_estimators' : [3, 10], 'max_features' : [2, 3, 4]}
]

forest_reg = RandomForestRegressor()

grid_search = GridSearchCV(forest_reg, param_grid, cv = 5,
                          scoring = 'neg_mean_squared_error',
                          return_train_score = True)

grid_search.fit(housing_prepared, housing_labels)
grid_search.best_params_
grid_search.best_estimator_
cvres = grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
    print(np.sqrt(-mean_score), params)<jupyter_output>63753.5461528816 {'max_features': 2, 'n_estimators': 3}
56152.21937257319 {'max_features': 2, 'n_estimators': 10}
55680.493228907944 {'max_features': 2, 'n_estimators': 10}
60071.83427422634 {'max_features': 4, 'n_estimators': 3}
53238.59067760571 {'max_features': 4, 'n_estimators': 10}
53272.46285373771 {'max_features': 4, 'n_estimators': 10}
59067.11817402712 {'max_features': 6, 'n_estimators': 3}
52123.910284894766 {'max_features': 6, 'n_estimators': 10}
51511.828043567955 {'max_features': 6, 'n_estimators': 10}
59419.71941788753 {'max_features': 8, 'n_estimators': 3}
52060.45361355151 {'max_features': 8, 'n_estimators': 10}
52055.81898330208 {'max_features': 8, 'n_estimators': 10}
62731.67739526088 {'bootstrap': False, 'max_features': 2, 'n_estimators': 3}
54459.70921097403 {'bootstrap': False, 'max_features': 2, 'n_estimators': 10}
60815.157162603784 {'bootstrap': False, 'max_features': 3, 'n_estimators': 3}
52196.44652080015 {'bootstrap': False, 'max_features': 3, 'n_estimators':[...]<jupyter_text>2.7.2. Randomize Search<jupyter_code># from sklearn.model_selection import RandomizedSearchCV
# from scipy.stats import randint

# param_distribs = {
#     'n_estimators' : randint(low = 1, high = 200),
#     'max_features' : randint(low = 1, high = 8)
# }

# forest_reg = RandomForestRegressor(random_state = 42)
# rnd_search = RandomizedSearchCV(forest_reg, param_distribs,
#                           n_iter = 10, cv = 5, scoring = 'neg_mean_squared_error',
#                           random_state = 42)

# rnd_search.fit(housing_prepared, housing_labels)<jupyter_output><empty_output><jupyter_text>2.7.3. Ensemble  2.7.4. Best model & Analyze error<jupyter_code>feature_importances = grid_search.best_estimator_.feature_importances_
feature_importances
extra_attribs = ["rooms_per_hhold", "pop_per_hhold", "bedrooms_per_room"]
cat_encoder = full_pipeline.named_transformers_["cat"]
cat_one_hot_attribs = list(cat_encoder.categories_[0])
attributes = num_attribs + extra_attribs + cat_one_hot_attribs
sorted(zip(feature_importances, attributes), reverse = True)<jupyter_output><empty_output><jupyter_text>2.7.4. Evaluate System by Test set<jupyter_code>final_model= grid_search.best_estimator_

X_test = strat_test_set.drop("median_house_value", axis = 1)
y_test = strat_test_set["median_house_value"].copy()

X_test_prepared = full_pipeline.transform(X_test)

final_predictions = final_model.predict(X_test_prepared)

final_mse = mean_squared_error(y_test, final_predictions)
final_rmse = np.sqrt(final_mse)
final_rmse
from scipy import stats

confidence = 0.95
squared_errors = (final_predictions - y_test)**2
np.sqrt(stats.t.interval(confidence, len(squared_errors)-1,
                        loc = squared_errors.mean(),
                        scale = stats.sem(squared_errors)))<jupyter_output><empty_output><jupyter_text>2.10. Practice Problem  1.<jupyter_code># from sklearn.svm import SVR
# from sklearn.model_selection import GridSearchCV

# param_grid = [
#         {'kernel': ['linear'], 'C': [10., 30., 100., 300., 1000., 3000., 10000., 30000.0]},
#         {'kernel': ['rbf'], 'C': [1.0, 3.0, 10., 30., 100., 300., 1000.0],
#          'gamma': [0.01, 0.03, 0.1, 0.3, 1.0, 3.0]},
#     ]

# svm_reg = SVR()
# grid_search = GridSearchCV(svm_reg, param_grid, cv = 5,
#                           scoring = 'neg_mean_squared_error', verbose = 2)
# grid_search.fit(housing_prepared, housing_labels)
# negative_mse = grid_search.best_score_
# rmse = np.sqrt(-negative_mse)
# rmse
# grid_search.best_params_<jupyter_output><empty_output><jupyter_text>2.  3.<jupyter_code>from sklearn.base import BaseEstimator, TransformerMixin

def indices_of_top_k(arr, k):
    return np.sort(np.argpartition(np.array(arr), -k)[-k:])

class TopFeatureSelector(BaseEstimator, TransformerMixin):
    def __init__(self, feature_importances, k):
        self.feature_importances = feature_importances
        self.k = k
    def fit(self, X, y = None):
        self.feature_indices = indices_of_top_k(self.feature_importances, self.k)
        return self
    def transform(self, X):
        return X[:, self.feature_indices]
#     def fit_transform(self, X, y = None):
#         self.fit(self, X, y = None)
#         return self.transform
k = 5
top_k_feature_indices = indices_of_top_k(feature_importances, k)
top_k_feature_indices
np.array(attributes)[top_k_feature_indices]
sorted(zip(feature_importances, attributes), reverse = True)[:k]
preparation_and_feature_selection_pipeline = Pipeline([
    ('preparation', full_pipeline),
    ('feature_selection', TopFeatureSelector(feature_importances, k))
])
housing_prepared_top_k_features = preparation_and_feature_selection_pipeline.fit_transform(housing)
housing_prepared_top_k_features[0:3]
housing_prepared[0:3, top_k_feature_indices]<jupyter_output><empty_output><jupyter_text>4.<jupyter_code># prepare_select_and_predict_pipeline = Pipeline([
#     ("preparaion", full_pipeline),
#     ('feature_selection', TopFeatureSelector(feature_importances, k)),
#     ('svm_reg', SVR(**rnd_search.best_params_))
# ])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12085549394590925, 'fraction_numerical': 0.05058277696050696, 'mean_word_length': 5.808936825885978, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '579769', 'license': 'Apache-2.0', 'n_tokens_mistral': 3506, 'n_tokens_neox': 3147, 'n_words': 534, 'path': '(study)02_end_to_end_machine_learning_project.ipynb', 'repo_name': 'ddungiii/handson-ml', 'size': '8837'}