Created
August 28, 2021 20:36
-
-
Save buswedg/6fbcd28e9c5d626cd5a19756a2cdbc14 to your computer and use it in GitHub Desktop.
building_feature_engineering_pipelines\numeric_prediction_using_pipelines
This file contains hidden or 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
import numpy as np | |
import sklearn.base | |
from sklearn import metrics | |
class transform_predict(sklearn.base.BaseEstimator, sklearn.base.TransformerMixin): | |
def __init__(self, clf: sklearn.base.BaseEstimator): | |
self.clf = clf | |
def fit(self, *args, **kwargs): | |
self.clf.fit(*args, **kwargs) | |
return self | |
def transform(self, X: np.ndarray, **transform_params): | |
pred = self.clf.predict(X) | |
return pred.reshape(-1, 1) if len(pred.shape) == 1 else pred | |
class transform_predict_proba(sklearn.base.BaseEstimator, sklearn.base.TransformerMixin): | |
def __init__(self, clf: sklearn.base.ClassifierMixin, drop: bool = True): | |
self.clf = clf | |
self.drop = drop | |
def fit(self, *args, **kwargs): | |
self.clf.fit(*args, **kwargs) | |
return self | |
def transform(self, X: np.ndarray, **transform_params): | |
pred = self.clf.predict_proba(X) | |
return pred[:, 1:] if self.drop else pred | |
def get_regression_metrics(y_true, y_pred): | |
print('mean_squared_error', np.round(metrics.mean_squared_error(y_true, y_pred), 4)) | |
print('explained_variance_score', np.round(metrics.explained_variance_score(y_true, y_pred), 4)) | |
print('mean_absolute_error', np.round(metrics.mean_absolute_error(y_true, y_pred), 4)) | |
print('mean_squared_error', np.round(metrics.mean_squared_error(y_true, y_pred), 4)) | |
print('median_absolute_error', np.round(metrics.median_absolute_error(y_true, y_pred), 4)) | |
print('r2_score', np.round(metrics.r2_score(y_true, y_pred), 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment