<jupyter_start><jupyter_text>KNN From Scratch<jupyter_code>#import libraries
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import copy
import requests
import zipfile
from io import BytesIO

# import NB from bayes.py
from bayes import NB<jupyter_output>[nltk_data] Downloading package stopwords to
[nltk_data]     C:\Users\Asus\AppData\Roaming\nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package punkt to
[nltk_data]     C:\Users\Asus\AppData\Roaming\nltk_data...
[nltk_data]   Package punkt is already up-to-date!<jupyter_text>Import dataset<jupyter_code>#download data
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00228/smsspamcollection.zip'

request = requests.get(url)
file = zipfile.ZipFile(BytesIO(request.content))
file.extractall('./data/')

path='./data/SMSSpamCollection'
df = pd.read_csv(path,delimiter='\t',encoding = "ISO-8859-1", usecols=[0,1], names=["label", "message"], nrows=5000)

#inspect the head
print(df.head())<jupyter_output>label                                            message
0   ham  Go until jurong point, crazy.. Available only ...
1   ham                      Ok lar... Joking wif u oni...
2  spam  Free entry in 2 a wkly comp to win FA Cup fina...
3   ham  U dun say so early hor... U c already then say...
4   ham  Nah I don't think he goes to usf, he lives aro...<jupyter_text>Transform data label<jupyter_code>#transform the target into numeric codes
labelencoder= LabelEncoder()
data = copy.deepcopy(df)
data.label = labelencoder.fit_transform(data.label)

#original labels
orig_label = labelencoder.inverse_transform(data.label)

print(data.head())<jupyter_output>label                                            message
0      0  Go until jurong point, crazy.. Available only ...
1      0                      Ok lar... Joking wif u oni...
2      1  Free entry in 2 a wkly comp to win FA Cup fina...
3      0  U dun say so early hor... U c already then say...
4      0  Nah I don't think he goes to usf, he lives aro...<jupyter_text>Load your data into X and y<jupyter_code>X,y = data.message.values, data.label.values<jupyter_output><empty_output><jupyter_text>Define train test split<jupyter_code>train_X, test_X, y_train, y_test = train_test_split(X,y,test_size=.2,stratify=y,random_state=0)<jupyter_output><empty_output><jupyter_text>Fit the model<jupyter_code>model = NB().fit(train_X,y_train)<jupyter_output><empty_output><jupyter_text>Plot words<jupyter_code>#To view picture of spam, id='s', ham: id='h', specific text: id='a' (any string except h and s)
model.text_by_image(id='s',count=200,text=None)<jupyter_output><empty_output><jupyter_text>Compute Accuracy<jupyter_code>Train_pred_y = model.predict(train_X)
Test_pred_y = model.predict(test_X)

print('Train Accuracy {:0.2f}%'.format( model.evaluate(y_train,Train_pred_y)*100 ))
print('Test Accuracy {:0.2f}%'.format( model.evaluate(y_test,Test_pred_y)*100 ))<jupyter_output>Train Accuracy 94.53%
Test Accuracy 90.80%<jupyter_text>Single value prediction with best model<jupyter_code># features = ['Click on this link to win 1000 dollars']
features = ['Mum was sick but will soon recover, pray for her because she needs some money in dollars']

y_pred = model.predict(features)
y_pred = labelencoder.inverse_transform(y_pred)
print(y_pred[0])
model.text_by_image(id='a',count=200,text=features[0])<jupyter_output>ham
{'fraction_non_alphanumeric': 0.11486291486291486, 'fraction_numerical': 0.01875901875901876, 'mean_word_length': 4.407176287051482, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '387583', 'license': 'MIT', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1193, 'n_words': 306, 'path': 'bayes Modelling.ipynb', 'repo_name': 'TonyMTH/Bayers-From-Scratch', 'size': '3465'}