<jupyter_start><jupyter_text>Classification Using Logistic Regression, SVM and Random Forrest  This trains the model with all the training data and uses the test data to find the test performance Each classifers are trained and tested for RGB, HSV and Lab Color Spaces. Here, the mean color values of the images are used as input features.         To obtain consistent channel values across images captured at different conditions. Both the Sample Zone and Reference are used for input features. This somwhat rejects the inconsitency introduced due to illumination variance. The notion is both sample and reference zone will have same effect of illumination, so their difference will result in color constancy.<jupyter_code>### loads the xlsx file containing mean RGB data and returns the difference value of each RGB channel,i.e Sample - Reference

def load_RGB():
    # load RGB channel data
    data= pd.read_csv('C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/Data/new data/mean_pixel_RGB_2400.csv')
    
    #data= pd.read_csv('C:/Users/<NAME>/Desktop/Food Color Constant Distance and plane of angle/Indoor_Daylight_Closed_Curtains/Labels.csv')
    #data = data[(data['Camera']==1)]
    replace_values= { 1:11, 2 : 11, 3 : 11, 4 : 12, 5 : 12, 6 : 12 , 7 : 12, 8 : 13, 9 : 13, 10 : 13} # fuse the labels
    data.replace({'Dilution Factor' : replace_values}, inplace = True)
    
    # encode the label values into integer type
    lab_enc = preprocessing.LabelEncoder()
    Label = lab_enc.fit_transform(data['Dilution Factor'])
   

    # in each channel, calculate "sample-reference" values

    data['Diff_Red']= data['RedSample']-data['RedReference']
    data['Diff_Green']= data['GreenSample']-data['GreenReference']
    data['Diff_Blue']= data['BlueSample']-data['BlueReference']

    Diff_values= data[['Diff_Red','Diff_Green','Diff_Blue']]
    Values= data[['BlueReference','BlueSample','GreenReference','GreenSample','RedReference','RedSample']]
    Values_sample= data[['BlueSample','GreenSample','RedSample']]
    #Values = (Values- [9.08283115,  71.20775448, 124.16239099, 156.00875213, 177.02573369, 171.91121167])/[ 8.08678345, 51.96839949, 29.57453969, 31.35167548, 29.29973181, 29.4546092 ]
    #Values = (Values - Values.values.mean(axis=0, dtype='float64'))/Values.values.std(axis=0, dtype='float64')
    return Label, Values.values

#### loads the xlsx file containing mean HSV values and returns the difference of values, i.e, Sample- Reference of each value
def load_HSV():
    # load HSV channel data
    data= pd.read_csv('C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/Data/new data/mean_pixel_HSV_2400.csv')
    replace_values= { 1:11, 2 : 11, 3 : 11, 4 : 12, 5 : 12, 6 : 12 , 7 : 12, 8 : 13, 9 : 13, 10 : 13} # fuse the labels
    data.replace({'Dilution Factor' : replace_values}, inplace = True)

    # encode the label values into integer type
    lab_enc = preprocessing.LabelEncoder()
    Label = lab_enc.fit_transform(data['Dilution Factor'])
    

    # in each channel, calculate "sample-reference" values ,here we have used sample- refernce instead of reference - sample. The relation is same, just the sign is reversed.
    # This is done just to obtain the position value of H value.

    data['Diff_H']= data['HSample']-data['HReference']
    data['Diff_S']= data['SSample']-data['SReference']
    data['Diff_V']= data['VSample']-data['VReference']

    Diff_values= data[['Diff_H','Diff_S','Diff_V']]
    Values= data[['HReference','HSample','SReference','SSample','VReference','VSample']]
    Values_sample= data[['HSample','SSample','VSample']]

    return Label, Values.values
