Skip to content

Instantly share code, notes, and snippets.

Created July 22, 2021 09:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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"}')
!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), y)
predictions = model.predict(X_test)

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

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


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