Professional Documents
Culture Documents
2.4 SVR
2.4 SVR
kernel rbf , linear , poly , sigmoid , precomputedالمعادلة المستخدمة فيها ,وتكون
degree درجة المعادلة اذا تم استخدام بولي
max_iter العدد األقصي للمحاوالت
C معامل الجزاء للخطأN
epsilon قيمة ابسلون المستخدمة
2
_intercept التقاطع مع محور اكس
3
الصيغة العامة
#Import Libraries
from sklearn.svm import SVR
#----------------------------------------------------
'''
sklearn.svm.SVR(kernel=’rbf’, degree=3, gamma=’auto_deprecated’, coef0=0.0, tol=0.001,
C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False,max_iter=-1)
'''
SVRModel = SVR(C = 1.0 ,epsilon=0.1,kernel = 'rbf') # it also can be : linear, poly, rbf, sigmoid, precomputed
SVRModel.fit(X_train, y_train)
#Calculating Details
print('SVRModel Train Score is : ' , SVRModel.score(X_train, y_train))
print('SVRModel Test Score is : ' , SVRModel.score(X_test, y_test))
print('----------------------------------------------------')
4
#Calculating Prediction
y_pred = SVRModel.predict(X_test)
print('Predicted Value for SVRModel is : ' , y_pred[:10])
5
مثال
#Import Libraries
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import median_absolute_error
#----------------------------------------------------
BostonData = load_boston()
#X Data
X = BostonData.data
#y Data
y = BostonData.target
#----------------------------------------------------
6
#Splitting data
#----------------------------------------------------
#Applying SVR Model
'''
sklearn.svm.SVR(kernel=’rbf’, degree=3, gamma=’auto_deprecated’, coef0=0.0, tol=0.001,
C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False,max_iter=-1)
'''
SVRModel = SVR(C = 1.0 ,epsilon=0.1,kernel = 'rbf') # it also can be : linear, poly, rbf, sigmoid, precomputed
SVRModel.fit(X_train, y_train)
#Calculating Details
print('SVRModel Train Score is : ' , SVRModel.score(X_train, y_train))
print('SVRModel Test Score is : ' , SVRModel.score(X_test, y_test))
print('----------------------------------------------------')
#Calculating Prediction
y_pred = SVRModel.predict(X_test)
7
print('Predicted Value for SVRModel is : ' , y_pred[:10])
#----------------------------------------------------
#Calculating Mean Absolute Error
MAEValue = mean_absolute_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values
print('Mean Absolute Error Value is : ', MAEValue)
#----------------------------------------------------
#Calculating Mean Squared Error
MSEValue = mean_squared_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values
print('Mean Squared Error Value is : ', MSEValue)
#----------------------------------------------------
#Calculating Median Squared Error
MdSEValue = median_absolute_error(y_test, y_pred)
print('Median Squared Error Value is : ', MdSEValue )
8
مثال
from sklearn.svm import SVR
import numpy as np
n_samples=10
n_features = 10
np.random.seed(0)
y = np.random.randn(n_samples)
X = np.random.randn(n_samples, n_features)
X.shape
y
clf = SVR(gamma='scale', C=1.0, epsilon=0.2)
clf.fit(X, y)
newx = np.random.randn(1,10)
y_pred = clf.predict(newx)
9
Earthquakes : مثال
import numpy as np
import pandas as pd
dataset = pd.read_csv('Earthquakes.csv')
dataset.head(20)
X = dataset[:, :-1]
10
y = dataset[:, -1]
X
y
y_pred = clf.predict([[90,12,12,-5,54,0.3,0.9,3.5,16.2,10]])
y_pred
11
SAT : مثال
import numpy as np
import pandas as pd
dataset = pd.read_csv('satf.csv')
dataset.head(20)
X = dataset[:, :-1]
y = dataset[:, -1]
12
X
y
y_pred = clf.predict([[3.48,684,649,3.61]])
y_pred
13
SVR in Houses : مثال
# Importing the libraries
import numpy as np
import pandas as pd
dataset = pd.read_csv('houses.csv')
dataset.head(20)
X = dataset[:, :-1]
y = dataset[:, -1]
X
y
14
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
X_train
X_test
y_train
y_test
y_pred = clf.predict(X_test)
y_pred
16
SVR vs Linear Regression : مثال
import random
import math
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
# make data
random.seed(123)
def getData(N):
x,y = [],[]
for i in range(N):
a = i/10+random.uniform(-1,1)
yy = math.sin(a)+3+random.uniform(-1,1)
x.append([a])
y.append([yy])
return np.array(x), np.array(y)
17
x,y = getData(200)
######################################################
model = LinearRegression()
print(model)
model.fit(x,y)
pred_y = model.predict(x)
x_ax=range(200)
plt.scatter(x_ax, y, s=5, color="blue", label="original")
plt.plot(x_ax, pred_y, lw=1.5, color="red", label="predicted")
plt.legend()
plt.show()
score=model.score(x,y)
18
print('score = ' ,score)
rmse = math.sqrt(mse)
print("Root Mean Squared Error:", rmse)
######################################################
#apply SVR
model.fit(x,y)
pred_y = model.predict(x)
19
x_ax=range(200)
plt.scatter(x_ax, y, s=5, color="blue", label="original")
plt.plot(x_ax, pred_y, lw=1.5, color="red", label="predicted")
plt.legend()
plt.show()
score=model.score(x,y)
print('score = ' ,score)
rmse = math.sqrt(mse)
print("Root Mean Squared Error:", rmse)
20
SVR vs Linear Regression : مثال آخر
#Import Libraries
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import median_absolute_error
from sklearn.svm import SVR
#----------------------------------------------------
BostonData = load_boston()
#X Data
X = BostonData.data
#print('X Data is \n' , X[:10])
#print('X shape is ' , X.shape)
#print('X Features are \n' , BostonData.feature_names)
21
#y Data
y = BostonData.target
#print('y Data is \n' , y[:10])
#print('y shape is ' , y.shape)
#----------------------------------------------------
#Splitting data
#Splitted Data
#print('X_train shape is ' , X_train.shape)
#print('X_test shape is ' , X_test.shape)
#print('y_train shape is ' , y_train.shape)
#print('y_test shape is ' , y_test.shape)
#----------------------------------------------------
#Applying Linear Regression Model
#Calculating Prediction
y_pred = LinearRegressionModel.predict(X_test)
print('Predicted Value for Linear Regression is : ' , y_pred[:10])
#----------------------------------------------------
#Calculating Mean Absolute Error
MAEValue = mean_absolute_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values
print('Mean Absolute Error Value is : ', MAEValue)
#----------------------------------------------------
#Calculating Mean Squared Error
MSEValue = mean_squared_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values
print('Mean Squared Error Value is : ', MSEValue)
23
#----------------------------------------------------
#Calculating Median Squared Error
MdSEValue = median_absolute_error(y_test, y_pred)
print('Median Squared Error Value is : ', MdSEValue )
#----------------------------------------------------
#Applying SVR Model
'''
#sklearn.svm.SVR(kernel=’rbf’, degree=3, gamma=’auto_deprecated’, coef0=0.0, tol=0.001,
# C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False,max_iter=-1)
'''
print('==================================================================================='
)
SVRModel = SVR(C = 1.0 ,epsilon=0.1,kernel = 'rbf') # it also can be : linear, poly, rbf, sigmoid, precomputed
SVRModel.fit(X_train, y_train)
#Calculating Details
print('SVRModel Train Score is : ' , SVRModel.score(X_train, y_train))
print('SVRModel Test Score is : ' , SVRModel.score(X_test, y_test))
24
print('----------------------------------------------------')
#Calculating Prediction
y_pred = SVRModel.predict(X_test)
print('Predicted Value for SVRModel is : ' , y_pred[:10])
#----------------------------------------------------
#Calculating Mean Absolute Error
MAEValue = mean_absolute_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values
print('Mean Absolute Error Value is : ', MAEValue)
#----------------------------------------------------
#Calculating Mean Squared Error
MSEValue = mean_squared_error(y_test, y_pred, multioutput='uniform_average') # it can be raw_values
print('Mean Squared Error Value is : ', MSEValue)
#----------------------------------------------------
#Calculating Median Squared Error
MdSEValue = median_absolute_error(y_test, y_pred)
print('Median Squared Error Value is : ', MdSEValue )
25