Kaggle competition: House Prices - Advanced Regression Techniques
span['Order_Range_D'] = span.total_orders - span.first_order_num + 1
span.head()<merge>
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 print(train.head() , "
")
 print(train.info() )
uxp_ratio = pd.merge(times, span, on=['user_id', 'product_id'], how='left')
 uxp_ratio.head()<feature_engineering>
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))
prices = pd.DataFrame({"price":train["SalePrice"], "log(1+price)":np.log1p(train["SalePrice"])})
 prices.hist()
uxp_ratio['uxp_reorder_ratio'] = uxp_ratio.times_bought / uxp_ratio.Order_Range_D
uxp_ratio.head()<drop_column>
all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean())
 print("all_data.shape: ", all_data.shape, "
")
 print("successfully dummy encoded!" )
uxp_ratio.drop(['times_bought', 'total_orders', 'first_order_num', 'Order_Range_D'], axis=1, inplace=True)
 uxp_ratio.head()<drop_column>
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice
del all_data
print(X_train.shape)
 print(X_test.shape )
del [times, span, first_order_num, total_orders]
gc.collect()<merge>
print(numeric_feats, "
")
 print("number of numeric features: ", len(numeric_feats), "
")
 numeric_data = X_train[numeric_feats]
uxp = uxp.merge(uxp_ratio, on=['user_id', 'product_id'], how='left')
 del uxp_ratio
gc.collect()<data_type_conversions>
<groupby>
feature_cols = X_train.columns
selector = SelectKBest(score_func = f_regression, k=10)
 X_new = selector.fit_transform(X_train[feature_cols], y)
 selected_features = pd.DataFrame(selector.inverse_transform(X_new),
index = X_train.index,
columns=feature_cols)
 selected_columns = selected_features.columns[selected_features.var() != 0]
print("selected_columns: ", selected_columns, "
")
 scores = pd.Series(selector.scores_)
 sorted_scores = sorted(scores)[::-1][0:10]
print("sorted_scores: ", sorted_scores, "
")
 sns.barplot(sorted_scores, selected_columns, orient = "h" )
prior_orders['order_number_back'] = prior_orders.groupby(by=['user_id'])['order_number'].transform(max)- prior_orders.order_number + 1
prior_orders.head()<data_type_conversions>
selector = SelectKBest(score_func = mutual_info_regression, k=10)
 print("started fitting.....
 ")
 X_new = selector.fit_transform(X_train[feature_cols], y)
 selected_features = pd.DataFrame(selector.inverse_transform(X_new),
index = X_train.index,
columns=feature_cols)
 selected_columns = selected_features.columns[selected_features.var() != 0]
print("selected_columns: ", selected_columns, "
")
 scores = pd.Series(selector.scores_)
 sorted_scores = sorted(scores)[::-1][0:10]
print("sorted_scores: ", sorted_scores, "
")
 sns.barplot(sorted_scores, selected_columns, orient = "h" )
last_five['uxp_ratio_last_five'] = last_five.uxp_last_five / 5.0
last_five.head()<data_type_conversions>
from IPython.display import display
import eli5
from eli5.sklearn import PermutationImportance
<merge>
uxp = uxp.merge(last_five, on=['user_id', 'product_id'], how='left')
 del [last_five, temp]
gc.collect()
uxp.head()<correct_missing_values>
X_train_short = X_train.loc[:, ['OverallQual', 'YearBuilt', 'YearRemodAdd', '1stFlrSF', 'GrLivArea',
'FullBath', 'GarageCars', 'GarageArea', 'ExterQual_TA', 'KitchenQual_TA',
'Neighborhood_StoneBr', 'Functional_Typ', 'LotArea', 'Exterior1st_BrkFace',
'KitchenQual_Ex']]
print("fitting model...")
 my_model = Ridge().fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))
uxp.fillna(0, inplace=True)
 uxp.head(10 )<merge>
data = uxp.merge(users, on='user_id', how='left')
 data.head()<merge>
X_train_short = X_train.loc[:, ['OverallQual', 'YearBuilt', 'YearRemodAdd', '1stFlrSF', 'GrLivArea',
'FullBath', 'GarageCars', 'GarageArea', 'ExterQual_TA', 'KitchenQual_TA',
'Neighborhood_StoneBr', 'Functional_Typ', 'LotArea', 'Exterior1st_BrkFace',
'KitchenQual_Ex']]
print("fitting model...")
 my_model = Lasso().fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))
data = data.merge(prd, on='product_id', how='left')
 data.head()<drop_column>
print("fitting model...")
 my_model = Lasso(alpha = 0.001 ).fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))
del [users, prd, uxp]
gc.collect()<filter>
X_train_short = X_train.loc[:, ['OverallQual', 'YearBuilt', 'YearRemodAdd', '1stFlrSF', 'GrLivArea',
'FullBath', 'GarageCars', 'GarageArea', 'ExterQual_TA', 'KitchenQual_TA',
'Neighborhood_StoneBr', 'Functional_Typ', 'LotArea', 'Exterior1st_BrkFace',
'KitchenQual_Ex']]
print("fitting model...")
 my_model = RandomForestRegressor(n_estimators=100, random_state=1 ).fit(X_train_short, y)
 print("calculating permutation importance...( takes about 1-2 minutes)")
 perm = PermutationImportance(my_model, random_state=1 ).fit(X_train_short, y)
 display(eli5.show_weights(perm, feature_names = X_train_short.columns.tolist()))
orders_future = orders.loc[(( orders.eval_set == 1)|(orders.eval_set == 2)) , ['user_id', 'eval_set', 'order_id']]
orders_future.head()<merge>