forked from x-datascience-datacamp/datacamp-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
02_pipelines_and_column_transformers/solutions/01-count_encoder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
from collections import Counter | ||
import numpy as np | ||
from sklearn.base import BaseEstimator, TransformerMixin | ||
|
||
class CountEncoder(BaseEstimator, TransformerMixin): | ||
def __init__(self): | ||
pass | ||
|
||
def fit(self, X, y=None): | ||
n_features = X.shape[1] | ||
counters = [] | ||
for k in range(n_features): | ||
counters.append(Counter(X[:, k])) | ||
self.counters_ = counters | ||
return self | ||
|
||
def transform(self, X): | ||
X_t = X.copy() | ||
for x, counter in zip(X_t.T, self.counters_): | ||
# Uses numpy broadcasting | ||
idx = np.nonzero(list(counter.keys()) == x[:, None])[1] | ||
x[:] = np.asarray(list(counter.values()))[idx] | ||
return X_t | ||
|
||
X = np.array([ | ||
[0, 2], | ||
[1, 3], | ||
[1, 1], | ||
[1, 1], | ||
]) | ||
ce = CountEncoder() | ||
print(ce.fit_transform(X)) | ||
|
||
# Let's put this now in a Pipeline | ||
cat_pipeline = Pipeline([ | ||
("imputer", SimpleImputer(strategy='constant', fill_value='missing')), | ||
("count_encoder", CountEncoder()) | ||
]) | ||
|
||
categorical_preprocessing = ColumnTransformer([ | ||
("categorical_preproc", cat_pipeline, cat_col) | ||
]) | ||
|
||
model = Pipeline([ | ||
("categorical_preproc", categorical_preprocessing), | ||
("classifier", RandomForestClassifier(n_estimators=100)) | ||
]) | ||
model.fit(X_train, y_train) | ||
model.score(X_test, y_test) |
3 changes: 3 additions & 0 deletions
3
02_pipelines_and_column_transformers/solutions/01-pandas_fillna_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
X_test_num_imputed = X_test_num.fillna(X_train_num.mean()) | ||
model.score(X_test_num_imputed, y_test) |
23 changes: 23 additions & 0 deletions
23
02_pipelines_and_column_transformers/solutions/01b-full_column_transformer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
from sklearn.preprocessing import OrdinalEncoder | ||
|
||
cat_cols = ['sex', 'embarked', 'pclass'] | ||
num_cols = ['pclass', 'age', 'parch', 'fare'] | ||
|
||
cat_pipeline = Pipeline([ | ||
("imputer", SimpleImputer(strategy='constant', fill_value='missing')), | ||
("ordinal_encoder", OrdinalEncoder()) | ||
]) | ||
|
||
preprocessor = ColumnTransformer([ | ||
("categorical_preproc", cat_pipeline, cat_cols), | ||
("numerical_preproc", SimpleImputer(), num_cols) | ||
|
||
]) | ||
|
||
model = Pipeline([ | ||
("preprocessor", preprocessor), | ||
("classifier", RandomForestClassifier(max_depth=10, n_estimators=500)) | ||
]) | ||
model.fit(X_train, y_train) | ||
model.score(X_test, y_test) |
22 changes: 22 additions & 0 deletions
22
02_pipelines_and_column_transformers/solutions/01c-splitter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
import numpy as np | ||
from sklearn.model_selection import BaseCrossValidator | ||
|
||
class IndexBasedSplitter(BaseCrossValidator): | ||
def __init__(self): | ||
pass | ||
|
||
def get_n_splits(self, X=None, y=None, groups=None): | ||
return len(np.unique(y.index.values)) | ||
|
||
def split(self, X, y, groups=None): | ||
splits_idx = np.unique(y.index.values) | ||
idx = np.arange(len(X)) | ||
for k in splits_idx: | ||
mask = (y.index.values == k) | ||
train_idx = idx[~mask] | ||
test_idx = idx[mask] | ||
yield train_idx, test_idx | ||
|
||
cv = IndexBasedSplitter() | ||
plot_cv_indices(cv, X_df, y_with_provenance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters