
Python AI & Deep Learning Cheatsheet
Welcome to your go-to reference for AI and deep learning in Python! Whether you're just getting started or brushing up on key concepts, this guide will help you navigate the essential libraries, functions, and workflows. Let's jump right in.
Essential Libraries
When it comes to AI and deep learning in Python, a few libraries are indispensable. Here are the most important ones you should know:
- NumPy: The foundation for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices.
- Pandas: Perfect for data manipulation and analysis. It offers data structures like DataFrames that make working with structured data effortless.
- Matplotlib and Seaborn: Essential for data visualization. Use them to create plots, histograms, and heatmaps.
- Scikit-learn: A versatile library for machine learning. It includes tools for classification, regression, clustering, and more.
- TensorFlow and Keras: Powerhouses for building and training deep learning models. Keras is often used for its user-friendly API.
- PyTorch: Known for its flexibility and dynamic computation graph, it's a favorite among researchers.
Here’s a quick example of importing these libraries:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
import torch
Library | Primary Use Case | Key Feature |
---|---|---|
NumPy | Numerical operations | Efficient array processing |
Pandas | Data manipulation | DataFrame object |
Scikit-learn | Traditional ML algorithms | Easy-to-use API |
TensorFlow | Deep learning models | Production deployment |
PyTorch | Research and prototyping | Dynamic computation graph |
Data Preprocessing
Before feeding data into a model, it needs to be cleaned and formatted. Data preprocessing is a critical step that can significantly impact model performance.
Start by handling missing values. You can either remove rows with missing data or fill them with a statistic like the mean or median. Here’s how you might do it with Pandas:
# Drop rows with any missing values
df_cleaned = df.dropna()
# Fill missing values with the mean
df_filled = df.fillna(df.mean())
Next, encode categorical variables. Machine learning models require numerical input, so you’ll need to convert categories into numbers. Use label encoding for ordinal data and one-hot encoding for nominal data.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
# Label encoding
label_encoder = LabelEncoder()
df['category_encoded'] = label_encoder.fit_transform(df['category'])
# One-hot encoding
df = pd.get_dummies(df, columns=['category'])
Finally, normalize or standardize your features to ensure they’re on a similar scale. This is especially important for algorithms like SVM or neural networks.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
Technique | Use Case | Example Library Function |
---|---|---|
Handling missing values | Incomplete data | df.dropna() , df.fillna() |
Label encoding | Ordinal categories | LabelEncoder().fit_transform() |
One-hot encoding | Nominal categories | pd.get_dummies() |
Feature scaling | Algorithms sensitive to feature scale | StandardScaler() |
Building Machine Learning Models
With your data preprocessed, you’re ready to build and train models. Scikit-learn makes this process straightforward with a consistent API across algorithms.
First, split your data into training and testing sets:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Now, choose a model. For classification, you might use a Random Forest; for regression, perhaps Linear Regression. Here’s how to train a Random Forest classifier:
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
Evaluate your model’s performance using metrics like accuracy, precision, recall, or F1-score for classification, and MAE or RMSE for regression.
from sklearn.metrics import accuracy_score
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
- Split your data into training and testing sets
- Choose an appropriate algorithm
- Train the model on the training data
- Evaluate performance on the test set
- Tune hyperparameters for better results
Introduction to Neural Networks
Neural networks are at the heart of deep learning. They consist of layers of neurons that learn patterns from data. Using Keras, you can build neural networks with just a few lines of code.
Start by defining the architecture of your network. Here’s a simple example for a binary classification problem:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid')
])
Compile the model by specifying the optimizer, loss function, and metrics:
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
Now, train the model on your data:
history = model.fit(X_train, y_train, epochs=10, validation_split=0.2)
Layer Type | Activation Function | Common Use Case |
---|---|---|
Dense | ReLU | Hidden layers in most networks |
Dense | Sigmoid | Binary classification output |
Dense | Softmax | Multi-class classification output |
Convolutional Neural Networks (CNNs)
For image data, Convolutional Neural Networks (CNNs) are the go-to architecture. They use convolutional layers to detect spatial patterns like edges, textures, and objects.
Build a CNN using Keras:
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten
model = Sequential([
Conv2D(32, (3,3), activation='relu', input_shape=(64,64,3)),
MaxPooling2D(2,2),
Conv2D(64, (3,3), activation='relu'),
MaxPooling2D(2,2),
Flatten(),
Dense(128, activation='relu'),
Dense(1, activation='sigmoid')
])
Compile and train just like before:
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=32)
Key layers in a CNN: - Convolutional layers: Detect features using filters - Pooling layers: Reduce spatial dimensions, making the network more efficient - Flatten layer: Converts 2D feature maps into 1D vectors for dense layers
Recurrent Neural Networks (RNNs)
For sequential data like time series or text, Recurrent Neural Networks (RNNs) are ideal. They maintain a hidden state that captures information from previous steps.
A common type of RNN is the LSTM (Long Short-Term Memory) network, which handles long-range dependencies well. Here’s how to build one:
from tensorflow.keras.layers import LSTM
model = Sequential([
LSTM(50, return_sequences=True, input_shape=(timesteps, features)),
LSTM(50),
Dense(1)
])
Compile for regression (e.g., predicting a continuous value):
model.compile(optimizer='adam', loss='mse')
model.fit(X_train, y_train, epochs=10, batch_size=32)
Network Type | Best For | Key Layer |
---|---|---|
CNN | Image data | Conv2D |
RNN | Sequential data | LSTM |
Transformer | NLP tasks | Multi-Head Attention |
Natural Language Processing (NLP)
Natural Language Processing (NLP) involves working with text data. Common tasks include sentiment analysis, text classification, and machine translation.
Start by tokenizing your text—splitting it into words or subwords and converting them to integers:
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
Pad the sequences to ensure they’re all the same length:
from tensorflow.keras.preprocessing.sequence import pad_sequences
padded_sequences = pad_sequences(sequences, maxlen=100)
Now, build a model. For simple tasks, a feedforward network might suffice, but for better performance, use an LSTM or even a pre-trained model like BERT.
model = Sequential([
Embedding(10000, 128, input_length=100),
LSTM(64),
Dense(1, activation='sigmoid')
])
- Tokenize text into numerical sequences
- Pad sequences to uniform length
- Use embedding layers to learn word representations
- Choose RNNs or transformers for context-aware models
Hyperparameter Tuning
Hyperparameter tuning is the process of finding the best set of parameters for your model. This can significantly improve performance.
Use Grid Search or Random Search with Scikit-learn:
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 10, 20]
}
grid_search = GridSearchCV(RandomForestClassifier(), param_grid, cv=5)
grid_search.fit(X_train, y_train)
print("Best parameters:", grid_search.best_params_)
For neural networks, use Keras Tuner:
import keras_tuner as kt
def build_model(hp):
model = Sequential()
model.add(Dense(units=hp.Int('units', min_value=32, max_value=512, step=32), activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
tuner = kt.RandomSearch(build_model, objective='val_accuracy', max_trials=5)
tuner.search(X_train, y_train, epochs=5, validation_data=(X_val, y_val))
Tuning Method | Pros | Cons |
---|---|---|
Grid Search | Exhaustive, finds best combo | Computationally expensive |
Random Search | Faster, good for high-dimensional spaces | May miss optimal parameters |
Bayesian Optimization | Efficient, learns from past trials | More complex to implement |
Model Deployment
Once your model is trained, you’ll want to deploy it so others can use it. Common options include web APIs, mobile apps, or cloud services.
Save your model using Keras:
model.save('my_model.h5')
Load it later for predictions:
from tensorflow.keras.models import load_model
loaded_model = load_model('my_model.h5')
predictions = loaded_model.predict(new_data)
For a simple web API, use Flask:
from flask import Flask, request, jsonify
import numpy as np
app = Flask(__name__)
model = load_model('my_model.h5')
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
features = np.array(data['features']).reshape(1, -1)
prediction = model.predict(features)
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
app.run()
- Save your model to disk after training
- Use web frameworks like Flask or FastAPI to create APIs
- Deploy to cloud platforms like AWS, GCP, or Azure for scalability
Useful Tips and Tricks
Here are some additional tips to make your AI and deep learning journey smoother:
- Always normalize your data: This helps models converge faster and perform better.
- Use callbacks in Keras: They can stop training early if performance plateaus or save the best model automatically.
- Visualize your training: Plot training and validation loss to check for overfitting.
import matplotlib.pyplot as plt
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.show()
- Experiment with different architectures: Don’t be afraid to try new layer types or configurations.
- Leverage transfer learning: Use pre-trained models for tasks like image classification or NLP to save time and resources.
Tip Category | Suggestion | Benefit |
---|---|---|
Data Preparation | Normalize features | Faster convergence |
Training | Use Early Stopping | Prevents overfitting |
Deployment | Containerize with Docker | Consistent environments |
That wraps up our Python AI and Deep Learning Cheatsheet! Keep this guide handy as you build, train, and deploy your models. Happy coding!