<jupyter_start><jupyter_text>Accuracy and Confusion Matrix<jupyter_code>def predictAccuracy(x,y,n,table):
    cnt = 0.0
    
    confusion_matrix  = np.zeros((8,8))
    classes = [1,2,3,4,7,8,9,10]
    
    for i in range(n):
        review = x[i]
        py = predictRating(review,table)
        ay = y[i][0]
        if(py==y[i][0]):
            cnt +=1
        
        confusion_matrix[classes.index(py)][classes.index(ay)] +=1
        
    return cnt/n,confusion_matrix
        

#accu_train,cf_train_mat = predictAccuracy(x_test,ytest,len(y_test),table)
print(accu_train)

def predict(x,table):
    predictions = []
    n = len(xtrain)
    
    for i in range(n):
        prediction = predictRating(x[i],table)
        predictions.append(prediction)
        
    return predictions
        
        
accu_train,cf_train_mat = predictAccuracy(x_test,y_test,len(y_test),table)
print(accu_train)
predictions = predict(x_test,table)
predictions = np.asarray(predictions)
len(predictions)

predictions = predictions.reshape((predictions.shape[0],))
y_test = np.asarray(y_test)
y_test = y_test.reshape((y_test.shape[0]))

#predictions.shape
#y_test.shape
def plotConfusionMatrix(prediction,ytest):
    confusion_matrix = ConfusionMatrix(ytest,prediction)
    print("Confusion matrix:\n%s" % confusion_matrix)
    confusion_matrix.plot(normalized=True)
    plt.show()
    return confusion_matrix
    
cf = plotConfusionMatrix(predictions,y_test)
cf.classification_report<jupyter_output><empty_output><jupyter_text>Confusion Matrix over Test Set Accuracy is equal to Sum of Diagonal Elements/ Total of No of Reviews = 38.74%<jupyter_code>#Print Confusion Matrix for Test Set.

#print(cf_test_mat)

for i in cf_test_mat:
    for j in i:
        print(int(j),end=' ')
    print('\n')<jupyter_output>4291 1603 1370 1039 402 430 343 807 

63 45 46 44 8 8 4 10 

155 171 219 205 74 60 22 42 

257 279 500 664 271 172 91 100 

38 55 130 214 385 305 144 170 

67 54 126 249 554 717 486 568 

13 5 14 23 55 108 94 138 

138 90 136 197 558 1050 1160 3164<jupyter_text>Random Guess and Majority Prediction<jupyter_code>def randomGuess(y,n):
    cnt = 0.0
    
    labels = [1,2,3,4,7,8,9,10]
    
    for i in range(n):
        guess = np.random.randint(0,8)
        if(labels[guess]==y[i][0]):
            cnt +=1
        
    return cnt/n

rand_acc = randomGuess(y_test,len(y_test))
print(rand_acc)<jupyter_output>0.12584<jupyter_text>Random Guess Accuaracy comes out 12.33% ( which is almost same as 1/8 ie picking one class out of 8 classes)<jupyter_code>def majorityGuess(ytest,ytrain,n):
    cnt = 0.0
    
    label_freq = np.zeros(11)
    n = len(ytrain)
    
    for i in range(n):
        yactual = ytrain[i][0]
        label_freq[yactual] +=1
    
    #print(label_freq)
    majority_rating = np.argmax(label_freq)
    
    n = len(ytest)
    
    for i in range(n):
        yactual = ytest[i][0]
        if(yactual==majority_rating):
            cnt +=1 
    
    return cnt/n
    
    
rand_acc = majorityGuess(y_test,ytrain,len(y_test))
print(rand_acc)<jupyter_output>0.20088<jupyter_text>Majority Guess Accuracy is 20.08 %  Naived Bayes on Stemmed Data<jupyter_code>x_stem,y_stem = readData('./imdb/stemmed_imdb_train_text.txt','./imdb/imdb_train_labels.txt')
x_stem_test,y_stem_test = readData('./imdb/stemmed_imdb_test_text.txt','./imdb/imdb_test_labels.txt')
#print(x_stem[1])

VocabStem = getDictionary(x_stem)
VL = len(VocabStem)
print(VL)
saveModel(VocabStem,'vocab_2.pickle')
VocabStem2 = {}

tableStem= getTable(VocabStem,x_stem,y_stem,VL)

saveModel(tableStem,'nbtable_2.pickle')
accu_stem_train,cf_stem_train = predictAccuracy(x_stem,y_stem,len(y_stem_test),tableStem)
print(accu_stem_train)
print(cf_stem_train)
accu_stem_test,cf_stem_test = predictAccuracy(x_stem_test,y_stem_test,len(y_stem_test),tableStem)
print(cf_stem_test)<jupyter_output>[[2617. 1167. 1175. 1294.  206.  244.  198.  398.]
 [ 396.  174.  172.  231.   48.   44.   32.   84.]
 [ 542.  236.  274.  282.   73.   95.   75.  128.]
 [ 793.  348.  409.  474.  174.  208.  155.  278.]
 [ 262.  126.  143.  158.  304.  346.  237.  587.]
 [ 200.  105.  110.  108.  526.  604.  481.  999.]
 [ 104.   40.   55.   43.  290.  329.  238.  566.]
 [ 186.   88.   82.  106.  875. 1139.  847. 1692.]]
{'fraction_non_alphanumeric': 0.12283169245194561, 'fraction_numerical': 0.10079699953117674, 'mean_word_length': 3.2542372881355934, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '152313', 'license': 'MIT', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1697, 'n_words': 341, 'path': 'ml_repo/9. Project - Movie Rating Prediction/.ipynb_checkpoints/naive-bayes-2-checkpoint.ipynb', 'repo_name': 'sachinpr0001/data_science', 'size': '4266'}