#### loads the xlsx file containing mean LAB values and returns the difference, i.e,  Reference- sample of each value
def load_LAB():
    # load Lab channel data
    data= pd.read_csv('C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/Data/new data/mean_pixel_LAB_2400.csv')
    replace_values= { 1:11, 2 : 11, 3 : 11, 4 : 12, 5 : 12, 6 : 12 , 7 : 12, 8 : 13, 9 : 13, 10 : 13} # fuse the labels
    data.replace({'Dilution Factor' : replace_values}, inplace = True)

    # encode the label values into integer type
    lab_enc = preprocessing.LabelEncoder()
    Label = lab_enc.fit_transform(data['Dilution Factor'])
    


    # in each channel, calculate "reference-sample" values ,here we have used reference- sample instead of sample - reference. The relation is same, just the sign is reversed.
    # This is done just to obtain the position differnces

    data['Diff_L']= data['LReference']-data['LSample']
    data['Diff_a']= data['aReference']-data['aSample']
    data['Diff_b']= data['bReference']-data['bSample']

    Diff_values= data[['Diff_L','Diff_a','Diff_b']]
    Values = data [['LReference','LSample','aReference','aSample','bReference','bSample']]
    Values_sample= data[['LSample','aSample','bSample']]

    return Label, Values.values<jupyter_output><empty_output><jupyter_text>Plot Confusion Matrix to Visualize<jupyter_code>#### function to plot confusion matrix

def plot_confusion_matrix(y_true, y_pred, classes,
                          normalize=False,
                          title=None,
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        else:
            title = 'Confusion matrix, without normalization'

    # Compute confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    
    #transpose the matrix to make x-axis True Class and Y-axis Predicted Class
    cm= np.transpose(cm)
    
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    #print(cm)

    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[0]),
           yticks=np.arange(cm.shape[1]),
           # ... and label them with the respective list entries
           xticklabels=classes, yticklabels=classes,
           
           #here we are not printing the title
           #title=title,
           xlabel='True label',
           ylabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
    fig.tight_layout()
    return fig,ax<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>##### this function classifies the data using the logistic function and returns the predicition value, here stratified k-fold validation has been used 
def Logistic_Regression( Labels,Values,colorspace ="RGB"):
  
    


    kf=StratifiedKFold(n_splits=5, random_state=42, shuffle=True)

    kf_info= kf.get_n_splits(Values, Labels) # returns the number of splitting iterations in the cross-validator
    print(kf_info) 
    accuracy =[]
    i=1


    for train_index, test_index in kf.split(Values,Labels):
        lr = LogisticRegression(solver='lbfgs', multi_class='auto', max_iter=10000, random_state=42)
        #print (Values[train_index].shape)
        lr.fit(Values[train_index],Labels[train_index]) ## [:,0] is used to pass only 1D array to eliminate warning message,however, sklearn can self-interpret if 2D given, but shows warning
        Accuracy= accuracy_score(Labels[test_index],lr.predict(Values[test_index]))
        accuracy.append(Accuracy)
        print ("For Split:",i,", Accuracy Score: ", Accuracy)
        print(classification_report(Labels[test_index],lr.predict(Values[test_index]), target_names=["1","2","3"]))
        fig,ax= plot_confusion_matrix(Labels[test_index],lr.predict(Values[test_index]), classes=[1,2,3],title='Confusion matrix, Logistic Regression '+colorspace)
        plt.show()
        filename = "C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/new figures/Logistic Reg Confusion Matrix Mean"+colorspace+", split"+str(i)+".png"
        fig.savefig(filename)
        i+=1
    return accuracy<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>###### this Function uses SVM classifer, stratified k-fold validation is used to test validation accuarcy
def SVM_Classifier(Labels,Values,colorspace ="RGB"):
    


    kf=StratifiedKFold(n_splits=5, random_state=42, shuffle=True)

    kf_info= kf.get_n_splits(Values, Labels) # returns the number of splitting iterations in the cross-validator
    print(kf_info)
    accuracy =[]
    i=1

    for train_index, test_index in kf.split(Values,Labels):
        clf = SVC(kernel='linear',gamma='scale',random_state=42)
    
        clf.fit(Values[train_index],Labels[train_index]) #### [:,0] is used to pass only 1D array to eliminate warning message,however, sklearn can self-interpret if 2D given, but shows warning
        Accuracy= accuracy_score(Labels[test_index],clf.predict(Values[test_index]))
        accuracy.append(Accuracy)
        print ("For Split:",i,", Accuracy Score: ", Accuracy)
        print(classification_report(Labels[test_index],clf.predict(Values[test_index]), target_names=["1","2","3"]))
        fig,ax = plot_confusion_matrix(Labels[test_index],clf.predict(Values[test_index]), classes=[1,2,3],title='Confusion matrix, SVM '+colorspace)
        plt.show()
        filename = "C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/new figures/SVM Confusion Matrix Mean" +colorspace+", split" +str(i)+".png"
        fig.savefig(filename)
        i+=1
    return accuracy<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>###### this Function uses Random Forrest classifer, stratified k-fold validation is used to test validation accuarcy
