-
Notifications
You must be signed in to change notification settings - Fork 0
/
House_prices.py
104 lines (63 loc) · 2.49 KB
/
House_prices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pandas as pd
import numpy as np
import os
import sklearn
from sklearn.preprocessing import LabelEncoder
from math import sqrt
data = pd.read_csv('Data/House Prices/train.csv')
data.info()
data.isnull().sum().sort_values(ascending = False)
data = data.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu', 'Id'] , axis=1)
cols_with_null = data.columns[data.isnull().any()].tolist()
numeric = [x for x in data.columns if data[x].dtypes != 'object']
category = [x for x in data.columns if data[x].dtypes == 'object']
number = LabelEncoder()
for column in category:
data[column] = (number.fit_transform(data[column]))
#Imputing numerical data with mean
for column in numeric:
data[column].fillna((data[column].mean()), inplace=True)
#co-relation between variables
cor_matrix = data.corr().abs()
cor_y = cor_matrix[cor_matrix.index == 'SalePrice']
req_var = list(cor_y[cor_y >= 0.5].dropna(axis=1).columns)
req_var.remove('SalePrice')
len(req_var)
x = data.loc[:, req_var]
y = data['SalePrice']
x.shape
y.shape
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
# from sklearn import linear_model
# reg = linear_model.LinearRegression()
from sklearn.ensemble import RandomForestRegressor
reg = RandomForestRegressor(max_depth=2, random_state=0)
mdl = reg.fit(x_train, y_train)
pred = reg.predict(x_test)
from sklearn.metrics import mean_squared_error, r2_score
rmse = sqrt(mean_squared_error(y_test, pred))
r_square = r2_score(y_test, pred)
#--------predictions---------#
test = pd.read_csv('Data/House Prices/test.csv')
len(test)
test.isnull().sum().sort_values(ascending = False)
test = test.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu'] , axis=1)
category = [x for x in test.columns if test[x].dtypes == 'object']
for column in category:
test[column] = (number.fit_transform(test[column]))
numeric = [x for x in test.columns if test[x].dtypes != 'object']
for column in numeric:
test[column].fillna((test[column].mean()), inplace=True)
x_eval = test.loc[:, req_var]
y_eval = pd.DataFrame({'SalePrice' :mdl.predict(x_eval)})
len(y_eval)
len(test['Id'])
df_result = pd.concat([test['Id'], y_eval], axis=1)
df_final = pd.concat([test['Id'],df_result], axis=1)
filename = 'submission.csv'
try:
os.remove(filename)
except OSError:
pass
df_result.to_csv(filename, index=False)