Mari kita lihat bagaimana menggunakan konsep SVM untuk membangun regressor untuk memperkirakan harga rumah. Kita akan menggunakan dataset yang tersedia di sklearn dimana setiap titik data ditentukan, dengan 13 atribut.
Bertujuan untuk memperkirakan harga rumah berdasarkan atribut.
1. Buat file Python baru dan impor paket berikut:
import numpy as np
from sklearn import datasets
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error, explained_variance_score
from sklearn.utils import shuffle
# Load housing data
data = datasets.load_boston()
# Shuffle the data
X, y = shuffle(data.data, data.target, random_state=7)
# Split the data into training and testing datasets
num_training = int(0.8 * len(X))
X_train, y_train = X[:num_training], y[:num_training]
X_test, y_test = X[num_training:], y[num_training:]
# Create Support Vector Regression model
sv_regressor = SVR(kernel='linear', C=1.0, epsilon=0.1)
# Train Support Vector Regressor
sv_regressor.fit(X_train, y_train)
# Evaluate performance of Support Vector Regressor
y_test_pred = sv_regressor.predict(X_test)
mse = mean_squared_error(y_test, y_test_pred)
evs = explained_variance_score(y_test, y_test_pred)
print("\n#### Performance ####")
print("Mean squared error =", round(mse, 2))
print("Explained variance score =", round(evs, 2))
# Test the regressor on test datapoint
test_data = [3.7, 0, 18.4, 1, 0.87, 5.95, 91, 2.5052, 26, 666, 20.2, 351.34, 15.27]
print("\nPredicted price:", sv_regressor.predict([test_data])[0])
2. Jika kamu menjalankan kode ini, kamu akan melihat yang berikut ini tercetak di Terminal:
3. Sekian tutorialnya, tetap simak artikel saya selanjutnya
Referensi
- Prateek Joshi. 2017. Artificial Intelligence with Python-Build real-world Artificial Intelligence applications with Python to intelligently interact with the world around you. Birmingham, Mumbai
0 Komentar