How do I use scikit-survival in the online Jupyter Notebook?

Hello! My question is essentially the title. I’d like to use scikit-survival for Cox analysis, but I’m not sure how to go about importing that package; Googling was fruitless. Any help would be appreciated!

there are some code snippets and examples in the userguide.

1 Like

The snippets in the userguide seem to assume the package scikit-survival has been imported, but I’m not sure how to import it. The version of Jupyter I’m using is purely online, so I can’t run the command-line import statements in the installation guide.

To expand on spookster’s pointing out the excellent getting started guide, I’ll break it down demonstrating how to use it using an online Jupyter Notebook you can run in your browser:

Step-by-step to use online Jupyter with scikit-survival

Go to here and click on the gray and blue launch badge to the right of the ‘JupyterLab’ text.

In the session that comes up select from the main ‘File’ menu ‘File’ > ‘New’ > ‘Notebook’ and when you open the notebook select the Python kernel as the kernel choice.

Then in the first cell of that new notebook running a Python kernel, run the following line in that cell by pasting it in and hitting shift-enter to install the package as advised at the top here:

%conda install -c sebp scikit-survival

When that completes, restart the kernel using the second entry under ‘Kernel’ on the ‘File’ menubar just along the top of the notebook, then in the next cell enter the following code to and hit-shfit-enter run the first example from ’ Introduction to Survival Analysis with scikit-survival’:

from sksurv.datasets import load_veterans_lung_cancer

data_x, data_y = load_veterans_lung_cancer()
data_y

That will run and give result shown there where spookster suggested you start.
Continue on as you wish using scikit-survival.

2 Likes

Thanks for the step-by-step guide! I’m fairly new to Python/Jupyter/many things programming, so I didn’t realize using Anaconda didn’t involve the command line.
When I try to run the cell with the %conda code, however, I get the error below. Could this be an inherent limitation of the environment I’m working in? I’m using the All of Us Researcher Workbench, which 1) provides an online dataset and 2) contains the Jupyter notebook I’m working with.

Collecting package metadata (current_repodata.json): failed

NotWritableError: The current user does not have write permissions to a required path.
  path: /opt/conda/pkgs/cache/79699b5b.json
  uid: 1000
  gid: 100

If you feel that permissions on this path are set incorrectly, you can manually
change them by executing

  $ sudo chown 1000:100 /opt/conda/pkgs/cache/79699b5b.json

In general, it's not advisable to use 'sudo conda'.

Note: you may need to restart the kernel to use updated packages.

You don’t have permissions to use conda in that way it seems. Like you said it seems to be a limitation of the environment. Did you then try the pip route as the fall back? Usually that is less intrusive to the system and so sometimes it works.

Since you are running into restrictions caused by their set up, contact them.

When you posted, your title said “the online Jupyter Notebook”. Ideally you’d say what online instance you were using since it is not the general free one public one. It’s just ‘an online Jupyter Notebook’.

1 Like

For some reason, I’d assumed pip was also limited to the command line–but yes, it worked! Thank you so much. I’ll edit my post for clarity, if anyone else happens to stumble into this issue.
*Edit: Nevermind, seems I can’t edit the post, but I’ll keep your note about the notebook instance in mind for the future

Jupyter allows using a lot of command line abilities right inside the running .ipynb file. I suspect you are using one of the most convenient features of this right now and installing from inside the notebook?

The magic install commands, added in 2019, allow for running inside the notebook to do installations, see here for more about them. They magic variations of the install commands insure the installations occur in the environment where the kernel is running.
Because automagics are usually on by default in most Jupyter offerings, it may have worked because it used the magic variation behind-the-scenes even if you added no symbol. The use of the magic %pip install would be more explicit. Spelled out that is:

%pip install scikit-survival

Run that inside a notebook cell and then restarti the kernel before trying the import statement.

1 Like