#! /usr/bin/env python3 # def blob_classify_logistic_multi ( ): #*****************************************************************************80 # ## blob_classify_logistic_multi() repeatedly applies logistic regression. # # Discussion: # # The data involves three groups of "blobs". # # By applying logistic regression 3 times, we can find boundaries between # each group of blobs and the other two. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 07 July 2023 # # Author: # # John Burkardt # from sklearn.svm import LinearSVC from sklearn.datasets import make_blobs import matplotlib.pyplot as plt import mglearn import numpy as np import platform import sklearn print ( '' ) print ( 'blob_classify_logistic_multi()' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' scikit-learn version %s' % ( sklearn.__version__ ) ) print ( ' Use LogisticRegression() on three sets of blobs.' ) print ( ' Find a line between each blob and the other two.' ) # # Define the data. # X, y = make_blobs ( random_state = 42 ) # # Plot the three groups of blobs. # plt.clf ( ) mglearn.discrete_scatter ( X[:,0], X[:,1], y ) plt.xlabel ( 'Feature 0' ) plt.ylabel ( 'Feature 1' ) plt.legend ( [ "Class 0", "Class 1", "Class 2" ] ) plt.grid ( True ) filename = 'blob_data.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Define the model. # linear_svm = LinearSVC ( ).fit ( X, y ) print ( "" ) print ( " Coefficient shape: ", linear_svm.coef_.shape ) print ( " Intercept shape: ", linear_svm.intercept_.shape ) # # Plot the results. # plt.clf ( ) mglearn.discrete_scatter ( X[:,0], X[:,1], y ) line = np.linspace ( -15, 15 ) for coef, intercept, color in zip ( linear_svm.coef_, linear_svm.intercept_, mglearn.cm3.colors ): plt.plot ( line, - ( line * coef[0] + intercept ) / coef[1], c = color ) plt.ylim ( -10, 15 ) plt.xlim ( -10, 8 ) plt.xlabel ( 'Feature 0' ) plt.ylabel ( 'Feature 1' ) plt.legend ( [ "Class 0", "Class 1", "Class 2", "Line class 0", "Line class 1", "Line class2" ], loc = ( 1.01, 0.3 ) ) filename = 'blob_boundaries.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Terminate. # print ( '' ) print ( 'blob_classify_logistic_multi()' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) blob_classify_logistic_multi ( ) timestamp ( )