code: import pandas as pd
  from sklearn import datasets
  
  
  # load iris data set
  iris = datasets.load_iris()
  print(iris)
  
  species = [iris.target_names[x] for x in iris.target]
  
  iris = pd.DataFrame(iris['data'], columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']) 
  iris['Species'] = species
  
  
  iris.head()
  iris.dtypes
  
  
  # quick count
  iris['count'] = 1
  iris[['Species', 'count']].groupby('Species').count()
  iris.groupby('Species').count()
  
  
  
  # plot the data set 
  # %matplotlib inline
  def plot_iris(iris, col1, col2):
      print("plot_iris")
      import seaborn as sns
      import matplotlib.pyplot as plt
      sns.lmplot(x = col1, y=col2, 
      data = iris, 
      hue = "Species", 
      fit_reg=False)
      plt.xlabel(col1)
      plt.ylabel(col2)
      plt.title('Iris species show by color')
      plt.show() 
  
  plot_iris(iris, 'Petal_Width', 'Sepal_Length') 
  
  plot_iris(iris, 'Sepal_Width', 'Sepal_Length')
  
  
  # preparing numeric featurs by scaling
  
  from sklearn.preprocessing import scale
  
  import pandas as pd 
  
  num_cols = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']
  iris_scaled = scale(iris[num_cols])
  iris_scaled = pd.DataFrame(iris_scaled, columns = num_cols)
  print(iris_scaled.describe().round(3))
  
  # coding string col 'species' as numeric using a dictionary 
  levels = {'setosa':0, 
      'versicolor':1, 
      'virginica':2}
  
  # add coded species to the new scaled iris data frame 
  iris_scaled['Species'] = [levels[x] for x in iris['Species']]
  iris_scaled.head()
  plot_iris(iris_scaled, 'Sepal_Width', 'Sepal_Length')
  
  
  
  ## split the data into training and tes using Bernoulli sampling 
  
  from sklearn.model_selection import train_test_split
  import numpy as np
  
  np.random.seed(3456) 
  iris_split = train_test_split(np.asmatrix(iris_scaled), test_size = 75)
  
  iris_train_features = iris_split[0][:,:4]
  iris_train_labels = np.ravel(iris_split[0][:,4])
  
  iris_test_features = iris_split[1][:,:4]
  iris_test_labels = np.ravel(iris_split[1][:,4])
  
  print(iris_train_features.shape)
  print(iris_train_labels.shape)
  
  print(iris_test_features.shape)
  print(iris_test_labels.shape)
  
  # Train and Eval KNN model 
  
  #fit model 
  from sklearn.neighbors import KNeighborsClassifier
  KNN_mod = KNeighborsClassifier(n_neighbors=3) # this is K 
  KNN_mod.fit(iris_train_features, iris_train_labels)
  
  #test model on test data set
  iris_test = pd.DataFrame(iris_test_features, columns = num_cols)
  iris_test['predicted'] = KNN_mod.predict(iris_test_features)
  iris_test['actuals']  = iris_test_labels
  iris_test['correct'] = [1 if x == z else 0 for x, z in zip(iris_test['predicted'], iris_test_labels)]
  
  # calculate some accuracy measure 
  accuracy = 100 * float(sum(iris_test['correct'])) / float(iris_test.shape[0])
  print(accuracy)
  
  iris_test[iris_test.correct != 1]
  iris_test.loc[iris_test["correct"] != 1]
  
  
  
  
  # plotting the predicted values and highliting incorrectly classified observations 
  
  levels = {0:'setosa', 1:'versicolor', 2:'virginica'}
  iris_test['Species'] = [levels[x] for x in iris_test['predicted']]
  markers = {1:'^', 0:'o'}
  colors = {'setosa':'blue', 'versicolor':'green', 'virginica':'red'}
  def plot_shapes(df, col1,col2,  markers, colors):
      import matplotlib.pyplot as plt
      import seaborn as sns
      ax = plt.figure(figsize=(6, 6)).gca() # define plot axis
      for m in markers: # iterate over marker dictioary keys
          for c in colors: # iterate over color dictionary keys
              df_temp = df[(df['correct'] == m)  & (df['Species'] == c)]
              sns.regplot(x = col1, y = col2, 
                          data = df_temp,  
                          fit_reg = False, 
                          scatter_kws={'color': colors[c]},
                          marker = markers[m],
                          ax = ax)
      plt.xlabel(col1)
      plt.ylabel(col2)
      plt.title('Iris species by color')
      return 'Done'
  plot_shapes(iris_test, 'Petal_Width', 'Sepal_Length', markers, colors)
  plot_shapes(iris_test, 'Sepal_Width', 'Sepal_Length', markers, colors)
apis: matplotlib.pyplot.figure sklearn.preprocessing.scale pandas.DataFrame numpy.random.seed numpy.ravel matplotlib.pyplot.title numpy.asmatrix matplotlib.pyplot.show matplotlib.pyplot.ylabel sklearn.neighbors.KNeighborsClassifier matplotlib.pyplot.xlabel sklearn.datasets.load_iris