# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split, StratifiedKFold

# df = pd.read_csv('https://archive.ics.uci.edu/ml/'
#                  'machine-learning-databases'
#                  '/breast-cancer-wisconsin/wdbc.data',header=None)
df = pd.read_csv('wdbc.data', header=None)
df

# +
X = df.loc[:,2:].values
y = df.loc[:,1].values

le = LabelEncoder()
y = le.fit_transform(y)
print(f"New classes {le.transform(le.classes_)} are equivalente to old clases {le.classes_}, respectively")

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0, stratify=y)

# +
# Posibilities for a pipeline
# ----------------------------From here---------------------------
# Scaling (sc.fit, sc.transform)
#     normalization
#     standarization
#         StandardScaler
# Dimensionality reduction (tr.fit, tr.transform)
#     feature selection
#         RandomForestClassifier.feature_importances_
#         SBS (sequential backward selection)
#     feature extraction
#         unsupervised feature extraction
#             PCA (principal component analysis)
#             KernelPCA (kernel principal component analysis)
#         supervised feature extraction
#             LDA (linear discriminant analysis)
# Learning algorithm (model.fit)
# Predictive model (model.predict)
# --------------------------To here------------------------------
#


# chain the StandardScaler, PCA, and LogisticRegression objects in a pipeline
from sklearn.preprocessing import StandardScaler # for standarization
from sklearn.decomposition import PCA # for unsupervised feature extraction
from sklearn.linear_model import LogisticRegression # for learning and predicting
from sklearn.pipeline import make_pipeline # for making a pipeline

pipe_lr = make_pipeline(StandardScaler(), # transformer (fit->transform)
                        PCA(n_components=2), # transformer (fit->transform)
                        LogisticRegression(random_state=1,solver='lbfgs'), verbose=True) # estimator (fit->predict)

pipe_lr.fit(X_train, y_train)

y_pred = pipe_lr.predict(X_test)

print('Test Accuracy: %.3f' % pipe_lr.score(X_test, y_test))
# -

kfold = StratifiedKFold(n_splits=10).split(X_train, y_train)
scores = []
for k, (train, test) in enumerate(kfold):
    pipe_lr.fit(X_train[train], y_train[train]) # learn with the 'training' data set
    score = pipe_lr.score(X_train[test], y_train[test]) # assesses performance with the 'validation' dataset
    scores.append(score)
    print('Fold: %2d, Class dist.: %s, Acc: %.3f' % (k+1,
                                                     np.bincount(y_train[train]), score))
print('\nCV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores))) # 'test' dataset is left for checking the model afterwards



StarcoderdataJupyterScriptsDedupFiltered