![]() |
The majority of people in today’s society own a mobile phone, and they all frequently get communications (SMS/email) on their phones. But the key point is that some of the messages you get may be spam, with very few being genuine or important interactions. You may be tricked into providing your personal information, such as your password, account number, or Social Security number, by scammers that send out phony text messages. They may be able to access your bank, email, and other accounts if they obtain this information. To filter out these messages, a spam filtering system is used that marks a message spam on the basis of its contents or sender. In this article, we will be seeing how to develop a spam classification system and also evaluate our model using various metrics. In this article, we will be majorly focusing on OpenAI API. There are 2 ways to We will be using the Email Spam Classification Dataset dataset which has mainly 2 columns and 5572 rows with spam and non-spam messages. Steps to implement Spam Classification using OpenAINow there are two approaches that we will be covering in this article:1. Using Embeddings API developed by OpenAIStep 1: Install all the necessary salaries!pip install -q openai Step 2: Import all the required librariesPython3
Step 3: Assign your API key to the OpenAI environmentPython3
Step 4: Read the CSV file and clean the datasetOur dataset has 3 unnamed columns with NULL values, Note: Open AI’s public API does not process more than 60 requests per minute. so we will drop them and we are taking only 60 records here only. Python3
Output: ![]() Email Spam Classification Dataset Step 5: Define a function to use Open AI’s Embedding APIWe use the Open AI’s Embedding function to generate embedding vectors and use them for classification. Our API uses the “text-embedding-ada-002” model which belongs to the second generation of embedding models developed by OpenAI. The embeddings generated by this model are of length 1536. Python3
Output: ![]() Email Spam Classification Dataset Step 6: Custom Label the classes of the output variable to 1 and 0, where 1 means “spam” and 0 means “not spam”.Python3
Output: ![]() Spam Classification dataFrame after feature engineerin Step 7: Develop a Classification model.We will be splitting the dataset into a training set and validation dataset using train_test_split and training a Random Forest Classification model. Python3
Output: precision recall f1-score support Step 8: Calculate the accuracy of the modelPython3
Output: accuracy: 83.33 % Step 9: Print the confusion matrix for our classification modelPython3
Output: array([[9, 0], 2. Using text completion API developed by OpenAIStep 1: Install the Openai library in the Python environment!pip install -q openai Step 2: Import the following librariesPython3
Step 3: Assign your API key to the Openaithe environmentPython3
Step 4: Define a function using the text completion API of OpenaiPython3
Step 5: Try out the function with some examplesExample 1: Python3
Output: Spam Example 2: Python3
Output: Not spam Frequently Asked Questions (FAQs)1. Which algorithm is best for spam detection?
2. What is embedding or word embedding?
3. Is spam classification supervised or unsupervised?
4. What is spam vs ham classification?
ConclusionIn this article, we discussed the development of a spam classifier using OpenAI modules. Open AI has many such modules that can help you ease your daily work and also help you get started with projects in the field of Artificial Intelligence. You can check out other tutorials using Open AI API’s below:
|
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |