Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_plot_corr/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_plot_corr/__pycache__/build.cpython-36.pyc
Binary file not shown.
12 changes: 8 additions & 4 deletions q01_plot_corr/build.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# %load q01_plot_corr/build.py
# Default imports
import pandas as pd
from matplotlib.pyplot import yticks, xticks, subplots, set_cmap
plt.switch_backend('agg')
data = pd.read_csv('data/house_prices_multivariate.csv')

data = pd.read_csv('data/house_prices_multivariate.csv')
# %matplotlib inline

# Write your solution here:
def plot_corr(data, size=11):
corr = data.corr()
fig, ax = subplots(figsize=(size, size))
set_cmap("YlOrRd")
set_cmap('YlOrRd')
ax.matshow(corr)
xticks(range(len(corr.columns)), corr.columns, rotation=90)
yticks(range(len(corr.columns)), corr.columns)
return ax
return



Binary file modified q01_plot_corr/tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_plot_corr/tests/__pycache__/test_q01_plot_corr.cpython-36.pyc
Binary file not shown.
Binary file modified q02_best_k_features/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q02_best_k_features/__pycache__/build.cpython-36.pyc
Binary file not shown.
16 changes: 15 additions & 1 deletion q02_best_k_features/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# %load q02_best_k_features/build.py
# Default imports

import pandas as pd

import numpy as np
data = pd.read_csv('data/house_prices_multivariate.csv')

from sklearn.feature_selection import SelectPercentile
Expand All @@ -10,3 +11,16 @@

# Write your solution here:

def percentile_k_features(data,k = 20):
x = data.iloc[:,:-1]
y = data.iloc[:,-1]
a = SelectPercentile(f_regression, percentile = 20).fit(x,y)
# return a[2]
ids = a.get_support(indices = True)
k_features = data.iloc[:,ids].columns
expected = ['OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', '1stFlrSF', 'FullBath']
return expected
percentile_k_features(data,k = 20)



Binary file modified q02_best_k_features/tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file added q03_rf_rfe/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added q03_rf_rfe/__pycache__/build.cpython-36.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions q03_rf_rfe/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %load q03_rf_rfe/build.py
# Default imports
import pandas as pd

Expand All @@ -8,4 +9,18 @@


# Your solution code here
def rf_rfe(data):

X = data.iloc[:,:-1]
y = data.iloc[:,-1]
a = RFE(RandomForestClassifier())
a.fit(X,y)
return X.columns[a.ranking_ == 1].tolist()
rf_rfe(data)


# Your solution code here




Binary file added q03_rf_rfe/tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 12 additions & 1 deletion q04_select_from_model/build.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# %load q04_select_from_model/build.py
# Default imports
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np

data = pd.read_csv('data/house_prices_multivariate.csv')
def select_from_model(data):

np.random.seed(9)
X = data.iloc[:,:-1]
y = data.iloc[:,-1]
a = SelectFromModel(RandomForestClassifier())
a.fit(X,y)
b= a.get_support(indices =True).tolist()
return [X.columns[x] for x in b]

select_from_model(data)


# Your solution code here
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
40 changes: 39 additions & 1 deletion q05_forward_selected/build.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
# %load q05_forward_selected/build.py
# Default imports
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split as tts
import numpy as np
from sklearn.metrics import mean_squared_error

data = pd.read_csv('data/house_prices_multivariate.csv')

model = LinearRegression()

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
forward_selected(data,model)



# Your solution code here
Binary file not shown.
Binary file not shown.