import numpy as np
import pandas as pd
url = "https://DataScienceSchools.github.io/Machine_Learning/Sklearn/Case_Study/PowerPlant/PowerPlant.csv"
df = pd.read_csv(url)
df.head()
AT | V | AP | RH | PE | |
---|---|---|---|---|---|
0 | 8.34 | 40.77 | 1010.84 | 90.01 | 480.48 |
1 | 23.64 | 58.49 | 1011.40 | 74.20 | 445.75 |
2 | 29.74 | 56.90 | 1007.15 | 41.91 | 438.76 |
3 | 19.07 | 49.69 | 1007.22 | 76.79 | 453.09 |
4 | 11.80 | 40.66 | 1017.13 | 97.20 | 464.43 |
X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
y = y.reshape(len(y),1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
sc_y = StandardScaler()
y_train = sc_y.fit_transform(y_train)
from sklearn.svm import SVR
model = SVR(kernel = 'rbf')
model.fit(X_train, y_train)
/home/bahar/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
SVR()
y_pred = sc_y.inverse_transform(model.predict(sc_X.transform(X_test)))
data = pd.DataFrame()
pd.set_option('precision', 2)
data['Predicted_Y'] = y_pred
data['Real_Y'] = y_test
data
Predicted_Y | Real_Y | |
---|---|---|
0 | 431.22 | 426.18 |
1 | 448.88 | 451.10 |
2 | 444.04 | 442.87 |
3 | 446.71 | 443.70 |
4 | 460.43 | 460.59 |
... | ... | ... |
1909 | 467.73 | 468.19 |
1910 | 433.68 | 431.16 |
1911 | 455.18 | 454.20 |
1912 | 447.12 | 444.13 |
1913 | 432.72 | 436.58 |
1914 rows × 2 columns
from sklearn.metrics import r2_score
r2_score(y_test, y_pred)
0.945384075850576