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]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

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]:
['clf_trained.pkl',
 'clf_trained.pkl_01.npy',
 'clf_trained.pkl_02.npy',
 'clf_trained.pkl_03.npy',
 'clf_trained.pkl_04.npy',
 'clf_trained.pkl_05.npy',
 'clf_trained.pkl_06.npy',
 'clf_trained.pkl_07.npy',
 'clf_trained.pkl_08.npy',
 'clf_trained.pkl_09.npy',
 'clf_trained.pkl_10.npy',
 'clf_trained.pkl_11.npy']

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]))


predicted species is: 1
predicted species is: 0
predicted species is: 2
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)

In [ ]: