Banner Image

AI and ML Models Guide

Your weekly resource for mastering AI and Machine Learning, brought to you by Xode!

Introduction to Machine Learning

Machine Learning (ML) is a subset of Artificial Intelligence (AI) that allows systems to learn and improve from experience without being explicitly programmed. Here’s a basic example:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Load dataset
iris = load_iris()
X, y = iris.data, iris.target

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)

# Test model
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred)}")

This code demonstrates how to load a dataset, split it into training and testing sets, and train a Random Forest Classifier to make predictions.

Weekly Update: Understanding Linear Regression

Linear Regression is one of the simplest and most popular ML algorithms. It’s used to predict a continuous target variable based on input features.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Sample data
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2, 4, 6, 8, 10])

# Train model
model = LinearRegression()
model.fit(X, y)

# Predict
y_pred = model.predict(X)

# Plot
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label='Prediction')
plt.legend()
plt.show()

This code showcases Linear Regression in action, using simple data points to fit a predictive model.

Last updated: December 28, 2024