← Back to Home
🤖 AI & Machine Learning
Learn the essential libraries and techniques for AI and Machine Learning
1. NumPy - Working with Numbers
What is it? NumPy helps you work with large sets of numbers quickly. It's much faster than regular Python for math operations.
numpy_tutorial.py
import numpy as np
# Create arrays (like lists, but faster)
arr = np.array([1, 2, 3, 4, 5])
print(arr)
# Create special arrays
zeros = np.zeros((3, 3)) # All zeros
ones = np.ones((2, 4)) # All ones
range_arr = np.arange(0, 10, 2) # 0,2,4,6,8
# Math operations
result = arr * 2 # Multiply all by 2
print(np.sum(arr)) # Add all: 15
print(np.mean(arr)) # Average: 3
print(np.max(arr)) # Maximum: 5
# 2D arrays (tables)
matrix = np.array([[1, 2], [3, 4]])
print(matrix.shape) # Shows: (2, 2)
print(matrix[0, 1]) # First row, second column: 2
2. Pandas - Working with Data
What is it? Pandas lets you work with data like Excel spreadsheets. You can load, organize, and analyze data easily.
pandas_tutorial.py
import pandas as pd
# Create data (like a table)
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['NYC', 'LA', 'Chicago']
}
df = pd.DataFrame(data)
print(df)
# Get information
print(df['Name']) # Get one column
print(df.iloc[0]) # Get first row
print(df.shape) # Rows and columns
# Filter data
over30 = df[df['Age'] > 30]
print(over30)
# Statistics
print(df['Age'].mean()) # Average age
print(df['Age'].max()) # Oldest age
# Read from CSV file
df = pd.read_csv('data.csv')
# Save to CSV file
df.to_csv('output.csv', index=False)
3. Scikit-Learn - Machine Learning Basics
What is it? Scikit-Learn provides simple tools for machine learning. It handles training models and making predictions.
sklearn_tutorial.py
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Split data into training and testing
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Normalize data (make same scale)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Train a model
model = LogisticRegression()
model.fit(X_train, y_train)
# Make predictions
predictions = model.predict(X_test)
# Check accuracy
print(accuracy_score(y_test, predictions))
4. TensorFlow - Deep Learning
What is it? TensorFlow is for building neural networks that can learn complex patterns. Used for advanced AI like image recognition.
tensorflow_tutorial.py
from tensorflow import keras
from tensorflow.keras import layers
# Build a neural network
model = keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Set up training
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Train the model
model.fit(X_train, y_train, epochs=10, batch_size=32)
# Test the model
test_loss, test_accuracy = model.evaluate(X_test, y_test)
# Make predictions
predictions = model.predict(X_test)
