14 Pipeline
In [1]:
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y = True)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.tree import DecisionTreeClassifier
from sklearn.pipeline import Pipeline
pipeline = Pipeline([('pca', PCA(n_components = 4)), ('std', StandardScaler()), ('Decision_tree', DecisionTreeClassifier())], verbose = True)
pipeline.fit(X_train, y_train)[Pipeline] ............... (step 1 of 3) Processing pca, total= 0.0s [Pipeline] ............... (step 2 of 3) Processing std, total= 0.0s [Pipeline] ..... (step 3 of 3) Processing Decision_tree, total= 0.0s
Pipeline(steps=[('pca', PCA(n_components=4)), ('std', StandardScaler()),
('Decision_tree', DecisionTreeClassifier())],
verbose=True)In [2]:
y_pred = pipeline.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix
print("Matrice di confusione:")
print(confusion_matrix(y_test,y_pred))
print("\nClassification Report:")
print(classification_report(y_test,y_pred))Matrice di confusione:
[[11 0 0]
[ 0 16 1]
[ 0 0 10]]
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 11
1 1.00 0.94 0.97 17
2 0.91 1.00 0.95 10
accuracy 0.97 38
macro avg 0.97 0.98 0.97 38
weighted avg 0.98 0.97 0.97 38
In [3]:
# Visualizzo i parametri della pipeline
pipeline.get_params(){'Decision_tree': DecisionTreeClassifier(),
'Decision_tree__ccp_alpha': 0.0,
'Decision_tree__class_weight': None,
'Decision_tree__criterion': 'gini',
'Decision_tree__max_depth': None,
'Decision_tree__max_features': None,
'Decision_tree__max_leaf_nodes': None,
'Decision_tree__min_impurity_decrease': 0.0,
'Decision_tree__min_samples_leaf': 1,
'Decision_tree__min_samples_split': 2,
'Decision_tree__min_weight_fraction_leaf': 0.0,
'Decision_tree__random_state': None,
'Decision_tree__splitter': 'best',
'memory': None,
'pca': PCA(n_components=4),
'pca__copy': True,
'pca__iterated_power': 'auto',
'pca__n_components': 4,
'pca__random_state': None,
'pca__svd_solver': 'auto',
'pca__tol': 0.0,
'pca__whiten': False,
'std': StandardScaler(),
'std__copy': True,
'std__with_mean': True,
'std__with_std': True,
'steps': [('pca', PCA(n_components=4)),
('std', StandardScaler()),
('Decision_tree', DecisionTreeClassifier())],
'verbose': True}