Machine Learning Workshop
Nicolas Känzig
Email: [email protected]
Workshop Repository: https://github.com/nkaenzig/ml-workshop
Contenido
Modulo 1 Modulo 2 Modulo 3
- Introducción ML - Análisis de datos - Modelos de ML
- Python - Preprocesamiento de datos - Técnicas de evaluación
Machine Learning – Modelos
𝑎𝑖𝑗 ∈ ℝ
SVM (Support Vector Machine)
▪ Supervised Classification
▪ Scikit-Learn:
▪ sklearn.svm.SVC()
▪ Time Complexity (Training):
▪ Linear SVM: O(n)
▪ Non-Linear SVM: O(n2) - O(n3)
K-NN (K-Nearest Neighbors)
▪ Supervised Classification
▪ Scikit-Learn:
▪ sklearn.neighbors.KNeighborsClassifier()
▪ Time Complexity (Training):
▪ O(n)
Random Forest
▪ Supervised Classification & Regression
▪ Scikit-Learn:
▪ sklearn.ensemble.RandomForestClassifier
▪ Time Complexity (Training):
▪ O(nlog(n))
Ridge Regression
▪ Supervised Regression
▪ Scikit-Learn: 𝑓 𝑥, 𝜃 = 𝜃0 + 𝜃1 𝑥1 + 𝜃2 𝑥2 + … + 𝜃𝑝 𝑥𝑝
▪ sklearn.linear_model.Ridge
𝑝
𝜃 ∗ = argmin 𝑓 𝑥, 𝜃 − 𝑦 2
+ 𝛼 𝜃𝑖2
▪ Time Complexity (Training): 𝜃
𝑖=0
▪ O(n) Regularización
K-Means
▪ Unsupervised Clustering
▪ Scikit-Learn:
▪ sklearn.cluster.KMeans()
▪ Time Complexity (Training):
▪ O(n)
Artificial Neural Networks (Deep Learning)
Neurona artificial
Como funciona el Entrenamiento?
▪ Objetivo: 𝜃 ∗ = argmin 𝐿 𝑥, 𝑦, 𝜃
𝜃
▪ Gradient Descent
𝐿(𝜃)
Lmin (𝜃)
𝜃
Machine Learning Frameworks
▪ Los modelos y algoritmos de optimización ya están implementados!
Machine Learning:
Deep Learning
Model Selection
▪ Siempre empieza con el modelo mas simple!
Criterios para seleccionar un modelo
▪ Preguntas:
▪ ¿Es difícil configurar el modelo?
▪ ¿El modelo hace una suposición sobre la distribución de los datos?
▪ ¿El modelo funciona con features categoricos?
▪ ¿El modelo funciona con columnas correlacionadas?
▪ ¿El entrenamiento funciona con “imbalanced” datasets?
▪ ¿Que es la complejidad del modelo?
▪ ¿Qué rápido son las predicciones?
Consejo: Siempre empieza con el modelo mas simple / fácil de usar
Criterios para seleccionar un modelo
Model Feature Selection Class Balancing One-Hot Encoding Non-Linear Complexity
LinearSVM no yes yes no O(n)
SVM('rbf') yes yes yes yes O(n^2) - O(n^3)
K-NN yes yes yes yes O(n)
Random Forest no no no yes O(nlog(n))
▪ Empezar con Random Forest siempre es una buena idea!
▪ Muy fácil para configurar
▪ Muy poco preprocesamiento necesario
▪ Rápido (Entrenamiento & Prediccion)
▪ No hace ninguna suposición sobre la distribución de los datos
▪ ≈50% de los modelos ganadoros en Kaggle usan Random Forest
Machine Learning – Evaluación
Como medir la calidad de las predicciones?
▪ Clasificación:
# 𝑪𝒐𝒓𝒓𝒆𝒄𝒕 𝒑𝒓𝒆𝒅𝒊𝒄𝒕𝒊𝒐𝒏𝒔
𝑨𝒄𝒄𝒖𝒓𝒂𝒄𝒚 =
# 𝑷𝒓𝒆𝒅𝒊𝒄𝒕𝒊𝒐𝒏𝒔
# 𝑻𝒓𝒖𝒆 𝒑𝒐𝒔𝒊𝒕𝒊𝒗𝒆𝒔 # 𝑻𝒓𝒖𝒆 𝒑𝒐𝒔𝒊𝒕𝒊𝒗𝒆𝒔
𝑷𝒓𝒆𝒄𝒊𝒔𝒊𝒐𝒏 = 𝑹𝒆𝒄𝒂𝒍𝒍 =
# 𝑻𝒓𝒖𝒆 𝒑𝒐𝒔𝒊𝒕𝒊𝒗𝒆𝒔 + # 𝑭𝒂𝒍𝒔𝒆 𝑷𝒐𝒔𝒊𝒕𝒊𝒗𝒆𝒔 # 𝑻𝒓𝒖𝒆 𝒑𝒐𝒔𝒊𝒕𝒊𝒗𝒆𝒔 + # 𝑭𝒂𝒍𝒔𝒆 𝑵𝒆𝒈𝒂𝒕𝒊𝒗𝒆𝒔
Problemas con accuracy
▪ Dataset:
▪ 1000 samples de pacientes sin cancer (N)
▪ 5 samples de pacientes con cancer (P)
▪ Modelo solamente diagnostica 1 de los 5 pacientes con cancer
# 𝐶𝑜𝑟𝑟𝑒𝑐𝑡 𝑝𝑟𝑒𝑑𝑖𝑐𝑡𝑖𝑜𝑛𝑠
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 = =?
# 𝑃𝑟𝑒𝑑𝑖𝑐𝑡𝑖𝑜𝑛𝑠
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 = =?
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠 + # 𝐹𝑎𝑙𝑠𝑒 𝑃𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠
𝑅𝑒𝑐𝑎𝑙𝑙 = =?
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠 + # 𝐹𝑎𝑙𝑠𝑒 𝑁𝑒𝑔𝑎𝑡𝑖𝑣𝑒𝑠
Problemas con accuracy
▪ Dataset:
▪ 1000 samples de pacientes sin cancer (N)
▪ 5 samples de pacientes con cancer (P)
▪ Modelo solamente diagnostica 1 de los 5 pacientes con cancer
# 𝐶𝑜𝑟𝑟𝑒𝑐𝑡 𝑝𝑟𝑒𝑑𝑖𝑐𝑡𝑖𝑜𝑛𝑠 1001
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 = = 1005 % = 99.6%
# 𝑃𝑟𝑒𝑑𝑖𝑐𝑡𝑖𝑜𝑛𝑠
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠 1
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 = # 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠 + # 𝐹𝑎𝑙𝑠𝑒 𝑃𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠
= 1+0 % = 100%
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠 1
𝑅𝑒𝑐𝑎𝑙𝑙 = = 1+4 % = 20%
# 𝑇𝑟𝑢𝑒 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒𝑠 + # 𝐹𝑎𝑙𝑠𝑒 𝑁𝑒𝑔𝑎𝑡𝑖𝑣𝑒𝑠
Como medir la calidad de las predicciones?
▪ Regresión:
▪ Mean Absolute Error
𝒏
𝟏
ෝ 𝒊 − 𝒚𝒊 |
𝑴𝑨𝑬 = | 𝒚
𝒏
𝒊=𝟏
▪ Root Mean Squared Error
𝒏
𝟏
𝑹𝑴𝑺𝑬 = 𝒚𝒊 −𝒚𝒊 )𝟐
(ෝ
𝒏
𝒊=𝟏
Training vs. Validation
Training
▪ División de los datos en 3 partes: (Training Set)
▪ Training set (70%)
▪ Validation set (20%)
▪ Test set (10%)
Parameter bad performance Validation
Tuning (Validation set)
good performance
Final
Evaluation
(Test set)
Cross-Validation
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Data loading & preprocessing
# ...
# ...
# Define the model
model = SVC(kernel='linear');
# Train the model
model.fit(x_train, y_train)
# Make predictions
y_predicted = model.predict(x_test, y_test)
# Evaluate
accuracy_score(y_test, y_predicted)