-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathbuild.py
More file actions
46 lines (40 loc) · 1.47 KB
/
build.py
File metadata and controls
46 lines (40 loc) · 1.47 KB
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
# %load q05_forward_selected/build.py
# Default imports
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import numpy as np
data = pd.read_csv('data/house_prices_multivariate.csv')
model = LinearRegression()
# Your solution code here
def forward_selected(data,model):
old_r2_score = 0
new_r2_score = 1
features = list(data.drop('SalePrice',axis=1).columns)
selected_features = []
r2_score_features = []
X_selected = pd.DataFrame()
result = pd.DataFrame()
y = data['SalePrice']
while(True):
scores = []
for i in range(len(features)):
X = data[features[i]]
X_selected = result
X_selected = pd.concat([X_selected,X], axis=1)
model.fit(X_selected,y)
y_pred = model.predict(X_selected)
scores.append(r2_score(y,y_pred))
X_selected = result
np_scores = np.array(scores)
new_r2_score = np_scores.max()
if(new_r2_score>old_r2_score):
old_r2_score=new_r2_score
result = pd.concat([result,data[features[np.argmax(np_scores)]]], axis=1)
data = data.drop(features[np.argmax(np_scores)],axis = 1)
selected_features.append(features[np.argmax(np_scores)])
r2_score_features.append(new_r2_score)
features.remove(features[np.argmax(np_scores)])
else:
break
return selected_features,r2_score_features