1 - Data analysis
1.1 - Load the data
1.2 - Manipulating the data
1.3 - Visualizing the data
2 - Machine Learning
2.1 - Test predictions in data input
2.2 Test result prediction
# Python version
import sys
print('Python: {}'.format(sys.version))
# scipy
import scipy
print('scipy: {}'.format(scipy.__version__))
# numpy
import numpy
print('numpy: {}'.format(numpy.__version__))
# matplotlib
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
# pandas
import pandas
print('pandas: {}'.format(pandas.__version__))
# scikit-learn
import sklearn
print('sklearn: {}'.format(sklearn.__version__))
# Load libraries
import seaborn
import pandas
from pandas.tools.plotting import scatter_matrix
import matplotlib.pyplot as plt
from sklearn import cross_validation
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
1.1 Load the data
# Load dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length','sepal-width','petal-length','petal-width','class']
dataset = pandas.read_csv(url, names=names)
1.2 Manipulating the data
print(dataset.head(5))
print(dataset.shape)
print(dataset.describe())
1.3 Visualizing the data
dataset.plot(kind='box', subplots=False, layout=(2,2), sharex=False, sharey=False,figsize=(15,10))
plt.show()
seaborn.pairplot(dataset, hue="class", size=3, diag_kind="kde")
plt.show()
seaborn.pairplot(dataset, hue="class", size = 3)
seaborn.set()
dataset.hist(figsize=(15,10))
plt.show()
2 Machine Learning
# Split-out validation dataset
array = dataset.values
X = array[:,0:4]
Y = array[:,4]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = cross_validation.train_test_split(X, Y, test_size=validation_size,
random_state=seed)
# Test options and evaluation metric
num_folds = 10
num_instances = len(X_train)
seed = 7
scoring = 'accuracy'
#Here we are testing various predictive algorithms from scikit-learn
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
# evaluate each model in turn
results = []
names = []
for name, model in models:
kfold = cross_validation.KFold(n=num_instances, n_folds=num_folds, random_state=seed)
cv_results = cross_validation.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
# Compare Algorithms
fig = plt.figure(figsize=(15,10))
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()
# Make predictions on validation dataset
svn = SVC()
svn.fit(X_train, Y_train)
predictions = svn.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
2.1 Test predictions in data input
#Input Vector
X_new = numpy.array([[5.1,3.5,1.4,0.2], [3,4,5,1]])
print("X_new.shape: {}".format(X_new.shape))
prediction = svn.predict(X_new)
2.1 Test result prediction
#Prediction of the species from the input vector
print("Prediction of Species: {}".format(prediction))
from IPython.display import Image
print("Prediction of the The Iris Flower")
if prediction[0]== 'Iris-virginica':
display(Image("virginica.jpg"))
print("Iris-virginica")
print(X_new[0])
elif prediction[0] == 'Iris-setosa':
display(Image("setosas.jpg"))
print("Iris-Setosa")
print(X_new[0])
else :
display(Image("versicolor.jpg"))
print("Iris-Versicolor")
print(X_new[0])
if prediction[1]== 'Iris-virginica':
display(Image("virginica.jpg"))
print("Iris-virginica")
print(X_new[1])
elif prediction[1] == 'Iris-setosa':
display(Image("setosas.jpg"))
print("Iris-Setosa")
print(X_new[1])
elif prediction[1] == 'Iris-versicolor':
display(Image("versicolor.jpg"))
print("Iris-Versicolor")
print(X_new[1])
Thanks!!