Menu iconMenu icon
Héroe del Aprendizaje Automático

Capítulo 4: Técnicas de Aprendizaje Supervisado

Ejercicios Prácticos Capítulo 4

Ejercicio 1: Regresión Lineal

Tarea: Tienes el siguiente conjunto de datos que contiene información sobre los precios de las casas. Utiliza regresión lineal simple para predecir el precio de la casa basado en el tamaño de la casa.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Ejercicio 2: Regresión Polinómica

Tarea: Se te proporciona un conjunto de datos con una relación no lineal entre los años de experiencia y el salario. Utiliza regresión polinómica para modelar esta relación.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Ejercicio 3: Clasificación con SVM

Tarea: Utiliza un clasificador SVM para predecir si un paciente tiene enfermedad cardíaca basándote en dos características: edad y nivel de colesterol. Entrena el modelo utilizando el siguiente conjunto de datos:

Solución:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Ejercicio 4: Cálculo de Precisión y Exhaustividad

Tarea: Estás trabajando en un problema de clasificación con las siguientes etiquetas verdaderas y etiquetas predichas:

Etiquetas Verdaderas: [1, 0, 1, 1, 0, 1, 0, 0]

Etiquetas Predichas: [1, 0, 1, 0, 0, 1, 0, 1]

Calcula la precisión y la exhaustividad (recall) para la clase positiva (1).

Solución:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Ejercicio 5: Cálculo de AUC-ROC

Tarea: Se te proporcionan las probabilidades predichas de un modelo para la clase positiva (enfermedad cardíaca) de la siguiente manera:

Probabilidades Predichas: [0.1, 0.4, 0.8, 0.6, 0.3]

Etiquetas Verdaderas: [0, 0, 1, 1, 0]

Calcula el AUC-ROC score y traza la curva ROC.

Solución:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Ejercicio 6: Sintonización de Hiperparámetros con Random Forest

Tarea: Estás utilizando un clasificador Random Forest para clasificar casos de cáncer de mama. Realiza una búsqueda aleatoria para sintonizar los hiperparámetros, como el número de estimadores (n_estimators) y la profundidad máxima (max_depth), utilizando los siguientes rangos:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solución:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params_)
print("Best cross-validation accuracy:", random_search.best_score_)

Estos ejercicios prácticos ayudan a reforzar los conceptos clave en aprendizaje supervisado, incluyendo regresión, clasificación, métricas de evaluación y sintonización de hiperparámetros.

Ejercicios Prácticos Capítulo 4

Ejercicio 1: Regresión Lineal

Tarea: Tienes el siguiente conjunto de datos que contiene información sobre los precios de las casas. Utiliza regresión lineal simple para predecir el precio de la casa basado en el tamaño de la casa.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Ejercicio 2: Regresión Polinómica

Tarea: Se te proporciona un conjunto de datos con una relación no lineal entre los años de experiencia y el salario. Utiliza regresión polinómica para modelar esta relación.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Ejercicio 3: Clasificación con SVM

Tarea: Utiliza un clasificador SVM para predecir si un paciente tiene enfermedad cardíaca basándote en dos características: edad y nivel de colesterol. Entrena el modelo utilizando el siguiente conjunto de datos:

Solución:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Ejercicio 4: Cálculo de Precisión y Exhaustividad

Tarea: Estás trabajando en un problema de clasificación con las siguientes etiquetas verdaderas y etiquetas predichas:

Etiquetas Verdaderas: [1, 0, 1, 1, 0, 1, 0, 0]

Etiquetas Predichas: [1, 0, 1, 0, 0, 1, 0, 1]

Calcula la precisión y la exhaustividad (recall) para la clase positiva (1).

Solución:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Ejercicio 5: Cálculo de AUC-ROC

Tarea: Se te proporcionan las probabilidades predichas de un modelo para la clase positiva (enfermedad cardíaca) de la siguiente manera:

Probabilidades Predichas: [0.1, 0.4, 0.8, 0.6, 0.3]

Etiquetas Verdaderas: [0, 0, 1, 1, 0]

