Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconFundamentos del Análisis de Datos con Python
Fundamentos del Análisis de Datos con Python

Chapter 14: Supervised Learning

14.4 Practical Exercises Chapter 14: Supervised Learning

Exercise 1: Implementing Simple Linear Regression

Your first task is to implement simple linear regression from scratch. You'll predict house prices based on the number of bedrooms.

Dataset: You can generate or find a small dataset containing house prices and the number of bedrooms.

import numpy as np
import matplotlib.pyplot as plt

# Sample Data
X = np.array([1, 2, 3, 4, 5])  # Number of bedrooms
y = np.array([100, 150, 200, 250, 300])  # House prices in thousands

# Implement simple linear regression here

Exercise 2: Classify Iris Species Using k-NN

Dataset: Use the famous Iris dataset, which is available through scikit-learn.

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

# Load data
iris = load_iris()

# Train a k-NN classifier

Exercise 3: Decision Tree Classifier for Breast Cancer Data

Dataset: Use the Breast Cancer dataset from scikit-learn.

from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier

# Load data
data = load_breast_cancer()

# Create and train a Decision Tree classifier

Solutions

Exercise 1 Solution:

# Code for simple linear regression
slope = np.sum((X - np.mean(X)) * (y - np.mean(y))) / np.sum((X - np.mean(X)) ** 2)
intercept = np.mean(y) - slope * np.mean(X)

# Predictions
y_pred = slope * X + intercept

Exercise 2 Solution:

# Code for k-NN classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(iris['data'], iris['target'])

Exercise 3 Solution:

# Code for Decision Tree classifier
clf = DecisionTreeClassifier(random_state=42)
clf.fit(data.data, data.target)

Each of these exercises will give you hands-on experience with different supervised learning algorithms. They will help you understand the underlying math, the assumptions involved, and how to evaluate your models. Happy coding!

14.4 Practical Exercises Chapter 14: Supervised Learning

Exercise 1: Implementing Simple Linear Regression

Your first task is to implement simple linear regression from scratch. You'll predict house prices based on the number of bedrooms.

Dataset: You can generate or find a small dataset containing house prices and the number of bedrooms.

import numpy as np
import matplotlib.pyplot as plt

# Sample Data
X = np.array([1, 2, 3, 4, 5])  # Number of bedrooms
y = np.array([100, 150, 200, 250, 300])  # House prices in thousands

# Implement simple linear regression here

Exercise 2: Classify Iris Species Using k-NN

Dataset: Use the famous Iris dataset, which is available through scikit-learn.

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

# Load data
iris = load_iris()

# Train a k-NN classifier

Exercise 3: Decision Tree Classifier for Breast Cancer Data

Dataset: Use the Breast Cancer dataset from scikit-learn.

from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier

# Load data
data = load_breast_cancer()

# Create and train a Decision Tree classifier

Solutions

Exercise 1 Solution:

# Code for simple linear regression
slope = np.sum((X - np.mean(X)) * (y - np.mean(y))) / np.sum((X - np.mean(X)) ** 2)
intercept = np.mean(y) - slope * np.mean(X)

# Predictions
y_pred = slope * X + intercept

Exercise 2 Solution:

# Code for k-NN classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(iris['data'], iris['target'])

Exercise 3 Solution:

# Code for Decision Tree classifier
clf = DecisionTreeClassifier(random_state=42)
clf.fit(data.data, data.target)

Each of these exercises will give you hands-on experience with different supervised learning algorithms. They will help you understand the underlying math, the assumptions involved, and how to evaluate your models. Happy coding!

14.4 Practical Exercises Chapter 14: Supervised Learning

Exercise 1: Implementing Simple Linear Regression

Your first task is to implement simple linear regression from scratch. You'll predict house prices based on the number of bedrooms.

Dataset: You can generate or find a small dataset containing house prices and the number of bedrooms.

import numpy as np
import matplotlib.pyplot as plt

# Sample Data
X = np.array([1, 2, 3, 4, 5])  # Number of bedrooms
y = np.array([100, 150, 200, 250, 300])  # House prices in thousands

# Implement simple linear regression here

Exercise 2: Classify Iris Species Using k-NN

Dataset: Use the famous Iris dataset, which is available through scikit-learn.

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

# Load data
iris = load_iris()

# Train a k-NN classifier

Exercise 3: Decision Tree Classifier for Breast Cancer Data

Dataset: Use the Breast Cancer dataset from scikit-learn.

from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier

# Load data
data = load_breast_cancer()

# Create and train a Decision Tree classifier

Solutions

Exercise 1 Solution:

# Code for simple linear regression
slope = np.sum((X - np.mean(X)) * (y - np.mean(y))) / np.sum((X - np.mean(X)) ** 2)
intercept = np.mean(y) - slope * np.mean(X)

# Predictions
y_pred = slope * X + intercept

Exercise 2 Solution:

# Code for k-NN classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(iris['data'], iris['target'])

Exercise 3 Solution:

# Code for Decision Tree classifier
clf = DecisionTreeClassifier(random_state=42)
clf.fit(data.data, data.target)

Each of these exercises will give you hands-on experience with different supervised learning algorithms. They will help you understand the underlying math, the assumptions involved, and how to evaluate your models. Happy coding!

14.4 Practical Exercises Chapter 14: Supervised Learning

Exercise 1: Implementing Simple Linear Regression

Your first task is to implement simple linear regression from scratch. You'll predict house prices based on the number of bedrooms.

Dataset: You can generate or find a small dataset containing house prices and the number of bedrooms.

import numpy as np
import matplotlib.pyplot as plt

# Sample Data
X = np.array([1, 2, 3, 4, 5])  # Number of bedrooms
y = np.array([100, 150, 200, 250, 300])  # House prices in thousands

# Implement simple linear regression here

Exercise 2: Classify Iris Species Using k-NN

Dataset: Use the famous Iris dataset, which is available through scikit-learn.

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

# Load data
iris = load_iris()

# Train a k-NN classifier

Exercise 3: Decision Tree Classifier for Breast Cancer Data

Dataset: Use the Breast Cancer dataset from scikit-learn.

from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier

# Load data
data = load_breast_cancer()

# Create and train a Decision Tree classifier

Solutions

Exercise 1 Solution:

# Code for simple linear regression
slope = np.sum((X - np.mean(X)) * (y - np.mean(y))) / np.sum((X - np.mean(X)) ** 2)
intercept = np.mean(y) - slope * np.mean(X)

# Predictions
y_pred = slope * X + intercept

Exercise 2 Solution:

# Code for k-NN classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(iris['data'], iris['target'])

Exercise 3 Solution:

# Code for Decision Tree classifier
clf = DecisionTreeClassifier(random_state=42)
clf.fit(data.data, data.target)

Each of these exercises will give you hands-on experience with different supervised learning algorithms. They will help you understand the underlying math, the assumptions involved, and how to evaluate your models. Happy coding!