#! /usr/bin/env python3 # def draft_radius ( ): from pandas import read_csv from sklearn.neighbors import RadiusNeighborsClassifier import matplotlib.pyplot as plt import numpy as np import pandas as pd print ( "draft_radius():" ) print ( " Demonstrate radius neighbor classification." ) filename = "draft.csv" df = pd.read_csv ( filename, header = 0 ) # # Print columns. # print ( df.columns ) # # Select Draft columns of yes, and of no. # dfdraft = df['Draft'] dfyes = df[df['Draft']=='yes'] dfno = df[df['Draft']=='no'] # # Save only the numeric data. # dfall = df._get_numeric_data ( ) dfyes = dfyes._get_numeric_data ( ) dfno = dfno._get_numeric_data ( ) # # Convert the data to numpy arrays. # npdraft = dfdraft.to_numpy ( ) npall = dfall.to_numpy ( ) npyes = dfyes.to_numpy ( ) npno = dfno.to_numpy ( ) # # Display the results. # plt.plot ( npyes[:,1], npyes[:,2], 'b^', markersize = 10 ) plt.plot ( npno[:,1], npno[:,2], 'kv', markersize = 10 ) plt.grid ( True ) plt.xlabel ( "Speed" ) plt.ylabel ( "Agility" ) plt.title ( "Athletic ratings for drafting" ) plt.legend ( ( "Drafted", "Rejected" ), loc = "upper left" ) filename = "draft_results.png" plt.savefig ( filename ) plt.show ( ) plt.close ( ) # # Introduce a new data item. # npmaybe = np.array ( [ 21, 6.75, 3.00 ] ) plt.plot ( npyes[:,1], npyes[:,2], 'b^', markersize = 10 ) plt.plot ( npno[:,1], npno[:,2], 'kv', markersize = 10 ) plt.plot ( npmaybe[1], npmaybe[2], 'r*', markersize = 10 ) plt.grid ( True ) plt.xlabel ( "Speed" ) plt.ylabel ( "Agility" ) plt.title ( "Athletic ratings for drafting" ) plt.legend ( ( "Drafted", "Rejected", "New Candidate" ), loc = "upper left" ) filename = "draft_candidate.png" plt.savefig ( filename ) plt.show ( ) plt.close ( ) # # Define the classifier. # for r in [ 1.0, 3.0, 5.0, 20.0]: neigh = RadiusNeighborsClassifier ( radius = r ) neigh.fit ( npall, npdraft ) try: npmaybedraft = neigh.predict( [ npmaybe ] ) print ( " For radius ", r, "Draft/no draft prediction:", npmaybedraft ) except: print ( " For radius ", r, "no nearby data was found." ) if ( __name__ == "__main__" ): draft_radius ( )