Calcula el AUC-ROC score y traza la curva ROC.

Solución:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Ejercicio 6: Sintonización de Hiperparámetros con Random Forest

Tarea: Estás utilizando un clasificador Random Forest para clasificar casos de cáncer de mama. Realiza una búsqueda aleatoria para sintonizar los hiperparámetros, como el número de estimadores (n_estimators) y la profundidad máxima (max_depth), utilizando los siguientes rangos:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solución:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params_)
print("Best cross-validation accuracy:", random_search.best_score_)

Estos ejercicios prácticos ayudan a reforzar los conceptos clave en aprendizaje supervisado, incluyendo regresión, clasificación, métricas de evaluación y sintonización de hiperparámetros.

Ejercicios Prácticos Capítulo 4

Ejercicio 1: Regresión Lineal

Tarea: Tienes el siguiente conjunto de datos que contiene información sobre los precios de las casas. Utiliza regresión lineal simple para predecir el precio de la casa basado en el tamaño de la casa.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Ejercicio 2: Regresión Polinómica

Tarea: Se te proporciona un conjunto de datos con una relación no lineal entre los años de experiencia y el salario. Utiliza regresión polinómica para modelar esta relación.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Ejercicio 3: Clasificación con SVM

Tarea: Utiliza un clasificador SVM para predecir si un paciente tiene enfermedad cardíaca basándote en dos características: edad y nivel de colesterol. Entrena el modelo utilizando el siguiente conjunto de datos:

Solución:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Ejercicio 4: Cálculo de Precisión y Exhaustividad

Tarea: Estás trabajando en un problema de clasificación con las siguientes etiquetas verdaderas y etiquetas predichas:

Etiquetas Verdaderas: [1, 0, 1, 1, 0, 1, 0, 0]

Etiquetas Predichas: [1, 0, 1, 0, 0, 1, 0, 1]

Calcula la precisión y la exhaustividad (recall) para la clase positiva (1).

Solución:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Ejercicio 5: Cálculo de AUC-ROC

Tarea: Se te proporcionan las probabilidades predichas de un modelo para la clase positiva (enfermedad cardíaca) de la siguiente manera:

Probabilidades Predichas: [0.1, 0.4, 0.8, 0.6, 0.3]

Etiquetas Verdaderas: [0, 0, 1, 1, 0]

Calcula el AUC-ROC score y traza la curva ROC.

Solución:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Ejercicio 6: Sintonización de Hiperparámetros con Random Forest

Tarea: Estás utilizando un clasificador Random Forest para clasificar casos de cáncer de mama. Realiza una búsqueda aleatoria para sintonizar los hiperparámetros, como el número de estimadores (n_estimators) y la profundidad máxima (max_depth), utilizando los siguientes rangos:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solución:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params_)
print("Best cross-validation accuracy:", random_search.best_score_)

Estos ejercicios prácticos ayudan a reforzar los conceptos clave en aprendizaje supervisado, incluyendo regresión, clasificación, métricas de evaluación y sintonización de hiperparámetros.

Ejercicios Prácticos Capítulo 4

Ejercicio 1: Regresión Lineal

Tarea: Tienes el siguiente conjunto de datos que contiene información sobre los precios de las casas. Utiliza regresión lineal simple para predecir el precio de la casa basado en el tamaño de la casa.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Data: House size (X) and price (y)
X = np.array([800, 1000, 1200, 1500, 1800]).reshape(-1, 1)
y = np.array([150000, 180000, 210000, 250000, 300000])

# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict for new house sizes
X_new = np.array([2000, 2200]).reshape(-1, 1)
y_pred = model.predict(X_new)

# Plotting the data and the regression line
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price ($)")
plt.legend()
plt.show()

print("Predicted prices for new house sizes:", y_pred)

Ejercicio 2: Regresión Polinómica

Tarea: Se te proporciona un conjunto de datos con una relación no lineal entre los años de experiencia y el salario. Utiliza regresión polinómica para modelar esta relación.

