Close Menu
    Trending
    • Apple TV+ raises subscription prices worldwide, including in UK
    • How to Build a Business That Can Run Without You
    • Bots Are Taking Over the Internet—And They’re Not Asking for Permission
    • Data Analysis Lecture 2 : Getting Started with Pandas | by Yogi Code | Coding Nexus | Aug, 2025
    • TikTok to lay off hundreds of UK content moderators
    • People Really Only Care About These 3 Things at Work — Do You Offer Them?
    • Can Machines Really Recreate “You”?
    • Meet the researcher hosting a scientific conference by and for AI
    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

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

    August 22, 2025
    Machine Learning

    Current Landscape of Artificial Intelligence Threats | by Kosiyae Yussuf | CodeToDeploy : The Tech Digest | Aug, 2025

    August 22, 2025
    Machine Learning

    Optimizing ML Costs with Azure Machine Learning | by Joshua Fox | Aug, 2025

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

    Top Posts

    Apple TV+ raises subscription prices worldwide, including in UK

    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

    Save $90 on the Microsoft Office Apps Your Business Needs

    May 13, 2025

    How to Build LLMs That Actually Understand: What DeepSeek-R1 Teaches Us About Conceptual Understanding | by AI Gravity Lab | Jul, 2025

    July 17, 2025

    AT&T to Credit Customers After Internet Outages

    January 8, 2025
    Our Picks

    Apple TV+ raises subscription prices worldwide, including in UK

    August 22, 2025

    How to Build a Business That Can Run Without You

    August 22, 2025

    Bots Are Taking Over the Internet—And They’re Not Asking for Permission

    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.