ml

Machine Learning Concepts

Machine Learning (ML) Concepts with Code Examples

1. Supervised Learning

Supervised learning involves training a model on labeled data to make predictions. Common tasks: classification, regression.

Example: Classification using Scikit-learn
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier

data = load_iris()
X, y = data.data, data.target

model = RandomForestClassifier()
model.fit(X, y)

print(model.predict([[5.1, 3.5, 1.4, 0.2]]))

2. Unsupervised Learning

Unsupervised learning finds hidden patterns in data without labeled outcomes. Common tasks: clustering, dimensionality reduction.

Example: K-Means Clustering
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris

X = load_iris().data
kmeans = KMeans(n_clusters=3, random_state=0)
kmeans.fit(X)

print(kmeans.labels_[:10])

3. Reinforcement Learning

Reinforcement learning is about learning optimal actions through rewards and penalties in an environment.

Example: Basic Q-Learning (Pseudocode)
# Q(s, a) = Q(s, a) + alpha * (reward + gamma * max(Q(s', a')) - Q(s, a))
Q = {}  # Q-table
state = env.reset()
for step in range(1000):
    action = choose_action(state)
    new_state, reward = env.step(action)
    update_q_table(state, action, reward, new_state)
    state = new_state

4. Deep Learning

Deep learning is a subset of ML that uses neural networks with many layers. It excels in tasks like image recognition and language modeling.

Example: Simple Neural Network with Keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential([
    Dense(64, activation='relu', input_shape=(10,)),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy')
model.summary()

5. Neural Networks

Neural networks are inspired by the human brain and consist of layers of interconnected "neurons." They're the building blocks of deep learning.

Example: Perceptron in PyTorch
import torch.nn as nn

class Perceptron(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Linear(2, 1)
    
    def forward(self, x):
        return torch.sigmoid(self.fc(x))

6. Natural Language Processing (NLP)

NLP enables machines to understand and generate human language. Tasks include sentiment analysis, translation, and summarization.

Example: Sentiment Analysis with Hugging Face
from transformers import pipeline

classifier = pipeline("sentiment-analysis")
print(classifier("I love using AI tools!"))

7. Computer Vision

Computer Vision helps machines interpret visual data such as images and videos. Used in face recognition, object detection, etc.

Example: Image Classification with PyTorch
import torchvision.models as models

model = models.resnet18(pretrained=True)
model.eval()

# Example image classification requires input preprocessing, skipped for brevity
# Use torchvision.transforms and PIL to load & preprocess images

Comments

Popular posts from this blog

ree image-to-video generator tools

Example TensorFlow