|
|
import streamlit as st |
|
|
|
|
|
|
|
|
def introduction_to_ml(): |
|
|
introduction_blog = ''' |
|
|
## ๐ค Introduction to Machine Learning (ML) |
|
|
Machine Learning (ML) is a subset of Artificial Intelligence (AI) that enables computers to learn from data and make predictions or decisions without being explicitly programmed. It has revolutionized many industries and plays a crucial role in technologies such as self-driving cars ๐, recommendation systems ๐ฑ, and facial recognition ๐๏ธ. |
|
|
|
|
|
### ๐ Types of Machine Learning |
|
|
There are three main types of machine learning: |
|
|
|
|
|
1. **๐ Supervised Learning**: |
|
|
Supervised learning algorithms learn from labeled data. The model is trained using a dataset where the input data and the correct output are both provided. The goal is to learn a mapping from inputs to outputs. Examples include linear regression ๐, logistic regression ๐งโ๐ป, and decision trees ๐ณ. |
|
|
|
|
|
2. **๐ Unsupervised Learning**: |
|
|
In unsupervised learning, the algorithm is given data without any labeled outputs. The goal is to find hidden patterns or groupings in the data. Examples include clustering ๐ง (e.g., K-means) and dimensionality reduction techniques ๐๏ธ (e.g., PCA). |
|
|
|
|
|
3. **๐
Reinforcement Learning**: |
|
|
Reinforcement learning involves an agent that learns to make decisions by interacting with an environment to maximize a cumulative reward. It is widely used in robotics ๐ค, game AI ๐ฎ, and real-time decision-making systems. |
|
|
|
|
|
### ๐ Popular Machine Learning Algorithms |
|
|
Some of the most commonly used ML algorithms include: |
|
|
|
|
|
- **๐ Linear Regression**: A simple algorithm used for predicting continuous values. |
|
|
- **๐ Logistic Regression**: Used for binary classification problems. |
|
|
- **๐ณ Decision Trees**: A tree-like model used for both classification and regression tasks. |
|
|
- **๐ K-Nearest Neighbors (KNN)**: A non-parametric method used for classification and regression. |
|
|
- **โก Support Vector Machines (SVM)**: A powerful classifier that works well for high-dimensional spaces. |
|
|
- **๐ง Neural Networks**: A set of algorithms, modeled after the human brain, that are used for complex tasks like image and speech recognition. |
|
|
|
|
|
#### ๐ Applications of Machine Learning |
|
|
Machine learning is used in a wide variety of fields, including: |
|
|
|
|
|
- **๐ฅ Healthcare**: ML is used for predicting diseases, recommending treatments, and analyzing medical data. |
|
|
- **๐ฐ Finance**: Used for fraud detection, algorithmic trading, and risk analysis. |
|
|
- **๐๏ธ E-commerce**: ML powers recommendation systems, personalized marketing, and customer support chatbots. |
|
|
- **๐ Self-driving Cars**: ML algorithms help autonomous vehicles navigate and make real-time decisions. |
|
|
|
|
|
### ๐ Conclusion |
|
|
Machine learning continues to evolve, with new algorithms, techniques, and applications emerging regularly. As the amount of data grows ๐ and computational power increases โก, the potential of ML to impact industries and improve our daily lives is limitless. |
|
|
''' |
|
|
|
|
|
return introduction_blog |
|
|
|
|
|
|
|
|
def supervised_learning(): |
|
|
supervised = ''' |
|
|
### ๐ Supervised Learning |
|
|
Supervised learning algorithms learn from labeled data. The model is trained using a dataset where the input data and the correct output are both provided. The goal is to learn a mapping from inputs to outputs. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ Linear Regression**: Used to predict a continuous value, such as predicting house prices ๐ . |
|
|
```python |
|
|
from sklearn.linear_model import LinearRegression |
|
|
X = [[1], [2], [3], [4], [5]] # Features |
|
|
y = [1, 2, 2.5, 4, 5] # Target |
|
|
model = LinearRegression() |
|
|
model.fit(X, y) |
|
|
predictions = model.predict([[6]]) # Predict for 6 hours of study ๐ |
|
|
``` |
|
|
''' |
|
|
return supervised |
|
|
|
|
|
|
|
|
def unsupervised_learning(): |
|
|
unsupervised = ''' |
|
|
### ๐ Unsupervised Learning |
|
|
In unsupervised learning, the algorithm is given data without any labeled outputs. The goal is to find hidden patterns or groupings in the data. Examples include clustering ๐ง (e.g., K-means) and dimensionality reduction techniques ๐๏ธ (e.g., PCA). |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ K-Means Clustering**: Grouping data points into clusters based on similarity. |
|
|
```python |
|
|
from sklearn.cluster import KMeans |
|
|
X = [[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]] |
|
|
kmeans = KMeans(n_clusters=2) |
|
|
kmeans.fit(X) |
|
|
labels = kmeans.predict(X) |
|
|
``` |
|
|
''' |
|
|
return unsupervised |
|
|
|
|
|
|
|
|
def reinforcement_learning(): |
|
|
reinforcement = ''' |
|
|
### ๐
Reinforcement Learning |
|
|
Reinforcement learning involves an agent that learns to make decisions by interacting with an environment to maximize a cumulative reward. It is widely used in robotics ๐ค, game AI ๐ฎ, and real-time decision-making systems. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ Q-Learning**: A reinforcement learning algorithm where an agent learns to maximize rewards by updating Q-values. |
|
|
```python |
|
|
import numpy as np |
|
|
Q = np.zeros((5, 5)) # Example Q-table for 5 states and 5 actions |
|
|
alpha = 0.1 # Learning rate |
|
|
gamma = 0.9 # Discount factor |
|
|
reward = 10 |
|
|
state = 0 |
|
|
action = 1 |
|
|
next_state = 1 |
|
|
Q[state, action] = Q[state, action] + alpha * (reward + gamma * np.max(Q[next_state]) - Q[state, action]) |
|
|
``` |
|
|
''' |
|
|
return reinforcement |
|
|
|
|
|
|
|
|
def linear_regression(): |
|
|
linear = ''' |
|
|
### ๐ Linear Regression |
|
|
Linear regression is used to predict a continuous value based on one or more input features. It finds the best-fit line to minimize the error between the predicted and actual values. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ Predicting House Prices**: Predict the price of a house based on its features such as size and location. |
|
|
```python |
|
|
from sklearn.linear_model import LinearRegression |
|
|
X = [[1], [2], [3], [4], [5]] # Features (e.g., years of experience) |
|
|
y = [1, 2, 2.5, 4, 5] # Target (e.g., salary) |
|
|
model = LinearRegression() |
|
|
model.fit(X, y) |
|
|
predictions = model.predict([[6]]) # Predict for 6 years of experience |
|
|
``` |
|
|
''' |
|
|
return linear |
|
|
|
|
|
|
|
|
def logistic_regression(): |
|
|
logistic = ''' |
|
|
### ๐ Logistic Regression |
|
|
Logistic regression is used for binary classification tasks, where the goal is to predict one of two outcomes, such as pass/fail or spam/not spam. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ง Predicting Spam Emails**: Classifying emails as spam or not spam. |
|
|
```python |
|
|
from sklearn.linear_model import LogisticRegression |
|
|
from sklearn.datasets import load_iris |
|
|
data = load_iris() |
|
|
X = data.data |
|
|
y = (data.target == 0).astype(int) # Binary classification (class 0 vs others) |
|
|
model = LogisticRegression() |
|
|
model.fit(X, y) |
|
|
predictions = model.predict(X) |
|
|
``` |
|
|
''' |
|
|
return logistic |
|
|
|
|
|
|
|
|
def decision_trees(): |
|
|
decision = ''' |
|
|
### ๐ณ Decision Trees |
|
|
Decision trees split the data into subsets based on feature values, creating a tree-like model. It is used for both classification and regression tasks. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ธ Classifying Iris Species**: A decision tree can be used to classify different species of Iris flowers. |
|
|
```python |
|
|
from sklearn.tree import DecisionTreeClassifier |
|
|
from sklearn.datasets import load_iris |
|
|
data = load_iris() |
|
|
X = data.data |
|
|
y = data.target |
|
|
model = DecisionTreeClassifier() |
|
|
model.fit(X, y) |
|
|
predictions = model.predict(X) |
|
|
``` |
|
|
''' |
|
|
return decision |
|
|
|
|
|
|
|
|
def knn(): |
|
|
knn = ''' |
|
|
### ๐ K-Nearest Neighbors (KNN) |
|
|
KNN is a simple, non-parametric algorithm that classifies data based on the majority vote of its nearest neighbors. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ Classifying a Data Point**: Predict the class of a data point based on its nearest neighbors. |
|
|
```python |
|
|
from sklearn.neighbors import KNeighborsClassifier |
|
|
from sklearn.datasets import load_iris |
|
|
data = load_iris() |
|
|
X = data.data |
|
|
y = data.target |
|
|
model = KNeighborsClassifier(n_neighbors=3) |
|
|
model.fit(X, y) |
|
|
predictions = model.predict(X) |
|
|
``` |
|
|
''' |
|
|
return knn |
|
|
|
|
|
|
|
|
def svm(): |
|
|
svm = ''' |
|
|
### โก Support Vector Machines (SVM) |
|
|
SVM is a powerful classifier that works well for high-dimensional data. It tries to find the hyperplane that best separates the data points of different classes. |
|
|
|
|
|
**๐ Example**: |
|
|
- **๐ธ Classifying Iris Flowers**: An SVM can be used to classify Iris flowers into different species. |
|
|
```python |
|
|
from sklearn.svm import SVC |
|
|
from sklearn.datasets import load_iris |
|
|
data = load_iris() |
|
|
X = data.data |
|
|
y = data.target |
|
|
model = SVC(kernel='linear') |
|
|
model.fit(X, y) |
|
|
predictions = model.predict(X) |
|
|
``` |
|
|
''' |
|
|
return svm |
|
|
|
|
|
|
|
|
def neural_networks(): |
|
|
neural = ''' |
|
|
### ๐ง Neural Networks |
|
|
Neural networks are modeled after the human brain, with layers of interconnected nodes (neurons) used for tasks like image and speech recognition. |
|
|
|
|
|
**๐ Example**: |
|
|
- **1๏ธโฃ2๏ธโฃ3๏ธโฃ Classifying Handwritten Digits**: A simple neural network can be used to classify digits from the MNIST dataset. |
|
|
```python |
|
|
from sklearn.neural_network import MLPClassifier |
|
|
from sklearn.datasets import load_iris |
|
|
data = load_iris() |
|
|
X = data.data |
|
|
y = data.target |
|
|
model = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000) |
|
|
model.fit(X, y) |
|
|
predictions = model.predict(X) |
|
|
``` |
|
|
''' |
|
|
return neural |
|
|
|
|
|
|
|
|
st.sidebar.header("๐ Contents") |
|
|
|
|
|
|
|
|
page = st.sidebar.radio("๐ Select a Topic", |
|
|
["Introduction", "Types of Machine Learning", "Popular Algorithms"]) |
|
|
|
|
|
|
|
|
if page == "Types of Machine Learning": |
|
|
types_of_ml = st.sidebar.radio("๐ Types of Machine Learning", |
|
|
["๐ธ Supervised Learning", "๐ธ Unsupervised Learning", "๐ธ Reinforcement Learning"]) |
|
|
else: |
|
|
types_of_ml = None |
|
|
|
|
|
if page == "Popular Algorithms": |
|
|
popular_algorithms = st.sidebar.radio("๐ Popular Algorithms", |
|
|
["๐ Linear Regression", "๐ Logistic Regression", "๐ณ Decision Trees", |
|
|
"๐ K-Nearest Neighbors (KNN)", "โก Support Vector Machines (SVM)", "๐ง Neural Networks"]) |
|
|
else: |
|
|
popular_algorithms = None |
|
|
|
|
|
|
|
|
st.markdown("<h1 style='text-align: center; color: orange;'>Machine Learning (ML)</h1>", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
if page == "Introduction": |
|
|
st.markdown(introduction_to_ml()) |
|
|
|
|
|
elif types_of_ml == "๐ธ Supervised Learning": |
|
|
st.markdown(supervised_learning()) |
|
|
elif types_of_ml == "๐ธ Unsupervised Learning": |
|
|
st.markdown(unsupervised_learning()) |
|
|
elif types_of_ml == "๐ธ Reinforcement Learning": |
|
|
st.markdown(reinforcement_learning()) |
|
|
|
|
|
elif popular_algorithms == "๐ Linear Regression": |
|
|
st.markdown(linear_regression()) |
|
|
elif popular_algorithms == "๐ Logistic Regression": |
|
|
st.markdown(logistic_regression()) |
|
|
elif popular_algorithms == "๐ณ Decision Trees": |
|
|
st.markdown(decision_trees()) |
|
|
elif popular_algorithms == "๐ K-Nearest Neighbors (KNN)": |
|
|
st.markdown(knn()) |
|
|
elif popular_algorithms == "โก Support Vector Machines (SVM)": |
|
|
st.markdown(svm()) |
|
|
elif popular_algorithms == "๐ง Neural Networks": |
|
|
st.markdown(neural_networks()) |
|
|
|