In [1]:
from sklearn import svm
from sklearn import datasets
# load the Support vector machine algorithm
clf = svm.SVC()
# iris is a classic dataset that comes with sklearn. It has Iris flower features and the resulting type of
# Iris. See https://archive.ics.uci.edu/ml/datasets/Iris for the details.
iris = datasets.load_iris()
# X (iris.data is a list of sizes of a flower. There are 4 features of the form [5.6, 3.0, 4.1, 1.3] )
# y is the type of flower (0,1,2) for (Iris Setosa, Iris Versicolour, Iris Virginica)
X, y = iris.data, iris.target
# create a trained model with the iris data
clf.fit(X,y)
Out[1]:
In [2]:
# Save the trained model to disk so we can use the model at a later time to predict the class of a new flower
from sklearn.externals import joblib
joblib.dump(clf, 'clf_trained.pkl')
Out[2]:
In [9]:
# at some time later, or even in a different program (this is why the 2nd occurance of the import)
from sklearn.externals import joblib
clf_trained = joblib.load('clf_trained.pkl')
# Use the trained model to predict the Iris species based on the features of "new" flowers
new_flower_a = [ 5.3, 3.0, 4.1, 1.2]
new_flower_b = [ 3.5, 2.1, 2.1, 1.5]
new_flower_c = [10.5, 2.1, 2.1, 1.5]
y_hat_a = clf_trained.predict( new_flower_a)
# What is the predicted species of the flower based on it's feature values?
print ("predicted species is: {0:d}".format(y_hat_a[0]))
y_hat_b = clf_trained.predict( new_flower_b)
# What is the predicted species of the flower based on it's feature values?
print ("predicted species is: {0:d}".format(y_hat_b[0]))
y_hat_c = clf_trained.predict( new_flower_c)
# What is the predicted species of the flower based on it's feature values?
print ("predicted species is: {0:d}".format(y_hat_c[0]))
In [ ]: