Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconData Analysis Foundations with Python
Data Analysis Foundations with Python

Chapter 13: Introduction to Machine Learning

13.4 Practical Exercises Chapter 13: Introduction to Machine Learning

Here are some practical exercises to reinforce your understanding of the concepts covered in Chapter 13.

Exercise 13.1: Types of Machine Learning

Problem:

Classify the following scenarios as supervised, unsupervised, or reinforcement learning tasks:

  1. Teaching a drone to navigate through an obstacle course.
  2. Grouping articles based on their content.
  3. Predicting the weather for the next week.
  4. Recommending music based on a user's previous listening habits.

Solution:

  1. Reinforcement Learning
  2. Unsupervised Learning
  3. Supervised Learning
  4. Reinforcement Learning or Supervised Learning (depending on how it's implemented)

Exercise 13.2: Implement a Basic Algorithm

Problem:

Implement a basic K-Nearest Neighbors (KNN) algorithm to classify the following dataset into two classes:

# Data
X = [[2, 3], [4, 1], [1, 4], [4, 4], [2, 1], [3, 2]]
y = [0, 1, 0, 1, 0, 1]

Solution:

from sklearn.neighbors import KNeighborsClassifier

# Create a KNN classifier instance
knn = KNeighborsClassifier(n_neighbors=3)

# Fit the model
knn.fit(X, y)

# Predict a new data point
new_point = [[3, 3]]
prediction = knn.predict(new_point)
print(f'The predicted class of the point {new_point} is {prediction[0]}')

Exercise 13.3: Model Evaluation

Problem:

Evaluate a simple RandomForest model using accuracy and F1-score metrics on the following dataset:

# Data
X_train = [[1, 2], [3, 4], [5, 6], [7, 8]]
y_train = [0, 1, 1, 0]
X_test = [[2, 3], [4, 5]]
y_test = [0, 1]

Solution:

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score

# Initialize classifier
clf = RandomForestClassifier()

# Fit the model
clf.fit(X_train, y_train)

# Make predictions
y_pred = clf.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

print(f'Accuracy: {accuracy}')
print(f'F1 Score: {f1}')

Feel free to dive into these exercises, and don't hesitate to explore beyond them! Happy coding!

13.4 Practical Exercises Chapter 13: Introduction to Machine Learning

Here are some practical exercises to reinforce your understanding of the concepts covered in Chapter 13.

Exercise 13.1: Types of Machine Learning

Problem:

Classify the following scenarios as supervised, unsupervised, or reinforcement learning tasks:

  1. Teaching a drone to navigate through an obstacle course.
  2. Grouping articles based on their content.
  3. Predicting the weather for the next week.
  4. Recommending music based on a user's previous listening habits.

Solution:

  1. Reinforcement Learning
  2. Unsupervised Learning
  3. Supervised Learning
  4. Reinforcement Learning or Supervised Learning (depending on how it's implemented)

Exercise 13.2: Implement a Basic Algorithm

Problem:

Implement a basic K-Nearest Neighbors (KNN) algorithm to classify the following dataset into two classes:

# Data
X = [[2, 3], [4, 1], [1, 4], [4, 4], [2, 1], [3, 2]]
y = [0, 1, 0, 1, 0, 1]

Solution:

from sklearn.neighbors import KNeighborsClassifier

# Create a KNN classifier instance
knn = KNeighborsClassifier(n_neighbors=3)

# Fit the model
knn.fit(X, y)

# Predict a new data point
new_point = [[3, 3]]
prediction = knn.predict(new_point)
print(f'The predicted class of the point {new_point} is {prediction[0]}')

Exercise 13.3: Model Evaluation

Problem:

Evaluate a simple RandomForest model using accuracy and F1-score metrics on the following dataset:

# Data
X_train = [[1, 2], [3, 4], [5, 6], [7, 8]]
y_train = [0, 1, 1, 0]
X_test = [[2, 3], [4, 5]]
y_test = [0, 1]

Solution:

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score

# Initialize classifier
clf = RandomForestClassifier()

# Fit the model
clf.fit(X_train, y_train)

# Make predictions
y_pred = clf.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

print(f'Accuracy: {accuracy}')
print(f'F1 Score: {f1}')

Feel free to dive into these exercises, and don't hesitate to explore beyond them! Happy coding!

13.4 Practical Exercises Chapter 13: Introduction to Machine Learning

Here are some practical exercises to reinforce your understanding of the concepts covered in Chapter 13.

Exercise 13.1: Types of Machine Learning

Problem:

Classify the following scenarios as supervised, unsupervised, or reinforcement learning tasks:

  1. Teaching a drone to navigate through an obstacle course.
  2. Grouping articles based on their content.
  3. Predicting the weather for the next week.
  4. Recommending music based on a user's previous listening habits.

Solution:

  1. Reinforcement Learning
  2. Unsupervised Learning
  3. Supervised Learning
  4. Reinforcement Learning or Supervised Learning (depending on how it's implemented)

Exercise 13.2: Implement a Basic Algorithm

Problem:

Implement a basic K-Nearest Neighbors (KNN) algorithm to classify the following dataset into two classes:

# Data
X = [[2, 3], [4, 1], [1, 4], [4, 4], [2, 1], [3, 2]]
y = [0, 1, 0, 1, 0, 1]

Solution:

from sklearn.neighbors import KNeighborsClassifier

# Create a KNN classifier instance
knn = KNeighborsClassifier(n_neighbors=3)

# Fit the model
knn.fit(X, y)

# Predict a new data point
new_point = [[3, 3]]
prediction = knn.predict(new_point)
print(f'The predicted class of the point {new_point} is {prediction[0]}')

Exercise 13.3: Model Evaluation

Problem:

Evaluate a simple RandomForest model using accuracy and F1-score metrics on the following dataset:

# Data
X_train = [[1, 2], [3, 4], [5, 6], [7, 8]]
y_train = [0, 1, 1, 0]
X_test = [[2, 3], [4, 5]]
y_test = [0, 1]

Solution:

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score

# Initialize classifier
clf = RandomForestClassifier()

# Fit the model
clf.fit(X_train, y_train)

# Make predictions
y_pred = clf.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

print(f'Accuracy: {accuracy}')
print(f'F1 Score: {f1}')

Feel free to dive into these exercises, and don't hesitate to explore beyond them! Happy coding!

13.4 Practical Exercises Chapter 13: Introduction to Machine Learning

Here are some practical exercises to reinforce your understanding of the concepts covered in Chapter 13.

Exercise 13.1: Types of Machine Learning

Problem:

Classify the following scenarios as supervised, unsupervised, or reinforcement learning tasks:

  1. Teaching a drone to navigate through an obstacle course.
  2. Grouping articles based on their content.
  3. Predicting the weather for the next week.
  4. Recommending music based on a user's previous listening habits.

Solution:

  1. Reinforcement Learning
  2. Unsupervised Learning
  3. Supervised Learning
  4. Reinforcement Learning or Supervised Learning (depending on how it's implemented)

Exercise 13.2: Implement a Basic Algorithm

Problem:

Implement a basic K-Nearest Neighbors (KNN) algorithm to classify the following dataset into two classes:

# Data
X = [[2, 3], [4, 1], [1, 4], [4, 4], [2, 1], [3, 2]]
y = [0, 1, 0, 1, 0, 1]

Solution:

from sklearn.neighbors import KNeighborsClassifier

# Create a KNN classifier instance
knn = KNeighborsClassifier(n_neighbors=3)

# Fit the model
knn.fit(X, y)

# Predict a new data point
new_point = [[3, 3]]
prediction = knn.predict(new_point)
print(f'The predicted class of the point {new_point} is {prediction[0]}')

Exercise 13.3: Model Evaluation

Problem:

Evaluate a simple RandomForest model using accuracy and F1-score metrics on the following dataset:

# Data
X_train = [[1, 2], [3, 4], [5, 6], [7, 8]]
y_train = [0, 1, 1, 0]
X_test = [[2, 3], [4, 5]]
y_test = [0, 1]

Solution:

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score

# Initialize classifier
clf = RandomForestClassifier()

# Fit the model
clf.fit(X_train, y_train)

# Make predictions
y_pred = clf.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

print(f'Accuracy: {accuracy}')
print(f'F1 Score: {f1}')

Feel free to dive into these exercises, and don't hesitate to explore beyond them! Happy coding!