Friday, March 24, 2023

python code to email spam filter - Naive Bayes algorithm

 example Python code to implement an email spam filter using the Naive Bayes algorithm:



import os
import numpy as np
import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix

# Set the path of the dataset directory
data_dir = "data/"

# Read the emails from the dataset directory
emails = []
labels = []
for folder in os.listdir(data_dir):
    if folder == "ham":
        label = 0
    elif folder == "spam":
        label = 1
    else:
        continue
    folder_path = os.path.join(data_dir, folder)
    for file in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file)
        with open(file_path, "r", encoding="utf8", errors="ignore") as f:
            email = f.read()
        emails.append(email)
        labels.append(label)

# Preprocess the emails
nltk.download("punkt")
nltk.download("wordnet")
lemmatizer = WordNetLemmatizer()
tokenizer = CountVectorizer().build_tokenizer()
preprocessed_emails = []
for email in emails:
    tokens = tokenizer(email)
    lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens]
    preprocessed_email = " ".join(lemmatized_tokens)
    preprocessed_emails.append(preprocessed_email)

# Split the data into training and testing sets
X = preprocessed_emails
y = np.array(labels)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Vectorize the emails
vectorizer = CountVectorizer()
X_train_vect = vectorizer.fit_transform(X_train)
X_test_vect = vectorizer.transform(X_test)

# Train the Naive Bayes classifier
classifier = MultinomialNB()
classifier.fit(X_train_vect, y_train)

# Evaluate the classifier on the testing set
y_pred = classifier.predict(X_test_vect)
accuracy = accuracy_score(y_test, y_pred)
confusion = confusion_matrix(y_test, y_pred)
print("Accuracy:", accuracy)
print("Confusion matrix:\n", confusion)


This code reads the emails from a directory and preprocesses them using NLTK to tokenize and lemmatize the text. It then splits the data into training and testing sets and vectorizes the emails using the CountVectorizer from scikit-learn. Finally, it trains a Naive Bayes classifier on the training set and evaluates its performance on the testing set using accuracy and confusion matrix.

The requirements.txt file lists the Python packages required to run the email spam filter code. Here is an example requirements.txt file:

makefile
nltk==3.6.3 pandas==1.3.4 scikit-learn==1.0.2

This file specifies the version numbers of the nltk, pandas, and scikit-learn packages that the code requires. You can create this file by running the following command in your command prompt or terminal:

pip freeze > requirements.txt

This command writes all currently installed Python packages and their versions to the requirements.txt file. You can then edit this file to remove any unnecessary packages and specify the exact versions required by your code.

Unsupervised Machine Learning Techniques

 Unsupervised machine learning techniques are a category of machine learning algorithms that do not require labeled data to train the model. Instead, these algorithms use unsupervised learning methods to find patterns, structures, or relationships in the data.

The main objective of unsupervised machine learning is to find hidden structures or patterns in the data that can provide insights into the data distribution or help in data preprocessing. Here are some of the most commonly used unsupervised machine learning techniques:

  1. Clustering: Clustering is a technique that groups similar data points together in clusters based on their similarities or dissimilarities. The goal of clustering is to identify natural groupings in the data that can help in data segmentation, anomaly detection, or pattern recognition.

  2. Dimensionality Reduction: Dimensionality reduction is a technique that reduces the number of features or variables in the data while preserving the most important information. This can help in data compression, feature extraction, and visualization.

  3. Anomaly Detection: Anomaly detection is a technique that identifies rare or unusual data points that do not conform to the expected pattern or behavior. Anomaly detection can be used in fraud detection, intrusion detection, and fault diagnosis.

  4. Association Rule Mining: Association rule mining is a technique that discovers relationships between variables in the data. It involves finding frequent itemsets or sets of items that frequently occur together in the data. Association rule mining can be used in market basket analysis, recommendation systems, and customer behavior analysis.

  5. Principal Component Analysis (PCA): PCA is a dimensionality reduction technique that identifies the most important features or variables in the data. It involves finding the principal components that capture the maximum variance in the data while reducing the dimensionality.

  6. Autoencoders: Autoencoders are neural networks that can learn to encode the data in a low-dimensional representation and then decode it back to its original form. Autoencoders can be used in image and speech processing, data compression, and feature extraction.

Overall, unsupervised machine learning techniques can help in exploratory data analysis, data preprocessing, feature extraction, and anomaly detection. These techniques are widely used in various applications such as customer segmentation, image and speech processing, fraud detection, and recommendation systems

Example Applications of Supervised Machine Learning in Modern Businesses

Supervised machine learning has numerous applications in modern businesses, where it is used to build predictive models that can help organizations make data-driven decisions.

Here are some examples of how supervised machine learning is used in modern businesses:

  1. Customer segmentation: Businesses can use supervised machine learning algorithms to segment their customers based on demographic, behavioral, and transactional data. This can help organizations create targeted marketing campaigns, improve customer retention, and increase sales.

  2. Fraud detection: Supervised machine learning algorithms can be used to identify fraudulent transactions and activities in real-time. This can help financial institutions and e-commerce companies prevent financial losses and protect their customers from fraud.

  3. Credit scoring: Banks and other financial institutions can use supervised machine learning algorithms to build credit scoring models that predict the creditworthiness of borrowers based on their credit history, income, and other factors. This can help them make better lending decisions and reduce the risk of default.

  4. Sentiment analysis: Supervised machine learning algorithms can be used to analyze customer feedback and sentiment on social media platforms and other online forums. This can help businesses understand their customers' needs and preferences, improve customer satisfaction, and optimize their marketing strategies.

  5. Churn prediction: Supervised machine learning algorithms can be used to predict which customers are likely to churn or cancel their subscription. This can help businesses proactively engage with at-risk customers, reduce churn, and increase customer loyalty.

  6. Predictive maintenance: Supervised machine learning algorithms can be used to predict when a machine or equipment is likely to fail. This can help manufacturing companies reduce downtime, optimize maintenance schedules, and improve overall operational efficiency.

  7. Personalized recommendations: E-commerce companies can use supervised machine learning algorithms to make personalized product recommendations to their customers based on their browsing and purchase history. This can help increase sales and improve customer loyalty.

Overall, supervised machine learning can help businesses make data-driven decisions, improve operational efficiency, and increase revenue and customer satisfaction

Time Intelligence Functions in Power BI: A Comprehensive Guide

Time intelligence is one of the most powerful features of Power BI, enabling users to analyze data over time periods and extract meaningful ...