Close Menu
    Trending
    • Cuba’s Energy Crisis: A Systemic Breakdown
    • AI Startup TML From Ex-OpenAI Exec Mira Murati Pays $500,000
    • STOP Building Useless ML Projects – What Actually Works
    • Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025
    • The New Career Crisis: AI Is Breaking the Entry-Level Path for Gen Z
    • Musk’s X appoints ‘king of virality’ in bid to boost growth
    • Why Entrepreneurs Should Stop Obsessing Over Growth
    • Implementing IBCS rules in Power BI
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Machine Learning»Understanding K-Nearest Neighbor & Support Vector Machine | by Alvin Octa Hidayathullah | Feb, 2025
    Machine Learning

    Understanding K-Nearest Neighbor & Support Vector Machine | by Alvin Octa Hidayathullah | Feb, 2025

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


    import numpy as np
    import matplotlib.pyplot as plt

    class SVMSoftMargin:
    “””
    Implementation of a Gentle Margin Assist Vector Machine (SVM).
    “””

    def __init__(self, alpha: float = 0.001, lambda_: float = 0.01, n_iterations: int = 1000) -> None:
    “””
    Initializes the SVM mannequin.

    Parameters:
    alpha (float): Studying charge.
    lambda_ (float): Regularization parameter.
    n_iterations (int): Variety of coaching iterations.
    “””
    self.alpha = alpha
    self.lambda_ = lambda_
    self.n_iterations = n_iterations
    self.w = None
    self.b = None

    def match(self, X: np.ndarray, y: np.ndarray) -> tuple:
    “””
    Trains the SVM mannequin utilizing gradient descent.

    Parameters:
    X (np.ndarray): Function matrix.
    y (np.ndarray): Goal labels.

    Returns:
    tuple: Discovered weights and bias.
    “””
    n_samples, n_features = X.form
    self.w = np.zeros(n_features)
    self.b = 0

    for _ in vary(self.n_iterations):
    for i, Xi in enumerate(X):
    if y[i] * (np.dot(Xi, self.w) – self.b) >= 1:
    self.w -= self.alpha * (2 * self.lambda_ * self.w)
    else:
    self.w -= self.alpha * (2 * self.lambda_ * self.w – np.dot(Xi, y[i]))
    self.b -= self.alpha * y[i]
    return self.w, self.b

    def predict(self, X: np.ndarray) -> checklist:
    “””
    Makes predictions on new knowledge.

    Parameters:
    X (np.ndarray): Function matrix.

    Returns:
    checklist: Predicted class labels.
    “””
    predictions = np.dot(X, self.w) – self.b
    return [1 if val > 0 else -1 for val in predictions]

    def get_hyperplane(x: float, w: np.ndarray, b: float, offset: float) -> float:
    “””
    Computes the choice boundary hyperplane.

    Parameters:
    x (float): Function worth.
    w (np.ndarray): Weight vector.
    b (float): Bias time period.
    offset (float): Offset for margin boundary.

    Returns:
    float: Calculated hyperplane worth.
    “””
    return (-w[0] * x + b + offset) / w[1]

    def plot_svm(X: np.ndarray, y: np.ndarray, w: np.ndarray, b: float, title: str = ‘Plot for Linear SVM’) -> None:
    “””
    Plots the choice boundary of the SVM mannequin.

    Parameters:
    X (np.ndarray): Function matrix.
    y (np.ndarray): Goal labels.
    w (np.ndarray): Weight vector.
    b (float): Bias time period.
    title (str): Plot title.
    “””
    fig, ax = plt.subplots()
    plt.scatter(X[:, 0], X[:, 1], marker=’o’, c=y)

    x0_min, x0_max = np.amin(X[:, 0]), np.amax(X[:, 0])
    x1_decision_min, x1_decision_max = get_hyperplane(x0_min, w, b, 0), get_hyperplane(x0_max, w, b, 0)
    x1_margin_min, x1_margin_max = get_hyperplane(x0_min, w, b, -1), get_hyperplane(x0_max, w, b, -1)
    x1_margin_plus_min, x1_margin_plus_max = get_hyperplane(x0_min, w, b, 1), get_hyperplane(x0_max, w, b, 1)

    ax.plot([x0_min, x0_max], [x1_decision_min, x1_decision_max], ‘y–‘)
    ax.plot([x0_min, x0_max], [x1_margin_min, x1_margin_max], ‘ok’)
    ax.plot([x0_min, x0_max], [x1_margin_plus_min, x1_margin_plus_max], ‘ok’)

    ax.set_ylim([np.amin(X[:, 1]) – 3, np.amax(X[:, 1]) + 3])
    plt.title(title)
    plt.present()



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleApple pulls data protection tool after UK government security row
    Next Article The Next AI Revolution: A Tutorial Using VAEs to Generate High-Quality Synthetic Data
    Team_AIBS News
    • Website

    Related Posts

    Machine Learning

    Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025

    July 1, 2025
    Machine Learning

    Why PDF Extraction Still Feels LikeHack

    July 1, 2025
    Machine Learning

    🚗 Predicting Car Purchase Amounts with Neural Networks in Keras (with Code & Dataset) | by Smruti Ranjan Nayak | Jul, 2025

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Cuba’s Energy Crisis: A Systemic Breakdown

    July 1, 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

    Are You Still Using LoRA to Fine-Tune Your LLM?

    March 14, 2025

    Smash Your Way to Success with an iSmash Rage Room Franchise

    April 10, 2025

    Risking Trump’s Ire, E.U. Accuses Apple and Google of Unfair Practices

    March 19, 2025
    Our Picks

    Cuba’s Energy Crisis: A Systemic Breakdown

    July 1, 2025

    AI Startup TML From Ex-OpenAI Exec Mira Murati Pays $500,000

    July 1, 2025

    STOP Building Useless ML Projects – What Actually Works

    July 1, 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.