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.51].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)
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']]

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 *
regr = LinearRegression()
regr.fit(X_train, y_train)
pred = regr.predict(X_test)
mean_squared_error(pred, y_test)

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)
result_df = prepared_data.loc[prepared_data['Test'] == True]

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