Usage Examples & API Documentation
🏠 Real Estate Price Prediction (Linear Regression)
Predict real estate prices using the built-in dataset:
from jackofalltrades.datasets import get_real_estate
from jackofalltrades.Models import LinearRegression
from jackofalltrades.Errors import Error
# Load dataset
X, y = get_real_estate()
# Train model
model = LinearRegression()
model.fit(X, y)
# Make predictions
predictions = model.predict(X)
# Evaluate performance
evaluator = Error(y_true=y, y_predicted=predictions)
print(f"R² Score: {evaluator.RSquared():.3f}")
print(f"MSE: {evaluator.MSE():.3f}")
📊 Classification with Built-in Models
Use the Classification module for binary and multi-class classification:
from jackofalltrades.Models.Classification import LogisticRegression
from jackofalltrades.Errors import Error
import numpy as np
# Create sample data
X = np.random.rand(1000, 4)
y = np.random.randint(0, 2, 1000)
# Train logistic regression model
model = LogisticRegression()
model.fit(X, y)
# Make predictions
predictions = model.predict(X)
# Evaluate
evaluator = Error(y_true=y, y_predicted=predictions)
print(f"Accuracy: {evaluator.accuracy():.3f}")
🧠 Neural Networks
Build neural networks for complex pattern recognition:
from jackofalltrades.neural_network import MLPRegressor
from jackofalltrades.datasets import get_real_estate
# Load data
X, y = get_real_estate()
# Create and train neural network
model = MLPRegressor(hidden_layers=[100, 50], activation='relu')
model.fit(X, y, epochs=100)
# Predict
predictions = model.predict(X)
🎲 Generative Models - GANs
Generate synthetic data using Generative Adversarial Networks:
from jackofalltrades.Models.GAN import GAN
import torch
# Create GAN for 28x28 images
gan = GAN(noise_dim=100, image_channels=1, image_size=28)
# Generate synthetic images
noise = torch.randn(16, 100) # Generate 16 samples
generated_images = gan.generator(noise)
print(f"Generated images shape: {generated_images.shape}")
🔄 Variational Autoencoders (VAE)
Learn data representations and generate new samples:
from jackofalltrades.Models.VAE import VAE
import torch
# Create VAE
vae = VAE(input_dim=784, latent_dim=20)
# Encode and decode data
data = torch.randn(32, 784) # Batch of flattened 28x28 images
reconstructed, mu, logvar = vae(data)
# Generate new samples
with torch.no_grad():
z = torch.randn(10, 20) # Sample from latent space
generated = vae.decoder(z)
🏘️ K-Nearest Neighbors
Classify data points based on their neighbors:
from jackofalltrades.neighbors import KNeighborsClassifier
import numpy as np
# Create sample data
X = np.random.rand(100, 2)
y = np.random.randint(0, 3, 100)
# Train KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X, y)
# Predict new samples
new_samples = np.random.rand(10, 2)
predictions = knn.predict(new_samples)
📈 Built-in Datasets
Access curated datasets for quick experimentation:
from jackofalltrades.datasets import (
get_real_estate,
get_btc_data,
get_london_house_prices,
get_fuel_data
)
# Real estate dataset
X_real, y_real = get_real_estate()
print(f"Real estate data: {X_real.shape}, {y_real.shape}")
# Bitcoin price data
btc_data = get_btc_data()
print(f"BTC data columns: {btc_data.columns.tolist()}")
# London house prices
london_data = get_london_house_prices()
# Fuel consumption data
fuel_data = get_fuel_data()
📊 Model Evaluation
Comprehensive evaluation metrics for all model types:
from jackofalltrades.Errors import Error
# For regression tasks
evaluator = Error(y_true=y_actual, y_predicted=y_pred)
# Regression metrics
print(f"R² Score: {evaluator.RSquared():.3f}")
print(f"MSE: {evaluator.MSE():.3f}")
print(f"MAE: {evaluator.MAE():.3f}")
print(f"RMSE: {evaluator.RMSE():.3f}")
# For classification tasks
print(f"Accuracy: {evaluator.accuracy():.3f}")
print(f"Precision: {evaluator.precision():.3f}")
print(f"Recall: {evaluator.recall():.3f}")
print(f"F1 Score: {evaluator.f1_score():.3f}")
🚀 Quick Start Template
Complete workflow from data loading to evaluation:
# Complete ML pipeline example
from jackofalltrades.datasets import get_real_estate
from jackofalltrades.Models import LinearRegression
from jackofalltrades.Errors import Error
from sklearn.model_selection import train_test_split
# 1. Load and prepare data
X, y = get_real_estate()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 2. Train model
model = LinearRegression()
model.fit(X_train, y_train)
# 3. Make predictions
train_pred = model.predict(X_train)
test_pred = model.predict(X_test)
# 4. Evaluate performance
train_eval = Error(y_true=y_train, y_predicted=train_pred)
test_eval = Error(y_true=y_test, y_predicted=test_pred)
print("Training Performance:")
print(f" R² Score: {train_eval.RSquared():.3f}")
print(f" RMSE: {train_eval.RMSE():.3f}")
print("Test Performance:")
print(f" R² Score: {test_eval.RSquared():.3f}")
print(f" RMSE: {test_eval.RMSE():.3f}")
📚 API Reference
Core Modules
- jackofalltrades.Models - Linear/Logistic Regression, Classification, GANs, VAEs
- jackofalltrades.linear_model - Advanced linear regression algorithms
- jackofalltrades.neural_network - Multi-layer perceptrons and deep learning
- jackofalltrades.neighbors - K-nearest neighbors algorithms
- jackofalltrades.datasets - Built-in datasets and loading utilities
- jackofalltrades.Errors - Comprehensive evaluation metrics