<jupyter_start><jupyter_text>The quintessential iris dataset<jupyter_code>from sklearn import datasets
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.learning_curve import learning_curve
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Getting dataLoad the dataset from scikit-learn and perform a test train split<jupyter_code>iris = datasets.load_iris()
X, Y = iris.data, iris.target
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target)<jupyter_output><empty_output><jupyter_text>Fitting a modelLet's try with KNN. Fit the training data and test on testing dataset<jupyter_code>model = KNeighborsClassifier()
model.fit(x_train, y_train)
y_hat = model.predict(x_test)<jupyter_output><empty_output><jupyter_text>Validating the model fit<jupyter_code>conf_matrix = confusion_matrix(y_test, y_hat, labels=[0,1,2])
plt.imshow(conf_matrix, interpolation="nearest")
accuracy_score(y_test, y_hat) #94.73! not bad!<jupyter_output><empty_output><jupyter_text>Check if the we can reduce the size of training set and still get decent accuracy<jupyter_code>train_sizes, train_scores, test_scores = learning_curve(model, X, Y, cv=5)
plt.plot(train_sizes, np.mean(train_scores, axis=1),'o-', label="Training score")
plt.plot(train_sizes, np.mean(test_scores, axis=1),'o-', label="Testing score")
plt.legend(loc="lower right")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup