SVR regression

In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [2]:
dataset = pd.read_csv('Position_Salaries.csv')
dataset.head(2)
Out[2]:
Position Level Salary
0 Business Analyst 1 45000
1 Junior Consultant 2 50000
In [3]:
x = dataset.iloc[:,1:2].values
y = dataset.iloc[:,2].values
type(x)
Out[3]:
numpy.ndarray
In [4]:
#before feature scaling of x and y
from sklearn.svm import SVR
reg = SVR(kernel = "rbf")
reg.fit(x,y)
y_pred = reg.predict(6.5)
print(y_pred)

plt.scatter(x,y)
plt.plot(x,reg.predict(x))
[130001.55760156]
Out[4]:
[<matplotlib.lines.Line2D at 0x1f5a0b7ecf8>]
In [5]:
#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)

from sklearn.svm import SVR
reg = SVR(kernel = "rbf")
reg.fit(x,y)
y_pred = sc_y.inverse_transform(reg.predict(sc_x.transform(np.array([[6.5]]))))
print(y_pred)
plt.scatter(x,y,color = 'r')
plt.plot(x,reg.predict(x))
[170370.0204065]
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:420: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:420: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:420: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py:583: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:420: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
  warnings.warn(msg, DataConversionWarning)
C:\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py:646: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
Out[5]:
[<matplotlib.lines.Line2D at 0x1f59915ca20>]
In [ ]:
 
In [ ]:
 





Comments