Solución:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Data: Years of experience (X) and salary (y)
X = np.array([1, 2, 3, 5, 7]).reshape(-1, 1)
y = np.array([40000, 45000, 50000, 70000, 85000])

# Create polynomial features (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

# Train the polynomial regression model
model = LinearRegression()
model.fit(X_poly, y)

# Predict for new years of experience
X_new = np.array([4, 6]).reshape(-1, 1)
X_new_poly = poly.transform(X_new)
y_pred = model.predict(X_new_poly)

# Plot the data and the polynomial regression curve
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X_poly), color='red', label='Polynomial regression curve')
plt.xlabel("Years of Experience")
plt.ylabel("Salary ($)")
plt.legend()
plt.show()

print("Predicted salaries for new years of experience:", y_pred)

Ejercicio 3: Clasificación con SVM

Tarea: Utiliza un clasificador SVM para predecir si un paciente tiene enfermedad cardíaca basándote en dos características: edad y nivel de colesterol. Entrena el modelo utilizando el siguiente conjunto de datos:

Solución:

import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Data: Age, cholesterol, and heart disease label
X = np.array([[45, 200], [50, 220], [55, 240], [60, 210], [65, 280]])
y = np.array([0, 1, 1, 0, 1])

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the SVM classifier
model = SVC(kernel='linear')
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

print("Predicted heart disease labels for test set:", y_pred)

Ejercicio 4: Cálculo de Precisión y Exhaustividad

Tarea: Estás trabajando en un problema de clasificación con las siguientes etiquetas verdaderas y etiquetas predichas:

Etiquetas Verdaderas: [1, 0, 1, 1, 0, 1, 0, 0]

Etiquetas Predichas: [1, 0, 1, 0, 0, 1, 0, 1]

Calcula la precisión y la exhaustividad (recall) para la clase positiva (1).

Solución:

from sklearn.metrics import precision_score, recall_score

# True and predicted labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 0, 1]

# Calculate precision and recall
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")

Ejercicio 5: Cálculo de AUC-ROC

Tarea: Se te proporcionan las probabilidades predichas de un modelo para la clase positiva (enfermedad cardíaca) de la siguiente manera:

Probabilidades Predichas: [0.1, 0.4, 0.8, 0.6, 0.3]

Etiquetas Verdaderas: [0, 0, 1, 1, 0]

Calcula el AUC-ROC score y traza la curva ROC.

Solución:

from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Predicted probabilities and true labels
y_probs = [0.1, 0.4, 0.8, 0.6, 0.3]
y_true = [0, 0, 1, 1, 0]

# Calculate AUC-ROC score
auc_score = roc_auc_score(y_true, y_probs)
print(f"AUC-ROC Score: {auc_score:.2f}")

# Calculate ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_probs)

# Plot ROC curve
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], 'k--')  # Random classifier
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()

Ejercicio 6: Sintonización de Hiperparámetros con Random Forest

Tarea: Estás utilizando un clasificador Random Forest para clasificar casos de cáncer de mama. Realiza una búsqueda aleatoria para sintonizar los hiperparámetros, como el número de estimadores (n_estimators) y la profundidad máxima (max_depth), utilizando los siguientes rangos:

  • n_estimators: 50, 100, 150
  • max_depth: 10, 20, 30, None

Solución:

from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

# Load the breast cancer dataset
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)

# Define the hyperparameter grid
param_dist = {
    'n_estimators': [50, 100, 150],
    'max_depth': [10, 20, 30, None]
}

# Initialize the Random Forest model
rf = RandomForestClassifier()

# Perform randomized search
random_search = RandomizedSearchCV(rf, param_distributions=param_dist, n_iter=5, cv=5, random_state=42)
random_search.fit(X_train, y_train)

# Print the best parameters and corresponding score
print("Best parameters found:", random_search.best_params_)
print("Best cross-validation accuracy:", random_search.best_score_)

Estos ejercicios prácticos ayudan a reforzar los conceptos clave en aprendizaje supervisado, incluyendo regresión, clasificación, métricas de evaluación y sintonización de hiperparámetros.