Skip to content

Instantly share code, notes, and snippets.

@aicrowd-bot
Created April 24, 2020 23:02
Show Gist options
  • Save aicrowd-bot/1959b92e6de65401836c8d98d34ddcfb to your computer and use it in GitHub Desktop.
Save aicrowd-bot/1959b92e6de65401836c8d98d34ddcfb to your computer and use it in GitHub Desktop.
Gist created to open in colab
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Baseline for PKHND Educational Challenge on AIcrowd\n",
"#### Author : Ayush Shivani"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## To open this notebook on Google Computing platform Colab, click below!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ayushshivani/aicrowd_educational_baselines/blob/master/PKHND_baseline.ipynb)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download Necessary Packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"!{sys.executable} -m pip install numpy\n",
"!{sys.executable} -m pip install pandas\n",
"!{sys.executable} -m pip install scikit-learn"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download data\n",
"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\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!wget https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/aicrowd_educational_pkhnd/data/public/test.csv\n",
"!wget https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/aicrowd_educational_pkhnd/data/public/train.zip\n",
"!unzip train.zip"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Import packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.svm import SVC\n",
"from sklearn.metrics import f1_score,precision_score,recall_score,accuracy_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Data\n",
"We use pandas library to load our data. Pandas loads them into dataframes which helps us analyze our data easily. Learn more about it [here](https://www.tutorialspoint.com/python_data_science/python_pandas.htm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_data_path = \"train.csv\" #path where data is stored"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_data = pd.read_csv(train_data_path) #load data in dataframe using pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see there are 11 column where first 10 column contains the cards information and the last one describing the hand it makes. 1st and 2nd column contains suit and rank of first card respectively, 3rd and 4th column suit and rank of 2nd card and so on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Split Data into Train and Validation\n",
"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](https://machinelearningmastery.com/overfitting-and-underfitting-with-machine-learning-algorithms/) on the train dataset. There are many ways to do validation like [k-fold](https://machinelearningmastery.com/k-fold-cross-validation/),[leave one out](https://en.wikipedia.org/wiki/Cross-validation_(statistics), etc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_val= train_test_split(train_data, test_size=0.2, random_state=42) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, since we have our data splitted into train and validation sets, we need to get the label separated from the data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_train,y_train = X_train.iloc[:,:-1],X_train.iloc[:,-1]\n",
"X_val,y_val = X_val.iloc[:,:-1],X_val.iloc[:,-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the Classifier\n",
"Now we come to the juicy part. We have fixed our data and now we train a classifier. The classifier will learn the function by looking at the inputs and corresponding outputs. There are a ton of classifiers to choose from some being [Logistic Regression](https://towardsdatascience.com/logistic-regression-detailed-overview-46c4da4303bc), [SVM](https://towardsdatascience.com/support-vector-machine-introduction-to-machine-learning-algorithms-934a444fca47), [Random Forests](https://towardsdatascience.com/support-vector-machine-introduction-to-machine-learning-algorithms-934a444fca47), [Decision Trees](https://towardsdatascience.com/decision-trees-in-machine-learning-641b9c4e8052), etc. \n",
"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. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classifier = SVC(gamma='auto',max_iter=10)\n",
"\n",
"#from sklearn.linear_model import LogisticRegression\n",
"# classifier = LogisticRegression()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have used [Support Vector Machines](https://scikit-learn.org/stable/modules/svm.html#classification) 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](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use other classifiers. To read more about sklean classifiers visit [here](https://scikit-learn.org/stable/supervised_learning.html). Try and use other classifiers to see how the performance of your model changes. Try using [Logistic Regression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) or [MLP](http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html) and compare how the performance changes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Train the classifier"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classifier.fit(X_train, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Predict on Validation\n",
"Now we predict our trained classifier on the validation set and evaluate our model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_pred = classifier.predict(X_val)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Evaluate the Performance\n",
"We use the same metrics as that will be used for the test set. \n",
"[F1 score](https://en.wikipedia.org/wiki/F1_score) and [Log Loss](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html) are the metrics for this challenge"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"precision = precision_score(y_val,y_pred,average='micro')\n",
"recall = recall_score(y_val,y_pred,average='micro')\n",
"accuracy = accuracy_score(y_val,y_pred)\n",
"f1 = f1_score(y_val,y_pred,average='macro')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Accuracy of the model is :\" ,accuracy)\n",
"print(\"Recall of the model is :\" ,recall)\n",
"print(\"Precision of the model is :\" ,precision)\n",
"print(\"F1 score of the model is :\" ,f1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prediction on Evaluation Set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Test Set\n",
"Load the test data now"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"final_test_path = \"test.csv\"\n",
"final_test = pd.read_csv(final_test_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Predict Test Set\n",
"Time for the moment of truth! Predict on test set and time to make the submission."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"submission = classifier.predict(final_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Save the prediction to csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"submission = pd.DataFrame(submission)\n",
"submission.to_csv('/tmp/submission.csv',header=['label'],index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: Do take a look at the submission format.The submission file should contain a header.For eg here it is \"label\". "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## To download the generated csv in collab run the below command"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from google.colab import files\n",
"files.download('/tmp/submission.csv') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Go to [platform](https://www.aicrowd.com/challenges/pkhnd-poker-hand-recognition). Participate in the challenge and submit the submission.csv generated."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment