Example : SVR
Problem Statement
Predict salary based on years of experience using Support Vector Regression.
Dataset
| Experience | Salary |
|---|---|
| 1 | 15000 |
| 2 | 20000 |
| 3 | 28000 |
| 4 | 40000 |
| 5 | 60000 |
| 6 | 80000 |
Step 1: Import Libraries
import pandas as pd
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
Step 2: Create Dataset
data = {
"Experience": [1, 2, 3, 4, 5, 6],
"Salary": [15000, 20000, 28000, 40000, 60000, 80000]
}
df = pd.DataFrame(data)
print(df)
Step 3: Separate Input and Output
X = df[["Experience"]]
y = df[["Salary"]]
Here:
-
X = Experience
-
y = Salary
Step 4: Apply Feature Scaling
SVR is sensitive to scale, so we scale both input and output.
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X_scaled = scaler_X.fit_transform(X)
y_scaled = scaler_y.fit_transform(y)
Here:
- StandardScaler() creates a scaling object
- fit_transform(X) calculates mean and standard deviation, then scales the input values
- X_scaled contains normalized experience values
Step 5: Train SVR Model
model = SVR(kernel="rbf")
model.fit(X_scaled, y_scaled.ravel())
Here:
-
kernel="rbf" helps handle nonlinear patterns.
-
.ravel() converts target values into a 1D array.
Step 6: Predict Salary for 7 Years Experience
new_experience = [[7]]
new_experience_scaled = scaler_X.transform(new_experience)
predicted_salary_scaled = model.predict(new_experience_scaled)
predicted_salary = scaler_y.inverse_transform(
predicted_salary_scaled.reshape(-1, 1)
)
print("Predicted Salary:", predicted_salary[0][0])
In this step, we predict the salary for a person with 7 years of experience. First, new_experience = [[7]] creates the new input value in 2D format because Scikit-Learn expects input data as rows and columns. Next, scaler_X.transform() converts the new experience value into scaled form using the same scaling applied during training. The SVR model then predicts the salary using this scaled input, and the result is stored in predicted_salary_scaled. Since the predicted value is still in scaled form, inverse_transform() converts it back to the original salary scale so that the result becomes understandable. Finally, print(predicted_salary[0][0]) displays the actual predicted salary value
Complete Program
import pandas as pd
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
# Dataset
data = {
"Experience": [1, 2, 3, 4, 5, 6],
"Salary": [15000, 20000, 28000, 40000, 60000, 80000]
}
df = pd.DataFrame(data)
# Input and output
X = df[["Experience"]]
y = df[["Salary"]]
# Feature scaling
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X_scaled = scaler_X.fit_transform(X)
y_scaled = scaler_y.fit_transform(y)
# SVR model
model = SVR(kernel="rbf")
model.fit(X_scaled, y_scaled.ravel())
# Prediction
new_experience = [[7]]
new_experience_scaled = scaler_X.transform(new_experience)
predicted_salary_scaled = model.predict(new_experience_scaled)
predicted_salary = scaler_y.inverse_transform(
predicted_salary_scaled.reshape(-1, 1)
)
print("Predicted Salary:", predicted_salary[0][0])
Output
Predicted Salary: around 68000 to 75000
The exact value may vary depending on the SVR parameters.
SVR first scales the data because salary values are much larger than experience values. Then it uses the RBF kernel to learn the nonlinear relationship between experience and salary. After prediction, the scaled salary is converted back to the original salary range using inverse_transform().
Summary
SVR is useful when the relationship between input and output is nonlinear. In this example, salary does not increase in a perfectly straight line, so SVR can model the pattern better than a simple linear model.