Installing Python and pip
Most Ubuntu systems come with Python 3 preinstalled, but you should verify the version and install the latest updates. Run python3 --version to check; if Python 3 isn’t installed, use these commands:
sudo apt update
sudo apt install python3 python3-pip
This ensures you have Python 3 and pip (Python’s package manager) ready for machine learning workflows.
Setting Up a Virtual Environment (Recommended)
Virtual environments isolate project dependencies, preventing conflicts between libraries across different projects. Install the venv module (bundled with Python 3) and create a new environment:
sudo apt install python3-venv  # Install venv if not already available
python3 -m venv myenv          # Create a virtual environment named "myenv"
source myenv/bin/activate      # Activate the environment (your terminal prompt will change to show the environment name)
Deactivate the environment anytime with deactivate.
Installing Core Machine Learning Libraries
For basic machine learning (e.g., linear regression, decision trees), install essential libraries like NumPy (numerical computing), Pandas (data manipulation), and scikit-learn (algorithm implementation):
pip install numpy pandas scikit-learn matplotlib
Installing Deep Learning Frameworks (Optional)
For deep learning tasks (e.g., image recognition, natural language processing), install TensorFlow or PyTorch. These frameworks support GPU acceleration (via CUDA/cuDNN) for faster training.
pip install tensorflow
pip install torch torchvision torchaudio cudatoolkit=11.3 -c pytorch
Verify installations by importing the libraries in a Python shell and checking their versions (e.g., import tensorflow as tf; print(tf.__version__)).
Writing and Running Machine Learning Code
Use a text editor (e.g., VS Code, Sublime Text) or an IDE (e.g., PyCharm) to write scripts. Below are two common examples:
Linear Regression with scikit-learn: A simple algorithm for predicting continuous values (e.g., house prices).
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Generate synthetic data (X: input, y: target)
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
# Split data into training (80%) and testing (20%) 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 model
model = LinearRegression()
model.fit(X_train, y_train)
# Make predictions on the test set
predictions = model.predict(X_test)
# Evaluate the model (mean squared error)
mse = mean_squared_error(y_test, predictions)
print(f"Mean Squared Error: {mse:.2f}")
Save as linear_regression.py and run with python3 linear_regression.py.
K-Nearest Neighbors (KNN) Classification with scikit-learn: A classification algorithm that predicts labels based on the majority class of neighboring data points.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
# Load the Iris dataset (150 samples, 4 features, 3 classes)
iris = load_iris()
X, y = iris.data, iris.target
# Split data 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)
# Initialize the KNN classifier (k=3 neighbors)
knn = KNeighborsClassifier(n_neighbors=3)
# Train the model
knn.fit(X_train, y_train)
# Make predictions on the test set
y_pred = knn.predict(X_test)
# Evaluate the model (classification report: precision, recall, F1-score)
print(classification_report(y_test, y_pred))
Save as knn_iris.py and run with python3 knn_iris.py.
Using Jupyter Notebook (Optional but Recommended)
Jupyter Notebook is an interactive environment for data exploration and prototyping. Install it via pip:
pip install notebook
Start the Notebook server:
jupyter notebook
This opens a browser window where you can create .ipynb files (notebooks) to write code, add text, and visualize results interactively.
Key Tips for Success
pip install --upgrade pip