Loading

WINEQ

WineQ Challenge Notebook

You bet this notebook is very descriptive 🏃🏽‍♂️

vyom

image.png

So, what's Colab?

951a433c1470350b17898ed7154dcbf2.gif

In order to simplify things here, Google Colaboratory is a platform used by professionals in the field of AI as well Data Enthusiasts.

Fine Vyom, but what is that weird Connect button upthere?

What's a Cell?

In [ ]:
print( "This is a cell!" )
In [ ]:
print( "Hey vyom, tone down that enthusiasm!!!".upper() )

Wait, you can compile code while typing?!

In [ ]:
print("Pretty much.")

a, b = 99, 9

if b == 9 and (a + b == 108):
  print("Right?!")

Download Necessary Packages 📚

In [ ]:
#!pip install --upgrade fastai 
!pip install -qq aicrowd-cli==0.1

Download Data

The first step is to download out train test data. We will be training a model on the train data and make predictions on test data. We submit our predictions.

In [ ]:
API_KEY = '0f18b0d7960195ef877b35485c11a536' #Please enter your API Key from [https://www.aicrowd.com/participants/me]
!aicrowd login --api-key $API_KEY
In [ ]:
!aicrowd dataset download --challenge wineq
In [7]:
!rm -rf data
!mkdir data

!mv train.csv data/train.csv
!mv test.csv data/test.csv
!mv sample_submission.csv data/sample_submission.csv

SVMs! 🥊

image.png

The main objective of a support vector machine is to segregate the given data in the best possible way. When the segregation is done, the distance between the nearest points is known as the margin. The approach is to select a hyperplane with the maximum possible margin between the support vectors in the given data-sets.