def Random_Forrest(Labels,Values,colorspace ="RGB"):
  
    



    kf=StratifiedKFold(n_splits=5, random_state=42, shuffle=True)

    kf_info= kf.get_n_splits(Values, Labels) # returns the number of splitting iterations in the cross-validator
    print(kf_info) 
    accuracy = []
    i=1

    for train_index, test_index in kf.split(Values,Labels):
        clf = RandomForestClassifier(n_estimators=1000, max_depth= None,random_state=42)

        clf.fit(Values[train_index],Labels[train_index]) #### [:,0] is used to pass only 1D array to eliminate warning message,however, sklearn can self-interpret if 2D given, but shows warning
        Accuracy= accuracy_score(Labels[test_index],clf.predict(Values[test_index]))
        accuracy.append(Accuracy)
        print ("For Split:",i,", Accuracy Score: ", Accuracy)
        print(classification_report(Labels[test_index],clf.predict(Values[test_index]), target_names=["1","2","3"]))
        fig, ax= plot_confusion_matrix(Labels[test_index],clf.predict(Values[test_index]), classes=[1,2,3],title='Confusion matrix, Random Forrest '+colorspace)
        plt.show()
        filename = "C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/new figures/Random Forest Confusion Matrix Mean " +colorspace+", split"+str(i)+".png"
        fig.savefig(filename)
        i+=1
    return accuracy
def save_csv(accuracy_list,csv_file,remarks):
    avg= np.mean(accuracy_list)
    df_list = accuracy_list.copy()
    df_list.extend([avg,remarks])
    print (df_list)
    df=pd.DataFrame(columns=['split1', 'split2','split3','split4','split5','avg_accuracy','remarks'], data=[df_list])
    print (df)
    with open(csv_file, 'a') as f:
        df.to_csv(f, index= False, header=f.tell()==0)<jupyter_output><empty_output><jupyter_text>Train and Test Each Classifers for Different Input Features  Train and Test Logistic Regression<jupyter_code>print ("For RGB color space")
L,V= load_RGB()
accuracy_score_list_RGB =Logistic_Regression(L, V,colorspace ="RGB")
print ("Mean Accuracy",np.mean(accuracy_score_list_RGB))
save_csv(accuracy_score_list_RGB,'cross_validation_mean_value.csv','RGB,Logistic Regression')

print ("For LAB color space")
L, V = load_LAB()
accuracy_score_list_LAB = Logistic_Regression(L, V,colorspace ="LAB")
print ("Mean Accuracy",np.mean(accuracy_score_list_LAB))
save_csv(accuracy_score_list_LAB,'cross_validation_mean_value.csv','LAB,Logistic Regression')

print ("For HSV color space")
L,V=load_HSV()
accuracy_score_list_HSV =Logistic_Regression(L,V,colorspace ="HSV")
print ("Mean Accuracy",np.mean(accuracy_score_list_HSV))
save_csv(accuracy_score_list_HSV,'cross_validation_mean_value.csv','HSV,Logistic Regression')

ax = sns.boxplot(x=['RGB','HSV','LAB'], y=[accuracy_score_list_RGB,accuracy_score_list_HSV,accuracy_score_list_LAB],palette="Set1", whis= 2)
ax.set(title= 'Logistic Regression' ,ylabel='Accuracy')
plt.show()
filename = "C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/new figures/bax_plot_mean_logistic_regression.png"
fig = ax.get_figure()
fig.savefig(filename)<jupyter_output>For RGB color space
5
For Split: 1 , Accuracy Score:  0.951271186440678
              precision    recall  f1-score   support

           1       0.94      0.99      0.97       144
           2       0.96      0.92      0.94       192
           3       0.95      0.96      0.95       136

   micro avg       0.95      0.95      0.95       472
   macro avg       0.95      0.96      0.95       472
weighted avg       0.95      0.95      0.95       472

