Close Menu
    Trending
    • AI-Powered Content Creation Gives Your Docs and Slides New Life
    • AI is nothing but all Software Engineering: you have no place in the industry without software engineering | by Irfan Ullah | Aug, 2025
    • Robot Videos: World Humanoid Robot Games, RoboBall, More
    • I Risked Everything to Build My Company. Four Years Later, Here’s What I’ve Learned About Building Real, Lasting Success
    • Tried an AI Text Humanizer That Passes Copyscape Checker
    • 🔴 20 Most Common ORA- Errors in Oracle Explained in Details | by Pranav Bakare | Aug, 2025
    • The AI Superfactory: NVIDIA’s Multi-Data Center ‘Scale Across’ Ethernet
    • Apple TV+ raises subscription prices worldwide, including in UK
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Machine Learning»Support Vector Machines (SVM) 🏆 Build It from Scratch! | by Ahmed Abdulwahid
    Machine Learning

    Support Vector Machines (SVM) 🏆 Build It from Scratch! | by Ahmed Abdulwahid

    Team_AIBS NewsBy Team_AIBS NewsFebruary 14, 2025No Comments2 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    import numpy as np

    class SVM:
    def __init__(self, learning_rate=0.001, lambda_param=0.01, n_iters=1000):
    “””
    Initialize the SVM mannequin with hyperparameters.

    Parameters:
    – learning_rate: The step measurement for updating weights throughout coaching.
    – lambda_param: Regularization parameter to stop overfitting.
    – n_iters: Variety of iterations for coaching the mannequin.
    “””
    self.lr = learning_rate # Studying charge
    self.lambda_param = lambda_param # Regularization parameter
    self.n_iters = n_iters # Variety of coaching iterations
    self.w = None # Weights vector
    self.b = None # Bias time period

    def match(self, X, y):
    “””
    Practice the SVM mannequin utilizing gradient descent.

    Parameters:
    – X: Function matrix (n_samples x n_features).
    – y: Goal labels (n_samples,). Labels needs to be -1 or 1.
    “””
    n_samples, n_features = X.form # Get the variety of samples and options

    # Guarantee labels are both -1 or 1
    y_ = np.the place(y <= 0, -1, 1)

    # Initialize weights as zeros and bias as zero
    self.w = np.zeros(n_features)
    self.b = 0

    # Coaching utilizing Stochastic Gradient Descent (SGD)
    for _ in vary(self.n_iters):
    for idx, x_i in enumerate(X): # Iterate via all coaching samples
    # Test if the pattern is accurately categorised
    situation = y_[idx] * (np.dot(x_i, self.w) + self.b) >= 1
    if situation:
    # If accurately categorised, apply solely regularization replace
    self.w -= self.lr * (2 * self.lambda_param * self.w)
    else:
    # If misclassified, replace weights and bias
    self.w -= self.lr * (2 * self.lambda_param * self.w – np.dot(x_i, y_[idx]))
    self.b -= self.lr * y_[idx]

    def predict(self, X):
    “””
    Predict the category labels for enter knowledge.

    Parameters:
    – X: Function matrix (n_samples x n_features).

    Returns:
    – Class labels (-1 or 1) for every enter pattern.
    “””
    return np.signal(np.dot(X, self.w) + self.b)

    # 🔥 Instance Utilization
    X_train = np.array([[1, 2], [2, 3], [3, 4], [5, 5], [1, 0]]) # Coaching characteristic matrix
    y_train = np.array([1, 1, -1, -1, 1]) # Coaching labels

    # Initialize and practice the SVM mannequin
    svm = SVM()
    svm.match(X_train, y_train)

    # Take a look at the mannequin with new knowledge
    X_test = np.array([[3, 3], [1, 1]]) # Take a look at characteristic matrix
    print(svm.predict(X_test)) # Output: Predicted class labels (both -1 or 1)



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleA.I. Accelerates in Paris + Can A.I. Fix Your Love Life?
    Next Article AI vs. Human Fund Managers: A Comparative Analysis
    Team_AIBS News
    • Website

    Related Posts

    Machine Learning

    AI is nothing but all Software Engineering: you have no place in the industry without software engineering | by Irfan Ullah | Aug, 2025

    August 22, 2025
    Machine Learning

    🔴 20 Most Common ORA- Errors in Oracle Explained in Details | by Pranav Bakare | Aug, 2025

    August 22, 2025
    Machine Learning

    Data Analysis Lecture 2 : Getting Started with Pandas | by Yogi Code | Coding Nexus | Aug, 2025

    August 22, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    AI-Powered Content Creation Gives Your Docs and Slides New Life

    August 22, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    Marcus Lemonis Fixes Businesses But a Personal Struggle Changed Everything

    July 22, 2025

    Return-to-Office Push Meets Employee Pushback — What’s Next?

    April 9, 2025

    Step-by-Step Guide to Build and Deploy an LLM-Powered Chat with Memory in Streamlit

    May 2, 2025
    Our Picks

    AI-Powered Content Creation Gives Your Docs and Slides New Life

    August 22, 2025

    AI is nothing but all Software Engineering: you have no place in the industry without software engineering | by Irfan Ullah | Aug, 2025

    August 22, 2025

    Robot Videos: World Humanoid Robot Games, RoboBall, More

    August 22, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.