<jupyter_start><jupyter_text>encoding of data<jupyter_code>from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
le=LabelEncoder()
ds=data.apply(le.fit_transform)
ds=ds.values
print(ds)
X=ds[:,1:]
Y=ds[:,0]
print(Y)
print(X[:5,:])
# break data into test and train
xtrain,xtest,ytrain,ytest=train_test_split(X,Y,test_size=0.2)
print(xtrain.shape,ytrain.shape)
print(xtest.shape,ytest.shape)<jupyter_output>(6499, 22) (6499,)
(1625, 22) (1625,)<jupyter_text>BUILDING OUR CLASSIFIER<jupyter_code>a=np.array([1,1,0,0,1,0,1,0,1,0])
np.sum(a==1)
def prior_prob(y_train,label):
    total_examples=y_train.shape[0]
    class_examples=np.sum(y_train==label)
    return class_examples/float(total_examples)
prior_prob(a,1)
def conditional_probability(x_train,y_train,feature_col,feature_val,label):
    x_filtered=x_train[y_train==label]
    numerator=np.sum(x_filtered[:,feature_col]==feature_val)
    denominator=np.sum(y_train==label)
    
    return numerator/float(denominator)
def predict(x_train,y_train,xtest):
    ### xtest is a single example ###
    classes=np.unique(y_train)
    n_feature=x_train.shape[1]
    post_prob=[]
    for label in classes:
        likelihood=1.0
        for f in range(n_feature):
            cond=conditional_probability(x_train,y_train,f,xtest[f],label)
            likelihood*=cond
        prior=prior_prob(y_train,label)
        post=likelihood*prior
        post_prob.append(post)
        
    return np.argmax(post_prob)
output=predict(xtrain,ytrain,xtest[4])
print(output)
ypred=[]
for i in range(xtest.shape[0]):
    y=predict(xtrain,ytrain,xtest[i])
    ypred.append(y)
    
yped=np.array(ypred)
print(yped)
print(ytest)
from sklearn.metrics import accuracy_score

acc=accuracy_score(ytest,ypred)
print(acc)<jupyter_output>0.9987692307692307<jupyter_text>Confusion matrix<jupyter_code>from sklearn.metrics import confusion_matrix
cm=confusion_matrix()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13180661577608144, 'fraction_numerical': 0.030534351145038167, 'mean_word_length': 6.175182481751825, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '301796', 'license': 'MIT', 'n_tokens_mistral': 799, 'n_tokens_neox': 765, 'n_words': 98, 'path': 'MachineLearning/Naive Bayes Classifier/NaiveBayes.ipynb', 'repo_name': 'pulkit10251/cbData-Science-Master', 'size': '1965'}