-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEX_Support-Vector-Machine-R.py
More file actions
77 lines (48 loc) · 2.18 KB
/
Copy pathEX_Support-Vector-Machine-R.py
File metadata and controls
77 lines (48 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# coding: utf-8
""" # Support-Vector-Machine-Regression """
# #### Key Features :
# #### Epsilon-Insensitive Loss Function: SVR uses an epsilon-insensitive loss function, which means it ignores errors that fall within a specified margin (epsilon). This allows the model to be robust to small deviations and focuses on fitting the data points that are outside this margin.
# #### Support Vectors: Just like in SVM, SVR identifies support vectors, which are the data points that are closest to the predicted function (hyperplane). These points are critical because they influence the position and orientation of the hyperplane.
# #### Kernel Trick: SVR can handle non-linear relationships between input features and output by applying kernel functions.
# Common kernels include:
# #### Linear Kernel: For linear relationships.
# #### Polynomial Kernel: For polynomial relationships.
# #### Radial Basis Function (RBF) Kernel: For more complex, non-linear relationships.
""" Practicle Implementation """
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
# load the dataset
dataset = pd.read_csv('Position_Salaries.csv')
dataset
plt.scatter(dataset['Level'],dataset['Salary'])
plt.xlabel('Level')
plt.ylabel('Salary')
plt.show()
# transform the dataset into Rows and Cloumn
X = dataset.iloc[:,[1]].values
X
# target variable
Y = dataset.iloc[:,2].values
Y
# import the SVR packages
from sklearn.svm import SVR
# linear,sigmoid,poly,rbf
model = SVR(kernel='poly',degree=8)
# 'poly' indicates that a polynomial kernel will be employed.
# This kernel allows the model to learn non-linear relationships by transforming the input space into a higher-dimensional space using polynomial functions.
# degree=8: This parameter sets the degree of the polynomial kernel.
# A degree of 8 means that the polynomial function used in the kernel will be of the 8th order.
# traine the model
model.fit(X,Y)
model.score(X,Y)
plt.scatter(dataset['Level'],dataset['Salary'])
plt.plot(X,model.predict(X),color='red')
plt.xlabel('Level')
plt.ylabel('Salary')
plt.show()
# manually prediction
model.predict(np.array([[6.5]]))