To open this notebook on Google Computing platform Colab, click below!¶
Download Necessary Packages¶
import sys
!{sys.executable} -m pip install numpy
!{sys.executable} -m pip install pandas
!{sys.executable} -m pip install scikit-learn
Download data¶
The first step is to download out train test data. We will be training a classifier on the train data and make predictions on test data. We submit our predictions
!wget https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/aicrowd_educational_emspm/data/public/test.csv
!wget https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/aicrowd_educational_emspm/data/public/train.zip
!unzip train.zip
Import packages¶
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import f1_score,precision_score,recall_score,accuracy_score
train_data_path = "train.csv" #path where data is stored
train_data = pd.read_csv(train_data_path,header=None) #load data in dataframe using pandas
Visualize the data¶
train_data.head() # Visualize the data
We can see the dataset contains 57 columns, 0-55 defining the features such as average length of uninterrupted sequences of capital letters, total number of capital letters in the e-mail etc and last column contains 1/0 depending whether the email is spam or not.More information about the feature described by the column can be found here.
Split Data into Train and Validation¶
Now we want to see how well our classifier is performing, but we dont have the test data labels with us to check. What do we do ? So we split our dataset into train and validation. The idea is that we test our classifier on validation set in order to get an idea of how well our classifier works. This way we can also ensure that we dont overfit on the train dataset. There are many ways to do validation like k-fold,leave one out, etc
X_train, X_val= train_test_split(train_data, test_size=0.2, random_state=42)
Here we have selected the size of the testing data to be 20% of the total data. You can change it and see what effect it has on the accuracies. To learn more about the train_test_split function click here.
Now, since we have our data splitted into train and validation sets, we need to get the label separated from the data.
X_train,y_train = X_train.iloc[:,:-1],X_train.iloc[:,-1]
X_val,y_val = X_val.iloc[:,:-1],X_val.iloc[:,-1]
Define the classifier¶
classifier = SVC(gamma='auto')
#from sklearn.linear_model import LogisticRegression
# classifier = LogisticRegression()
We have used Support Vector Machines as a classifier here and set few of the parameteres. But one can set more parameters and increase the performance. To see the list of parameters visit here.
We can also use other classifiers. To read more about sklean classifiers visit here. Try and use other classifiers to see how the performance of your model changes. Try using Logistic Regression and compare how the performance changes.
Tip: A good model doesnt depend solely on the classifier but on the features(columns) you choose. So make sure to play with your data and keep only whats important.
Train the classifier¶
classifier.fit(X_train, y_train)
Predict on Validation¶
Now we predict our trained classifier on the validation set and evaluate our model
y_pred = classifier.predict(X_test)
precision = precision_score(y_test,y_pred,average='micro')
recall = recall_score(y_test,y_pred,average='micro')
accuracy = accuracy_score(y_test,y_pred)
f1 = f1_score(y_test,y_pred,average='macro')
print("Accuracy of the model is :" ,accuracy)
print("Recall of the model is :" ,recall)
print("Precision of the model is :" ,precision)
print("F1 score of the model is :" ,f1)
Prediction on Evaluation Set¶
Load Test Set¶
Load the test data now
final_test_path = "test.csv"
final_test = pd.read_csv(final_test_path,header=None)
Predict Test Set¶
Time for the moment of truth! Predict on test set and time to make the submission.## Predict on evaluation set
submission = classifier.predict(final_test)
Save the prediction to csv¶
submission = pd.DataFrame(submission)
submission.to_csv('/tmp/submission.csv',header=['label'],index=False)
Note: Do take a look at the submission format.The submission file should contain a header.For eg here it is "label".
To download the generated csv in collab run the below command¶
from google.colab import files
files.download('/tmp/submission.csv')