<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sagar Das</title>
    <description>The latest articles on Forem by Sagar Das (@sagar25).</description>
    <link>https://forem.com/sagar25</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1358036%2F857a2c40-0930-442d-8845-51a2fbbffc36.jpg</url>
      <title>Forem: Sagar Das</title>
      <link>https://forem.com/sagar25</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sagar25"/>
    <language>en</language>
    <item>
      <title>TDoC 2024 - Day 3: Introduction to Machine Learning</title>
      <dc:creator>Sagar Das</dc:creator>
      <pubDate>Tue, 17 Dec 2024 16:20:20 +0000</pubDate>
      <link>https://forem.com/sagar25/tdoc-2024-day-3-introduction-to-machine-learning-kdk</link>
      <guid>https://forem.com/sagar25/tdoc-2024-day-3-introduction-to-machine-learning-kdk</guid>
      <description>&lt;p&gt;Welcome to Day 3 of &lt;strong&gt;TDoC 2024&lt;/strong&gt;! Today, we embarked on a fascinating journey into the realm of &lt;strong&gt;Machine Learning (ML)&lt;/strong&gt;—a powerful branch of &lt;strong&gt;Artificial Intelligence (AI)&lt;/strong&gt; that allows machines to learn from data and improve their performance over time without explicit programming.&lt;/p&gt;

&lt;p&gt;Machine Learning essentially involves teaching computers to recognize patterns in data so they can make predictions or decisions. In simpler terms, it’s about creating smarter systems that get better with experience.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding Machine Learning
&lt;/h2&gt;

&lt;p&gt;Machine Learning enables systems to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recognize handwritten digits after being trained with multiple examples.&lt;/li&gt;
&lt;li&gt;Predict outcomes, such as stock prices or weather conditions.&lt;/li&gt;
&lt;li&gt;Make suggestions, like Netflix recommending your next favorite show.
Real-World Applications&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Here are some practical ML applications that you may already be using:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Spam Classification&lt;/strong&gt;: Filtering out spam emails (e.g., Gmail).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Movie Recommendations&lt;/strong&gt;: Suggesting shows based on your viewing history (e.g., Netflix).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Language Translation&lt;/strong&gt;: Translating text from one language to another (e.g., Google Translate).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grammar Assistance&lt;/strong&gt;: Correcting grammar and spelling errors (e.g., Grammarly).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For our &lt;strong&gt;VocalShift&lt;/strong&gt; project, we’ll leverage Machine Learning to build an intelligent voice-changing AI.&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of Machine Learning
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Supervised Learning&lt;/strong&gt;&lt;br&gt;
The algorithm is trained on labeled data, where each input has a corresponding output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Goal:&lt;/strong&gt; Discover the relationship between input features and their outcomes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Predicting house prices based on features like size, location, and amenities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this project we’ll focus on Supervised Learning to train models for voice transformation.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;2. Unsupervised Learning&lt;/strong&gt;&lt;br&gt;
The algorithm identifies patterns in data without predefined labels.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Goal:&lt;/strong&gt; Cluster or group similar data points based on their features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Segmenting customers into groups based on purchasing behavior.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;3. Reinforcement Learning&lt;/strong&gt;&lt;br&gt;
The algorithm learns by interacting with an environment, receiving rewards for desired actions and penalties for undesirable ones.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Goal:&lt;/strong&gt; Optimize actions to maximize long-term rewards.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Training a robot to play chess through trial and error.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Steps in a Machine Learning Project
&lt;/h2&gt;

&lt;p&gt;A typical ML workflow can be broken into three key stages:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Data Collection&lt;/strong&gt;&lt;br&gt;
The success of any ML project begins with gathering high-quality data.&lt;br&gt;
 For example, a dataset of customer purchase records or a list of cricket chirps per minute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Data Modeling&lt;/strong&gt;&lt;br&gt;
Modeling involves applying ML algorithms to extract insights from data and create predictive models.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Model Deployment&lt;/strong&gt;&lt;br&gt;
Once the model is trained, it is integrated into real-world applications, such as a product recommendation engine or a medical diagnostic system.&lt;/p&gt;


