#! /usr/bin/env python3 # def faithful_classify ( ): #*****************************************************************************80 # ## faithful_classify() uses a KMeans model to classify new data. # # Discussion: # # Clustering data. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 September 2023 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np import platform from sklearn.cluster import KMeans print ( '' ) print ( 'faithful_kmeans_classify():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Use a KMeans model to classify new data.' ) # # Read the data. # data = np.loadtxt ( 'faithful_normalized.txt' ) # # Create x and y. # x = data[:,0] y = data[:,1] # # Call kmeans() # k = 2 kmeans = KMeans ( n_clusters = k, n_init = 'auto' ) kmeans.fit ( data ) Z = kmeans.cluster_centers_ C = kmeans.labels_ # # Create a few new data items # data2 = np.array ( [ [-0.1, 0.3 ], [ 0.3, 0.3 ], [ 0.4, 0.8 ], [ 0.9, 0.5 ] ] ) x2 = data2[:,0] y2 = data2[:,1] # # Classify them using the KMeans clusters. # C2 = kmeans.predict ( data2 ) # # Display the results. # plt.clf ( ) plt.plot ( data[C==0,0], data[label==0,1], 'c.', markersize = 10 ) plt.plot ( data[C==1,0], data[label==1,1], 'm.', markersize = 10 ) plt.plot ( data2[C2==0,0], data2[C2==0,1], 'c*', markersize = 20 ) plt.plot ( data2[C2==1,0], data2[C2==1,1], 'm*', markersize = 20 ) plt.plot ( Z[0,0], Z[0,1], 'bo', markersize = 15 ) plt.plot ( Z[1,0], Z[1,1], 'ro', markersize = 15 ) plt.xlabel ( '<-- Duration -->', fontsize = 16 ) plt.ylabel ( '<-- Wait -->', fontsize = 16 ) plt.title ( 'Classify new data using kmeans()', fontsize = 16 ) plt.grid ( True ) filename = 'faithful_classify.png' plt.savefig ( filename ) plt.show ( ) plt.close ( ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) # # Terminate. # print ( '' ) print ( 'faithful_classify():' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): faithful_classify ( )