Confusion matrix, without normalization<jupyter_text>Train and Test SVM<jupyter_code>print ("For RGB color space")
L,V= load_RGB()
accuracy_score_list_RGB=SVM_Classifier(L, V,colorspace ="RGB")
print ("Mean Accuracy",np.mean(accuracy_score_list_RGB))
save_csv(accuracy_score_list_RGB,'cross_validation_mean_value.csv','RGB,SVM')

print ("SVM")
print ("For LAB color space")
L, V = load_LAB()
accuracy_score_list_LAB=SVM_Classifier(L, V,colorspace ="LAB")
print ("Mean Accuracy",np.mean(accuracy_score_list_LAB))
save_csv(accuracy_score_list_LAB,'cross_validation_mean_value.csv','LAB,SVM')

print ("For HSV color space")
L,V=load_HSV()
accuracy_score_list_HSV=SVM_Classifier(L,V,colorspace ="HSV")
print ("Mean Accuracy",np.mean(accuracy_score_list_HSV))
save_csv(accuracy_score_list_HSV,'cross_validation_mean_value.csv','HSV,SVM')

ax = sns.boxplot(x=['RGB','HSV','LAB'], y=[accuracy_score_list_RGB,accuracy_score_list_HSV,accuracy_score_list_LAB],palette="Set1", whis= 2)
ax.set(title= 'SVM' ,ylabel='Accuracy')
filename = "C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/new figures/bar_plot_mean_SVM.png"
fig = ax.get_figure()
fig.savefig(filename)
plt.show()<jupyter_output>For RGB color space
5
For Split: 1 , Accuracy Score:  0.951271186440678
              precision    recall  f1-score   support

           1       0.94      0.99      0.97       144
           2       0.96      0.92      0.94       192
           3       0.95      0.96      0.95       136

   micro avg       0.95      0.95      0.95       472
   macro avg       0.95      0.96      0.95       472
weighted avg       0.95      0.95      0.95       472

Confusion matrix, without normalization<jupyter_text>Train and Test Random Forrest<jupyter_code>print ("For RGB color space")
L,V= load_RGB()
accuracy_score_list_RGB=Random_Forrest(L, V,colorspace ="RGB")
print ("Mean Accuracy",np.mean(accuracy_score_list_RGB))
save_csv(accuracy_score_list_RGB,'cross_validation_mean_value.csv','RGB,Random Forest')

print ("Random Forrest")
print ("For LAB color space")
L, V = load_LAB()
accuracy_score_list_LAB=Random_Forrest(L, V,colorspace ="LAB")
print ("Mean Accuracy",np.mean(accuracy_score_list_LAB))
save_csv(accuracy_score_list_LAB,'cross_validation_mean_value.csv','LAB,Random Forest')



print ("For HSV color space")
L,V=load_HSV()
accuracy_score_list_HSV=Random_Forrest(L,V,colorspace ="HSV")
print ("Mean Accuracy",np.mean(accuracy_score_list_HSV))
save_csv(accuracy_score_list_HSV,'cross_validation_mean_value.csv','HSV,Random Forest')


ax = sns.boxplot(x=['RGB','HSV','LAB'], y=[accuracy_score_list_RGB,accuracy_score_list_HSV,accuracy_score_list_LAB],palette="Set1", whis= 2)
ax.set(title= 'Random Forrest' ,ylabel='Accuracy')
plt.show()
filename = "C:/Users/<NAME>/Documents/Bidur Git Repo/padColorimetry/new figures/bar_plot_mean_Random_Forest.png"
fig = ax.get_figure()
fig.savefig(filename)<jupyter_output>For RGB color space
5
For Split: 1 , Accuracy Score:  0.9449152542372882
              precision    recall  f1-score   support

           1       0.93      0.97      0.95       144
           2       0.95      0.92      0.93       192
           3       0.96      0.96      0.96       136

   micro avg       0.94      0.94      0.94       472
   macro avg       0.95      0.95      0.95       472
weighted avg       0.95      0.94      0.94       472

Confusion matrix, without normalization
{'fraction_non_alphanumeric': 0.12205779538569098, 'fraction_numerical': 0.03460731764157539, 'mean_word_length': 3.905687339239783, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '184511', 'license': 'MIT', 'n_tokens_mistral': 6076, 'n_tokens_neox': 5541, 'n_words': 1338, 'path': 'notebooks/Test_Classifiers.ipynb', 'repo_name': 'naamiinepal/colorimetric_analysis_final', 'size': '17234'}