Skip to content

Instantly share code, notes, and snippets.

View CatChenal's full-sized avatar

Cat Chenal CatChenal

View GitHub Profile
@CatChenal
CatChenal / future_use.py
Created October 25, 2019 14:14
Gist is neat!
idea = """
GitHub Gist can store code snippets, which can be called using their gist url...
This is going to be very handy for coding presentations especially when used in combination with
Steamlit (https://streamlit.io/).
Imagine: you are presenting at a major conference & to reduce your anxiety, you buy yourself
this free insurance by following these steps:
For each steps in your presentation, create a gist. Then, create another gist, i.e. confX_presentation.py that list each steps.
"""
print(idea)
@CatChenal
CatChenal / SOMPY_colab.md
Last active October 22, 2021 19:51
Installing SOMPY on Colab

Importing SOMPY from github in Colab

Need: SOMPY is not packaged on pipy, and conda is not installed on the colab platform (as of 9/21):

Run this in a colab cell:

!pip install git+https://github.com/sevamoo/SOMPY.git

Then import and test:

@CatChenal
CatChenal / code_in_cell0.md
Last active July 4, 2022 19:53
Code included in the first cell of a Jupyter notebook to allow multi-output

Code that I ususally include in the first cell of a jupyter nootebook:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
  • This avoids using print() to obtain multiple outputs in one cell. For example, given this code in one cell:
a = 1
@CatChenal
CatChenal / pd_pie_plot.md
Last active October 28, 2021 20:08
Pandas pie plot with percentages and values on wedges

Output:

pie

Code:

import pandas as pd
import matplotlib.pyplot as plt

df1 = pd.DataFrame({'sizes':[75, 30, 45, 10]},
 index=['Frogs', 'Hogs', 'Dogs', 'Logs'])
@CatChenal
CatChenal / min_pca.md
Last active December 17, 2021 20:07
Function to get the data reduced with the minimal number of components when using PCA decomposition.
def get_min_pca(scaled_data, min_var_explained=0.95, verbose=False):
  """
  Decompose `scaled_data` into principal components.
  Return the number of components above `min_var_explained` threshold,
  the threshold value and the transformed data.
  Args
  ----
  scaled_data: numpy array: scaled data
 min_var_explained: float, default 0.95: variance explained threshold
@CatChenal
CatChenal / min_svd.md
Last active December 17, 2021 20:12
Function to get the data reduced with the minimal number of components when using SVD (Singular Value Decomposition).

Output (if show == True):

min_svd

Motivation:

Scikit-learn decomposition module includes many decomposition algorithms that can be used for dimensionality reduction. PCA and SVD are two algorithms that differ in their functional implementation in regards to their parameters. PCA uses its n_components parameter in different ways depending on its type:

n_components int, float or ‘mle’, default=None

Notably:

If 0 < n_components < 1 and svd_solver == 'full', select the number of components such that the amount of variance that needs to be explained is greater than the percentage specified by n_components.

@CatChenal
CatChenal / update_git.md
Last active May 2, 2022 15:14
How to update git at the command line

I used to command line version given in this SO post to upgrade my current git version:

  1. Check current version:
(base) C:\Users\me>git --version
git version 2.34.1.windows.1
  1. Update if necessary, which it is for any version below 2.35 as per this offical announcement from 2022-04-12:
(base) C:\Users\me&gt;git update-git-for-windows
@CatChenal
CatChenal / ipywidgets_interact.md
Last active August 8, 2023 15:24
Some flavors of `ipywidgets.interact[]` require a print statement in the dependent function

Example 1 - Simplest implementation: using interact:

=> No need for the function to have a print/display statement => This should also be the behavior of interactive & interactive_output, but it's not

import ipywidgets as ipw


@ipw.interact(x=(20.,250.,5),
          a=(-60.,60.,10),
          b=(-6_000.,6_000,100))
@CatChenal
CatChenal / h_candle_plot.md
Last active July 27, 2022 19:57
Candle plot for two series with categorical y-axis

candle

Data points and sample figure to replicate in E. Downey's plotting

import matplotlib.pyplot as plt

sandwich_list = [
    'Lobster roll',
    'Chicken caesar',
@CatChenal
CatChenal / clear_unused_axes.md
Last active July 27, 2022 18:05
When populating a grid of mpl axes with unknown actual number of datasets, some axes may be empty: clear them AFTER the main loop.

Tip: when populating a grid of mpl axes with unknown actual number of datasets, some axes may be empty: clear them AFTER the main loop.

Code source: DETR_panoptic notebook on colab

  • In the enumerated loop below, we don't know where i will end (because out["pred_masks"][keep] was not saved into a variable, hence we can't access its length).
  • We then use the main loop index into a second one to clear any empty axis.
#[...]