Python

Python

Sample Implementation

 

Example 1

import numpy as np import matplotlib.pyplot as plt import pandas as pd dataset = pd.read_csv('Position_Salaries.csv') X = dataset.iloc[:, 1:2].values y = dataset.iloc[:, 2].values #Feature Scaling from sklearn.preprocessing import StandardScaler sc_X = StandardScaler() sc_y = StandardScaler() X = sc_X.fit_transform(X) y = sc_y.fit_transform(y) #Fitting SVR to the dataset from sklearn.svm import SVR regressor = SVR(kernel = 'rbf') regressor.fit(X, y) #Predicting a new result y_pred = regressor.predict(6.5) y_pred = sc_y.inverse_transform(y_pred) #Visualizing the SVR results X_grid = np.arange(min(X), max(X), 0.01) #this step required because data is feature scaled. X_grid = X_grid.reshape((len(X_grid), 1)) plt.scatter(X, y, color = 'red') plt.plot(X_grid, regressor.predict(X_grid), color = 'blue') plt.title('Truth or Bluff (SVR)') plt.xlabel('Position level') plt.ylabel('Salary') plt.show()
notion image
 

Major Kernel Functions in Support Vector Machine (SVM)

Gaussian Kernel Radial Basis Function (RBF)
RBF核函数是一种常用的非线性核函数。它将数据映射到无穷维度的高维度空间,并以高斯分布的方式表达。RBF核函数适用于许多非线性问题,但它有许多超参数需要调整,包括带宽参数(gamma)。
from sklearn.svm import SVC classifier = SVC(kernel ='rbf', random_state = 0) # training set in x, y axis classifier.fit(x_train, y_train)
 
Sigmoid Kernel: this function is equivalent to a two-layer, perceptron model of the neural network, which is used as an activation function for artificial neurons.
from sklearn.svm import SVC classifier = SVC(kernel ='sigmoid') classifier.fit(x_train, y_train) # training set in x, y axis
 
Polynomial Kernel: It represents the similarity of vectors in the training set of data in a feature space over polynomials of the original variables used in the kernel.
多项式核函数将数据映射到高维度空间,并引入了多项式特征。多项式核函数允许SVM处理一定程度的非线性数据。
from sklearn.svm import SVC classifier = SVC(kernel ='poly', degree = 4) classifier.fit(x_train, y_train) # training set in x, y axis
 
Linear Kernel: used when data is linearly separable.
线性核函数是最简单的核函数,它在原始特征空间中执行内积操作,将数据从低维度映射到高维度,但不引入新的特征。它适用于线性可分的数据。
from sklearn.svm import SVC classifier = SVC(kernel ='linear') classifier.fit(x_train, y_train) # training set in x, y axis