&lt;h2&gt;
  
  
  Diving Deeper: Data Preprocessing
&lt;/h2&gt;

&lt;p&gt;Before training an ML model, data must be cleaned and prepared.&lt;/p&gt;
&lt;h3&gt;
  
  
  Steps in Data Preprocessing:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Handing Missing Values
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Replace missing values with statistical measures like mean, median, or mode.&lt;/li&gt;
&lt;li&gt;Drop rows or columns with excessive missing data.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Encoding Categorical Data
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Convert non-numeric data (e.g., categories) into numeric formats for model compatibility.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Feature Scaling
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Standardize data (mean = 0, std = 1) to ensure uniformity, especially for algorithms sensitive to scale, like neural networks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Dataset Splitting
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Divide the dataset into training and testing subsets.&lt;/li&gt;
&lt;li&gt;The training set is used to build the model, while the testing set evaluates its performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Choosing a Machine Learning Model
&lt;/h2&gt;

&lt;p&gt;Selecting the right ML model depends on the type of problem and the nature of the data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Supervised&lt;/strong&gt; Learning models are suited for labeled data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unsupervised&lt;/strong&gt; Learning models are ideal for unlabeled datasets.&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Training the Model
&lt;/h3&gt;

&lt;p&gt;Model training involves fitting the ML algorithm to the training dataset. As the model learns, it improves its ability to make accurate predictions on unseen data.&lt;/p&gt;
&lt;h3&gt;
  
  
  Parameter Tuning
&lt;/h3&gt;

&lt;p&gt;To achieve optimal results, ML models undergo parameter tuning using techniques like Gradient Descent to minimize the loss function—a measure of prediction error.&lt;/p&gt;


&lt;h2&gt;
  
  
  Hands-On: Implementing Linear Regression
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Creating a Simple Linear Regression Class
&lt;/h3&gt;

&lt;p&gt;Here’s how we can code a Simple Linear Regression model in Python, complete with gradient descent for optimization:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np
import random
import matplotlib.pyplot as plt

class SimpleLinearRegression:
    def __init__(self, learning_rate=0.01, n_iterations=1000, threshold=1e-6):
        self.lr = learning_rate
        self.max_iter = n_iterations
        self.threshold = threshold
        self.weight = None  # Coefficient (slope)
        self.bias = None    # Intercept

    def fit(self, X, Y):
        X = np.array(X)
        Y = np.array(Y)
        if X.ndim == 1:
            X = X.reshape(-1, 1)

        n = X.shape[0]
        loss_history = []

        # Initializing weight and bias
        self.weight = random.uniform(-1, 1)
        self.bias = random.uniform(-1, 1)

        # Initial prediction and loss
        Y_predict = self.predict(X)
        errors = Y_predict - Y
        prev_loss = np.sum(errors ** 2) / (2 * n)
        loss_history.append(prev_loss)

        for i in range(self.max_iter):
            # Compute gradients
            dW = np.sum(errors * X.flatten()) / n  # Gradient for weight
            dB = np.sum(errors) / n                # Gradient for bias

            # Gradient descent step
            self.weight -= self.lr * dW
            self.bias -= self.lr * dB

            # Update predictions and calculate new loss
            Y_predict = self.predict(X)
            errors = Y_predict - Y
            curr_loss = np.sum(errors ** 2) / (2 * n)
            loss_history.append(curr_loss)

            # Check for convergence
            if abs(prev_loss - curr_loss) &amp;lt; self.threshold:
                print(f"Converged after {i + 1} iterations.")
                break
            prev_loss = curr_loss

            # Optional progress output
            if (i + 1) % 100 == 0:
                print(f"Iteration {i + 1}: Loss = {curr_loss:.6f}")

        return loss_history

    def predict(self, X):
        X = np.array(X)
        if X.ndim == 1:
            X = X.reshape(-1, 1)
        return self.weight * X.flatten() + self.bias

    def score(self, X, Y):
        y_pred = self.predict(X)
        u = np.sum((Y - y_pred) ** 2)  # Residual sum of squares
        v = np.sum((Y - np.mean(Y)) ** 2)  # Total sum of squares
        return 1 - (u / v)

    def plot(self, X, Y):
        X = np.array(X)
        Y = np.array(Y)
        if X.ndim == 1:
            X = X.reshape(-1, 1)

        y_pred = self.predict(X)
        plt.scatter(X.flatten(), Y, label='Data Points')
        plt.plot(X.flatten(), y_pred, color='red', label='Regression Line')
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.title('Simple Linear Regression')
        plt.legend()
        plt.show()

    def plot_loss(self, loss_history):
        plt.plot(range(len(loss_history)), loss_history, label='Loss')
        plt.xlabel('Iterations')
        plt.ylabel('Loss')
        plt.title('Loss Over Iterations')
        plt.legend()
        plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Testing the Implementation
