text_html_output_1.png
from sklearn.model_selection import train_test_split

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train_df = pd.read_csv('../input/train.csv')
train_df['Test'] = False
test_df = pd.read_csv('../input/test.csv')
test_df['Test'] = True
df = pd.concat([train_df, test_df], sort=False)

corr = abs(train_df.corr())
price_corr = corr['SalePrice'].sort_values()
columns = list(price_corr[price_corr > 0.4].index)
columns.append('Test')
columns.append('Id')

prepared_data = df[columns].copy()
id_col = df['Id']
uniq = prepared_data.apply(lambda x: x.nunique())
idxs = np.array((uniq <= 10) & (uniq > 2))
dummies_columns = prepared_data.iloc[:, idxs].columns
cont_cols = set(prepared_data.columns) - set(dummies_columns)
have_garage_cards = np.where(prepared_data['GarageCars'] >= 1, 1, 0)
have_full_bath = np.where(prepared_data['FullBath'] >= 1, 1, 0)
is_new = np.where(prepared_data['YearBuilt'] > 2005, 1, 0)
have_firplaces = np.where(prepared_data['Fireplaces'] >= 1, 1, 0)
prepared_data = pd.get_dummies(prepared_data, columns=dummies_columns)
prepared_data.fillna(0, inplace=True)
price_multy = prepared_data['SalePrice'].max()
scaler = MinMaxScaler()
prepared_data.loc[:, cont_cols] /= prepared_data.loc[:, cont_cols].max()
prepared_data[['Id', 'Test']] = df[['Id', 'Test']]
prepared_data['HaveCar'] = have_garage_cards
prepared_data['HaveFullBath'] = have_garage_cards
prepared_data['IsNew'] = is_new
prepared_data['HaveFirplaces'] = have_firplaces

train_data = prepared_data.loc[prepared_data['Test'] != True]
train_y = train_data['SalePrice']
train_x = train_data.drop(columns=['SalePrice', 'Id'])
X_train, X_test, y_train, y_test = train_test_split(train_x, train_y, test_size=0.2, random_state=42)

from sklearn.linear_model import *
from sklearn.metrics import *
results = []
models = [RidgeCV(), LinearRegression(), Ridge(), Lasso(alpha=0.1), BayesianRidge()]
fit_models = []
for regr in models:
    regr.fit(X_train, y_train)
    pred = regr.predict(X_test)
    mse = mean_squared_error(pred, y_test)
    results.append(mse)
    fit_models.append(regr)
best_idx = np.argmin(results)
regr = fit_models[best_idx]

result_df = prepared_data.loc[prepared_data['Test'] == True]
id_col = result_df['Id']
result_df = result_df.drop(columns=['SalePrice', 'Id'])
predictions = regr.predict(result_df)
res_df = pd.DataFrame(predictions * price_multy, columns=['SalePrice'])
res_df['Id'] = id_col
res_df.to_csv('sub.csv', index=None, header=True)
res_df.head()