Skip to content

Instantly share code, notes, and snippets.

@rantav
Created July 22, 2021 09:31
Show Gist options
  • Save rantav/6a0f3e3096c393387d4db32006cbbc0e to your computer and use it in GitHub Desktop.
Save rantav/6a0f3e3096c393387d4db32006cbbc0e to your computer and use it in GitHub Desktop.
Submit to Kaggle from within a kaggle notebook

Create a ~/.kaggle/kaggle.json file

This is a one-time operation you need to run per kernel

Go and download your kaggle.json file from your kaggle account page and replace YOUR_USERNAME and YOUR_KEY below.

Then run this code in a kaggle python cell

text_file = open("/root/.kaggle/kaggle.json", "w")
n = text_file.write('{"username":"YOUR_USERNAME","key":"YOUR_KEY"}')
text_file.close()
!chmod 600 /root/.kaggle/kaggle.json
# Make sure it worked
!cat /root/.kaggle/kaggle.json

Enable internet access

Go to your kernel settings and enable internet access.
You might need to verify your account before you do it (get an SMS typically)

Create the predictions

Example prediction generation:

from sklearn.ensemble import RandomForestClassifier
import datetime

features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
                             
y = train_set["Survived"]
X = train_set.drop(columns=['Survived'])
X_test = test_set
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
model.fit(X, y)
predictions = model.predict(X_test)

predictions = predictions.astype(int)
output = pd.DataFrame({'PassengerId': p_test.PassengerId, 'Survived': predictions})

file_name = f'submission_{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}.csv'
output.to_csv(file_name, index=False)
print("Your submission was successfully saved: " + file_name)

Submit

And then in another cell (or in the same cell, that's ok) run this:

submission_message = f'Features: {", ".join(features)}'

# Submit
!kaggle competitions submit -c titanic -f {file_name} -m "{submission_message}"

# List submissions
!kaggle competitions submissions  -c titanic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment