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.
8 changes: 7 additions & 1 deletion q01_plot_corr/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# %load q01_plot_corr/build.py
# Default imports
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import yticks, xticks, subplots, set_cmap
plt.switch_backend('agg')
data = pd.read_csv('data/house_prices_multivariate.csv')
Expand All @@ -9,8 +11,12 @@
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

plot_corr(data)


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.
13 changes: 13 additions & 0 deletions q02_best_k_features/build.py
Original file line number Diff line number Diff line change
@@ -1,6 +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')

Expand All @@ -9,4 +11,15 @@


# Write your solution here:
def percentile_k_features(df, k=20):
predictors = df.drop('SalePrice', axis=1)
target = df['SalePrice']

fs = SelectPercentile(f_regression, k)
features = fs.fit_transform(predictors, target)
features_by_score = [predictors.columns[i] for i in np.argsort(fs.scores_)[::-1]]

return features_by_score[:7]

percentile_k_features(data, 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.
14 changes: 14 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,17 @@


# Your solution code here
def rf_rfe(df):
X = df.drop('SalePrice', axis=1)
y = df['SalePrice']

model = RandomForestClassifier()
rfe = RFE(model, n_features_to_select=len(X.columns)/2)
fit = rfe.fit(X, y)

features = X.columns[fit.support_]
return list(features)

rf_rfe(data)


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: 13 additions & 0 deletions q04_select_from_model/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %load q04_select_from_model/build.py
# Default imports
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier
Expand All @@ -8,3 +9,15 @@


# Your solution code here
def select_from_model(df):
X = df.drop('SalePrice', axis=1)
y = df['SalePrice']

model = RandomForestClassifier()
sfm = SelectFromModel(model)
sfm.fit_transform(X, y)

return list(X.columns[sfm.get_support()])

select_from_model(data)

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 36 additions & 0 deletions q05_forward_selected/build.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
# %load q05_forward_selected/build.py
# Default imports
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split as tts
from sklearn.metrics import mean_squared_error, r2_score

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

model = LinearRegression()


# Your solution code here
def forward_selected(df, LinReg):
features = df.drop('SalePrice', axis=1)
target = df['SalePrice']
feature_list = list(features.columns)
best_features = []
best_scores = []

while len(feature_list) > 0:
scores_with_features = []

for feature in feature_list:
best_features.append(feature)

LinReg.fit(features[best_features], target)
rsquare = LinReg.score(features[best_features], target)
scores_with_features.append((rsquare, feature))

best_features.remove(feature)

scores_with_features.sort()
best_score, best_candidate = scores_with_features.pop()

feature_list.remove(best_candidate)

best_features.append(best_candidate)
best_scores.append(best_score)

return best_features, best_scores

forward_selected(data, model)


Binary file not shown.
Binary file not shown.