&lt;/h4&gt;

&lt;p&gt;We can test the above class using synthetic data:'&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Generate synthetic data
np.random.seed(42)
X = np.random.rand(100) * 10  # Random numbers between 0 and 10
Y = 2.5 * X + np.random.randn(100) * 2  # Linear relationship with noise

#Train the model
model = SimpleLinearRegression(learning_rate=0.01, n_iterations=1000, threshold=1e-6)
loss_history = model.fit(X, Y)

#Evaluate the model
print("Weight (slope):", model.weight)
print("Bias (intercept):", model.bias)
print("R² Score:", model.score(X, Y))

#Visualize the regression line
model.plot(X, Y)

#Visualize the loss over iterations
model.plot_loss(loss_history)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Solving a Real-World Problem: Predicting Temperature from Cricket Chirps
&lt;/h3&gt;

&lt;p&gt;Using a real-world dataset, let’s predict temperature based on cricket chirps.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Import and Load the Dataset
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np
import pandas as pd

dataset = pd.read_csv('cricket_chirps.csv')
print(dataset)
X = dataset.iloc[:, :-1].values  # Extract independent variables
y = dataset.iloc[:, 1].values    # Extract dependent variable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 2: Split Data into Training and Test Sets
&lt;/h4&gt;

&lt;p&gt;Splitting the dataset is a crucial step to ensure the model is trained on one part of the data and evaluated on another.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 3: Train a Linear Regression Model
&lt;/h4&gt;

&lt;p&gt;Using the scikit-learn library, we can create a linear regression model:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sklearn.linear_model import LinearRegression

regressor = LinearRegression()
regressor.fit(X_train, y_train)  #Train the model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 4: Make Predictions
&lt;/h4&gt;

&lt;p&gt;Once the model is trained, it can be used to predict unseen data:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y_pred = regressor.predict(X_test)
print(y_pred)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 5: Visualize the Results
&lt;/h4&gt;

&lt;p&gt;Visualization helps in understanding the performance of the model:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import matplotlib.pyplot as plt

plt.scatter(X_train, y_train, color='green')  # Training data
plt.scatter(X_test, y_test, color='red')      # Actual test data
plt.scatter(X_test, y_pred, color='blue')     # Predicted test data
plt.plot(X_train, regressor.predict(X_train), color='gray')  # Regression line
plt.title('Temperature Prediction Based on Cricket Chirps')
plt.xlabel('Chirps per Minute')
plt.ylabel('Temperature')
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  What You Achieved on Day 3
&lt;/h3&gt;

&lt;p&gt;By the end of today, you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gained a solid understanding of Machine Learning and its real-world applications.&lt;/li&gt;
&lt;li&gt;Learned about the three main types of ML: Supervised, Unsupervised, and Reinforcement Learning.&lt;/li&gt;
&lt;li&gt;Explored the ML project lifecycle, from data collection to deployment.&lt;/li&gt;
&lt;li&gt;Implemented and visualized a Linear Regression model in Python.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Resources for Further Learning
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://scikit-learn.org/stable/user_guide.html#user-guide" rel="noopener noreferrer"&gt;scikit-learn user guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Your Feedback Matters!
&lt;/h3&gt;

&lt;p&gt;Feel free to share your results, challenges, or questions in the comments. Happy coding! 🚀&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
