Close Menu
    Trending
    • 3D Printer Breaks Kickstarter Record, Raises Over $46M
    • People are using AI to ‘sit’ with them while they trip on psychedelics
    • Reinforcement Learning in the Age of Modern AI | by @pramodchandrayan | Jul, 2025
    • How This Man Grew His Beverage Side Hustle From $1k a Month to 7 Figures
    • Finding the right tool for the job: Visual Search for 1 Million+ Products | by Elliot Ford | Kingfisher-Technology | Jul, 2025
    • How Smart Entrepreneurs Turn Mid-Year Tax Reviews Into Long-Term Financial Wins
    • Become a Better Data Scientist with These Prompt Engineering Tips and Tricks
    • Meanwhile in Europe: How We Learned to Stop Worrying and Love the AI Angst | by Andreas Maier | Jul, 2025
    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

    Reinforcement Learning in the Age of Modern AI | by @pramodchandrayan | Jul, 2025

    July 1, 2025
    Machine Learning

    Finding the right tool for the job: Visual Search for 1 Million+ Products | by Elliot Ford | Kingfisher-Technology | Jul, 2025

    July 1, 2025
    Machine Learning

    Meanwhile in Europe: How We Learned to Stop Worrying and Love the AI Angst | by Andreas Maier | Jul, 2025

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

    Top Posts

    3D Printer Breaks Kickstarter Record, Raises Over $46M

    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

    Investing in AI Startups: Opportunities and Risks

    April 19, 2025

    Building GenAI Applications with Ragbits and Pydantic | by Ankush k Singal | AI Artistry | Jun, 2025

    June 15, 2025

    Smartphone-Blocking Tech Prevents Driver Distraction Crashes

    June 11, 2025
    Our Picks

    3D Printer Breaks Kickstarter Record, Raises Over $46M

    July 1, 2025

    People are using AI to ‘sit’ with them while they trip on psychedelics

    July 1, 2025

    Reinforcement Learning in the Age of Modern AI | by @pramodchandrayan | Jul, 2025

    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.