<jupyter_start><jupyter_text>Load the Dataset<jupyter_code>df=pd.read_table("SMSSpamCollection",header=None,encoding='utf-8')
df.head()
df.info()
df.value_counts()
classes=df[0]
classes.value_counts()<jupyter_output><empty_output><jupyter_text>Preprocessing the Data<jupyter_code>from sklearn.preprocessing import LabelEncoder
encoder=LabelEncoder()
y=encoder.fit_transform(classes)


print(classes[:10])
print(y[:10])
text_messages = df[1]
text_messages[:10]
import re 
#replace email
processed=text_messages.str.replace(r'^.+@[^\.].*\.[a-z]{2,}$','emailaddr')
#replace url
processed=processed.str.replace(r'^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$','webaddr')
#replace moneysym
processed=processed.str.replace(r'£|\$','moneysymbol')
#replace phonenumber
processed=processed.str.replace(r'^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$','phonenumber')
#replace normal number
processed=processed.str.replace(r'\d+(\.\d+)?','number')
#remove punctuation
processed=processed.str.replace(r'[^\w\d\s]',' ')

#removce whitespace
processed=processed.str.replace(r'\s+',' ')

#remove leading and trailing whitespace
processed=processed.str.replace(r'^\s+|\s+?$',' ')

#change word to lower case
processed=processed.str.lower()
processed
#remove stopword fromtext message
from nltk.corpus import stopwords

stop_words=set(stopwords.words('english'))

processed=processed.apply(lambda x : ' '.join(term for term  in x.split() if term not in stop_words))
#remove word stems using Porter stemmer 
ps=nltk.PorterStemmer()
processed=processed.apply(lambda x : ' '.join(ps.stem(term) for term in x.split()))
processed[:10]
from nltk.tokenize import word_tokenize

#creating bag of words
all_words=[]

for message in processed:
    words=word_tokenize(message)
    for w in words:
        all_words.append(w)
all_words=nltk.FreqDist(all_words)
print(f'Number of words',len(all_words))
print(f'Most Common Words',all_words.most_common(15))
#use the most common words  as features
word_features=list(all_words.keys())[:1500]
#define a find_features functionj
def find_features(message):
    words=word_tokenize(message)
    features={}
    for word in word_features:
        features[word]=(word in words)
    return features 


features=find_features(processed[0])
for key,value in features.items():
    if value==True:
        print (key)
processed[0]
message=list(zip(processed,y))
seed=1
np.random.seed=seed
np.random.shuffle(message)

featuresets=[(find_features(text),label) for (text,label) in message]
from sklearn import model_selection
training,testing=model_selection.train_test_split(featuresets,test_size=0.25,random_state=seed)
print(f'training data length:',len(training))
print(f'testing data length:',len(testing))<jupyter_output>training data length: 4179
testing data length: 1393<jupyter_text>SKLearn  classifiers  with NLTK<jupyter_code>from sklearn.neighbors import  KNeighborsClassifier
from sklearn.tree         import  DecisionTreeClassifier
from sklearn.ensemble     import  RandomForestClassifier
from sklearn.linear_model import  LogisticRegression,SGDClassifier
from sklearn.naive_bayes  import  MultinomialNB
from sklearn.svm          import  SVC
from sklearn.metrics      import  classification_report,accuracy_score,confusion_matrix
names=['K Nearest Neighbors','Decision Tree','Random Forest','Logistic Regression','SGD Classifier','Naive Bayes','SVM Linear']
classifier=[KNeighborsClassifier(),
            DecisionTreeClassifier(),
            RandomForestClassifier(),
            LogisticRegression(),
            SGDClassifier(max_iter=100),
            MultinomialNB(),
            SVC(kernel='linear')]
models=zip(names,classifier)
print(models)
from nltk.classify.scikitlearn import SklearnClassifier
for name,model in models:
    nltk_model=SklearnClassifier(model)
    nltk_model.train(training)
    accuracy=nltk.classify.accuracy(nltk_model,testing)*100
    print('{}: Accuracy {}'.format(name,accuracy))
#voting classifier
from sklearn.ensemble import VotingClassifier


names=['K Nearest Neighbors','Decision Tree','Random Forest','Logistic Regression','SGD Classifier','Naive Bayes','SVM Linear']
classifier=[KNeighborsClassifier(),DecisionTreeClassifier(),RandomForestClassifier(),LogisticRegression(),SGDClassifier(max_iter=100),MultinomialNB(),SVC(kernel='linear')]
models=zip(names,classifier)


nltk_ensemble=SklearnClassifier(VotingClassifier(estimators=list(models),voting='hard',n_jobs=-1))


nltk_ensemble.train(training)
accuracy=nltk.classify.accuracy(nltk_ensemble, testing)*100
print('Ensemble Method Accuracy : {} '.format(accuracy))
txt_features,labels=zip(*testing)

prediction=nltk_ensemble.classify_many(txt_features)
#print a classfication report and confusion matrix
print(classification_report(labels,prediction))

pd.DataFrame(confusion_matrix(labels,prediction),
            index=[['actual','actual'],['ham','spam']],
            columns=[['predicted','predicted'],['ham','spam']])<jupyter_output>precision    recall  f1-score   support

           0       0.98      1.00      0.99      1210
           1       0.99      0.89      0.94       183

    accuracy                           0.98      1393
   macro avg       0.99      0.94      0.96      1393
weighted avg       0.98      0.98      0.98      1393