(https://medium.com/edureka/support-vector-machine-in-python-539dca55c26a)

Import packages

In [8]:
# Libraries for looking at the Data
import pandas as pd
import numpy as np

# For Splitting the Data
from sklearn.model_selection import train_test_split

from sklearn.neural_network import MLPClassifier

# Importing the SVM!
from sklearn.svm import SVC

# Importing StandardScaler
from sklearn.preprocessing import StandardScaler

# Importing the metrics
from sklearn.metrics import accuracy_score, f1_score

Load Data

  • We use pandas 🐼 library to load our data.
  • Pandas loads the data into dataframes and facilitates us to analyse the data.
  • Learn more about it here 🤓
In [9]:
# Path where data is stored
all_data_path = "data/train.csv"
In [10]:
# Load data in dataframe using pandas
all_data = pd.read_csv(all_data_path)

Visualize the data 👀

In [ ]:
all_data

We can see the dataset contains 12 columns,where columns 0-10 denotes different attributes of the wine the last column tells the quality of the wine from 1-10.

Split Data into Train and Validation 🔪

  • The next step is to think of a way to test how well our model is performing. we cannot use the test data given as it does not contain the data labels for us to verify.
  • The workaround this is to split the given training data into training and validation. Typically validation sets give us an idea of how our model will perform on unforeseen data. it is like holding back a chunk of data while training our model and then using it to for the purpose of testing. it is a standard way to fine-tune hyperparameters in a model.
  • There are multiple ways to split a dataset into validation and training sets. following are two popular ways to go about it, k-fold, leave one out. 🧐
  • Validation sets are also used to avoid your model from overfitting on the train dataset.
In [12]:
X_train, X_val= train_test_split(all_data, test_size=0.2, random_state=42) 

# You can play around with: test_size :)

# Always remember that the test_size can only be between 0 and 0.99! 🙋🏽‍♂️
  • We have decided to split the data with 20 % as validation and 80 % as training.
  • To learn more about the train_test_split function click here. 🧐
  • This is of course the simplest way to validate your model by simply taking a random chunk of the train set and setting it aside solely for the purpose of testing our train model on unseen data. as mentioned in the previous block, you can experiment 🔬 with and choose more sophisticated techniques and make your model better.
  • Now, since we have our data splitted into train and validation sets, we need to get the corresponding labels separated from the data.
  • with this step we are all set move to the next step with a prepared dataset.
In [13]:
# Time to make seperate Training Dataset!

X_train,y_train = X_train.iloc[:,:11], X_train.iloc[:,11]



# Time to make seperate Validation Dataset!

X_val,y_val = X_val.iloc[:,:11],X_val.iloc[:,11]
In [14]:
#Applying Standard scaling to get optimized result

sc = StandardScaler()

X_train = pd.DataFrame(sc.fit_transform(X_train))
X_val = pd.DataFrame(sc.fit_transform(X_val))

image.png

TRAINING PHASE 🏋️

Define the Model

  • We have fixed our data and now we are ready to train our model.

  • There are a ton of classifiers to choose from some being Logistic Regression, SVM, Random Forests, Decision Trees, etc.🧐

  • Remember that there are no hard-laid rules here. you can mix and match classifiers, it is advisable to read up on the numerous techniques and choose the best fit for your solution , experimentation is the key.

  • A good model does not depend solely on the classifier but also on the features you choose. So make sure to analyse and understand your data well and move forward with a clear view of the problem at hand. you can gain important insight from here.🧐

In [15]:
classifier = SVC(gamma='auto', max_iter=1000, kernel = 'poly', degree=5)


# Incase you want to go a step ahead, uncomment these two lines:
#from sklearn.linear_model import LogisticRegression
#2classifier = LogisticRegression()

What can I do to improve and experiment? 🤔

  • You can change gamma to: 0.1, 1, 10, 100

  • You can play around with max_iter: 1000 to infinity?

  • You can change kernel to: ‘linear’, ‘rbf’, ‘poly’

  • You can change degree to: 0, 1, 2, 3, 4, 5, 6

To read more about other sklearn classifiers visit here 🧐. Try and use other classifiers to see how the performance of your model changes. Try using Logistic Regression or MLP and compare how the performance changes.

  • To start you off, We have used a basic Support Vector Machines classifier here.
  • But you can tune parameters and increase the performance. To see the list of parameters visit here.
  • Do keep in mind there exist sophisticated techniques for everything, the key as quoted earlier is to search them and experiment to fit your implementation.

Train the classifier

In [ ]:
classifier.fit(X_train, y_train)

Got a warning! Dont worry, its just beacuse the number of iteration is very less(defined in the classifier in the above cell).Increase the number of iterations and see if the warning vanishes and also see how the performance changes.Do remember increasing iterations also increases the running time.( Hint: max_iter=500)

Validation Phase 🤔

Wonder how well your model learned! Lets check it.

Predict on Validation

Now we predict using our trained model on the validation set we created and evaluate our model on unforeseen data.

In [17]:
y_pred = classifier.predict(X_val)

Evaluate the Performance

  • We have used basic metrics to quantify the performance of our model.
  • This is a crucial step, you should reason out the metrics and take hints to improve aspects of your model.
  • Do read up on the meaning and use of different metrics. there exist more metrics and measures, you should learn to use them correctly with respect to the solution,dataset and other factors.
  • F1 score is the metric for this challenge
In [18]:
accuracy = accuracy_score(y_val,y_pred)
f1 = f1_score(y_val,y_pred,average='macro')

print("F1 score of the model is :" ,f1)
print("Accuracy of the model is :" ,accuracy)
F1 score of the model is : 0.23066370462189525
Accuracy of the model is : 0.4604591836734694

Testing Phase 😅

We are almost done. We trained and validated on the training data. Now its the time to predict on test set and make a submission.

Load Test Set

Load the test data now

In [19]:
final_test_path = "data/test.csv"
final_test = pd.read_csv(final_test_path)

Predict Test Set

Time for the moment of truth! Predict on test set and time to make the submission.

In [20]:
submission = classifier.predict(final_test)

Save the prediction to csv

🚧 Note :

  • Do take a look at the submission format.
  • The submission file should contain a header.
  • Follow all submission guidelines strictly to avoid inconvenience.
In [21]:
submission = pd.DataFrame(submission) 
submission.to_csv('submission.csv',header=['quality'],index=False)

Making Direct Submission thought Aicrowd CLI

In [ ]:
!aicrowd submission create -c wineq -f submission.csv

Tip for Starting into Python 🐍

  • Code everyday.
  • Do dumb sh*t with Python.
  • StackOverflow is the best Python tutorial.
  • It's never too late.

Comments

AdnanZaidi
Over 3 years ago

Nice guide; for beginners to submit their entires.

You must login before you can post a comment.

Execute