Python ML Basics

Introduction to machine learning concepts and Python setup

Back to Tutorial List

1. Introduction to Machine Learning

Machine learning is a branch of artificial intelligence that enables computers to learn from data without being explicitly programmed. It involves developing algorithmss that can identify patterns in data and make predictions or decisions based on those patterns.

1.1 Key Concepts in Machine Learning

  • Data: The foundation of machine learning, including features (inputs) and labels (outputs for supervised learning).
  • Model: A mathematical representation that learns patterns from data.
  • Training: The process of feeding data to a model so it can learn patterns.
  • Testing/Evaluation: Assessing how well a model performs on new, unseen data.
  • Prediction/Inference: Using a trained model to make predictions on new data.

1.2 Types of Machine Learning

  • Supervised Learning: Models learn from labeled data, where each training example has both features and a corresponding label.
  • Unsupervised Learning: Models learn from unlabeled data, identifying patterns and structures without explicit guidance.
  • Reinforcement Learning: Models learn through trial and error, receiving rewards or penalties based on their actions.
  • Semi-supervised Learning: Combines labeled and unlabeled data for training.
  • Transfer Learning: Applying knowledge gained from one task to a different but related task.

2. Python Ecosystem for Machine Learning

Python has become the most popular programming language for machine learning due to its simplicity, versatility, and rich ecosystem of libraries and frameworks. Here are the key libraries that form the foundation of machine learning in Python:

2.1 Essential Libraries

2.1.1 NumPy

NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays.

2.1.2 Pandas

Pandas is a powerful library for data manipulation and analysis. It provides data structures like DataFrames that make it easy to work with structured data, including functions for reading and writing data from various file formats.

2.1.3 Matplotlib

Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. It is widely used for data visualization in machine learning.

2.1.4 Scikit-learn

Scikit-learn is a comprehensive machine learning library that provides simple and efficient tools for data mining and data analysis. It includes implementations of many machine learning algorithmss and tools for model evaluation and selection.

2.1.5 TensorFlow and PyTorch

TensorFlow and PyTorch are deep learning frameworks that provide tools for building and training neural networks. They are widely used for complex machine learning tasks such as image recognition, natural language processing, and reinforcement learning.

3. Setting Up the Development Environment

Before you start working with machine learning in Python, you need to set up a proper development environment. Here's how to get started:

3.1 Installing Python

First, ensure you have Python installed on your system. We recommend using Python 3.7 or later for machine learning work.

# Check Python version
python --version

# If Python is not installed, download and install from https://www.python.org/downloads/

3.2 Creating a Virtual Environment

It's best practice to create a virtual environment for your machine learning projects to avoid dependency conflicts.

# Create a virtual environment
python -m venv ml_env

# Activate the virtual environment
# On Windows
ml_env\Scripts\activate
# On macOS and Linux
source ml_env/bin/activate

3.3 Installing Essential Libraries

Once your virtual environment is activated, install the essential libraries for machine learning:

# Install NumPy, Pandas, Matplotlib, and Scikit-learn
pip install numpy pandas matplotlib scikit-learn

# Install TensorFlow (optional for deep learning)
pip install tensorflow

# Install PyTorch (optional for deep learning)
pip install torch torchvision

# Verify installations
pip list

4. First Machine Learning Example

Let's create a simple machine learning example using Scikit-learn to demonstrate the basic workflow. We'll use the Iris dataset, which is a classic dataset for classification tasks.

# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

# Load the Iris dataset
iris = load_iris()

# Create a DataFrame for better visualization
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['target'] = iris.target
df['target_names'] = [iris.target_names[i] for i in iris.target]

# Display the first few rows
print("First 5 rows of the dataset:")
print(df.head())

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42
)

# Create and train a K-Nearest Neighbors classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)

# Make predictions on the test set
y_pred = knn.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"\nModel accuracy: {accuracy:.2f}")

# Example prediction
new_sample = [[5.1, 3.5, 1.4, 0.2]]  # Sepal length, sepal width, petal length, petal width
prediction = knn.predict(new_sample)
predicted_class = iris.target_names[prediction[0]]
print(f"\nPrediction for new sample {new_sample}: {predicted_class}")

5. Practice Case

5.1 Case Objective

Create a simple machine learning model to predict housing prices based on the Boston Housing dataset. This will help you practice the complete machine learning workflow, from data loading to model evaluation.

5.2 Implementation Steps

5.2.1 Data Loading and Exploration

# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Load the Boston Housing dataset
boston = load_boston()

# Create a DataFrame
df = pd.DataFrame(data=boston.data, columns=boston.feature_names)
df['PRICE'] = boston.target

# Explore the dataset
print("Dataset shape:", df.shape)
print("\nFirst 5 rows:")
print(df.head())
print("\nDataset description:")
print(df.describe())

5.2.2 Data Visualization

# Plot the distribution of housing prices
plt.figure(figsize=(10, 6))
plt.hist(df['PRICE'], bins=30)
plt.xlabel('Price ($1000s)')
plt.ylabel('Frequency')
plt.title('Distribution of Housing Prices in Boston')
plt.show()

# Plot the relationship between average number of rooms and price
plt.figure(figsize=(10, 6))
plt.scatter(df['RM'], df['PRICE'])
plt.xlabel('Average number of rooms')
plt.ylabel('Price ($1000s)')
plt.title('Relationship between Rooms and Price')
plt.show()

5.2.3 Model Training and Evaluation

# Split the data into features and target
X = df.drop('PRICE', axis=1)
y = df['PRICE']

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

# Create and train a Linear Regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")
print(f"Root Mean Squared Error: {rmse:.2f}")
print(f"R-squared: {r2:.2f}")

# Plot actual vs predicted prices
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2)
plt.xlabel('Actual Price')
plt.ylabel('Predicted Price')
plt.title('Actual vs Predicted Housing Prices')
plt.show()

6. Interactive Exercises

Exercise 1: Environment Setup

Follow these steps to set up your machine learning environment:

  1. Check if Python is installed on your system.
  2. Create a virtual environment named ml_env.
  3. Activate the virtual environment.
  4. Install NumPy, Pandas, Matplotlib, and Scikit-learn.
  5. Verify the installations by importing the libraries in a Python script.

Exercise 2: Dataset Exploration

Using the Iris dataset, complete the following tasks:

  1. Load the Iris dataset using Scikit-learn.
  2. Create a DataFrame with the dataset.
  3. Display the number of samples for each class.
  4. Plot the relationship between sepal length and sepal width for each class.
  5. Calculate the mean and standard deviation for each feature.

Exercise 3: Model Creation

build a machine learning model to predict flower species:

  1. Split the Iris dataset into training (80%) and testing (20%) sets.
  2. Train a decision tree classifier on the training set.
  3. Make predictions on the test set.
  4. Calculate the accuracy of the model.
  5. Try different values for the max_depth parameter and see how it affects accuracy.