Created
December 10, 2019 07:24
-
-
Save Dpananos/71c609a80687f865c05119bf2183be29 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
from sklearn.svm import SVR | |
from sklearn.pipeline import Pipeline | |
from sklearn.compose import TransformedTargetRegressor | |
from scipy.stats import beta | |
from scipy.special import expit, logit | |
np.random.seed(0) | |
# Generate features | |
X = np.random.normal(size=(1000, 5)) | |
# Combine them and ensure they are between 0 and 1 | |
betas = np.random.normal(0, 1, size=5) | |
eta = 1 / (1 + np.exp(-X @ betas)) | |
# Make outcomes | |
y = beta(eta * 50, (1 - eta) * 50).rvs() | |
plt.scatter(eta, y) | |
no_trans_pipe = Pipeline([("reg", SVR(gamma="scale"))]) | |
trans_pipe = Pipeline( | |
[ | |
( | |
"reg", | |
TransformedTargetRegressor( | |
SVR(gamma="scale"), func=logit, inverse_func=expit | |
), | |
) | |
] | |
) | |
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, train_size=0.5) | |
# Uh oh, predictions out of bounds! | |
(no_trans_pipe.fit(Xtrain, ytrain).predict(Xtest) < 0).any() | |
# Predictions stay constrained! | |
(trans_pipe.fit(Xtrain, ytrain).predict(Xtest) < 0).any() | ( | |
trans_pipe.fit(Xtrain, ytrain).predict(Xtest) > 1 | |
).any() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment