sklearn tests
This commit is contained in:
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python
|
||||
from sklearn import set_config
|
||||
from sklearn.pipeline import make_pipeline
|
||||
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
||||
from sklearn.impute import SimpleImputer
|
||||
from sklearn.compose import make_column_transformer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
set_config(display='diagram')
|
||||
|
||||
num_proc = make_pipeline(SimpleImputer(strategy='median'), StandardScaler())
|
||||
|
||||
cat_proc = make_pipeline(
|
||||
SimpleImputer(strategy='constant', fill_value='missing'),
|
||||
OneHotEncoder(handle_unknown='ignore'))
|
||||
|
||||
preprocessor = make_column_transformer((num_proc, ('feat1', 'feat3')),
|
||||
(cat_proc, ('feat0', 'feat2')))
|
||||
|
||||
clf = make_pipeline(preprocessor, LogisticRegression())
|
||||
clf
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.inspection import plot_partial_dependence
|
||||
from sklearn.experimental import enable_hist_gradient_boosting # noqa
|
||||
from sklearn.ensemble import HistGradientBoostingRegressor
|
||||
|
||||
n_samples = 500
|
||||
rng = np.random.RandomState(0)
|
||||
X = rng.randn(n_samples, 2)
|
||||
noise = rng.normal(loc=0.0, scale=0.01, size=n_samples)
|
||||
y = (5 * X[:, 0] + np.sin(10 * np.pi * X[:, 0]) - noise)
|
||||
|
||||
gbdt_no_cst = HistGradientBoostingRegressor().fit(X, y)
|
||||
gbdt_cst = HistGradientBoostingRegressor(monotonic_cst=[1, 0]).fit(X, y)
|
||||
|
||||
disp = plot_partial_dependence(
|
||||
gbdt_no_cst, X, features=[0], feature_names=['feature 0'],
|
||||
line_kw={'linewidth': 4, 'label': 'unconstrained'})
|
||||
plot_partial_dependence(gbdt_cst, X, features=[0],
|
||||
line_kw={'linewidth': 4, 'label': 'constrained'}, ax=disp.axes_)
|
||||
disp.axes_[0, 0].plot(X[:, 0], y, 'o', alpha=.5, zorder=-1, label='samples')
|
||||
disp.axes_[0, 0].set_ylim(-3, 3); disp.axes_[0, 0].set_xlim(-1, 1)
|
||||
plt.legend()
|
||||
plt.show()
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from sklearn import datasets
|
||||
from sklearn import svm
|
||||
iris = datasets.load_iris()
|
||||
digits = datasets.load_digits()
|
||||
|
||||
# print(digits.data)
|
||||
# print(digits.target)
|
||||
# print(digits.images[0])
|
||||
|
||||
from sklearn import svm
|
||||
clf = svm.SVC(gamma=0.001, C=100.)
|
||||
clf.fit(digits.data[:-1], digits.target[:-1])
|
||||
|
||||
print(clf.predict(digits.data[-1:]))
|
||||
Reference in New Issue
Block a user