Python: Membuat Model Klasifikasi Support Vector Machines menggunakan Scikit-learn

Berikut merupakan teknik untuk membuat model prediksi menggunakan teknik Support Vector Machines.

from sklearn import datasets
from sklearn import svm
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
import _pickle as pickle
import requests, json

iris = datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y)

clf = svm.SVC()
clf.fit(iris.data, iris.target).predict(iris.data)

print("Accuracy = %0.2f" % accuracy_score(y_test, clf.predict(X_test)))
print(classification_report(y_test, clf.predict(X_test)))

pickle.dump(clf, open("iris_svm.pkl", "wb"))
my_support_vector_machines = pickle.load(open("iris_svm.pkl", "rb"))

print("Accuracy = %0.2f" % accuracy_score(y_test, my_support_vector_machines.predict(X_test)))
print(classification_report(y_test, my_support_vector_machines.predict(X_test)))

Python: Membuat Model Klasifikasi Gaussian Naïve Bayes menggunakan Scikit-learn

Berikut merupakan teknik untuk membuat model prediksi menggunakan teknik Gaussian Naïve Bayes.

from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
import _pickle as pickle
import requests, json

iris = datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y)

gnb = GaussianNB()
gnb.fit(iris.data, iris.target).predict(iris.data)

print("Accuracy = %0.2f" % accuracy_score(y_test, gnb.predict(X_test)))
print(classification_report(y_test, gnb.predict(X_test)))

pickle.dump(gnb, open("iris_gnb.pkl", "wb"))
my_gaussian_naive_bayes = pickle.load(open("iris_gnb.pkl", "rb"))

print("Accuracy = %0.2f" % accuracy_score(y_test, my_gaussian_naive_bayes.predict(X_test)))
print(classification_report(y_test, my_gaussian_naive_bayes.predict(X_test)))