External Cheatsheets:

Numpy

  • Numpy for matlab users

  • Numpy Array to list

    np.array.tolist()
  • Initializing a matrix

    # initialzing a matrix
    a = np.zeros(shape=(3, 2))
    a = np.empty(shape=(3, 2)) #initializes with random values
    a = np.full(shape=(3,2), np.nan)
  • Boolean array or matrix

      a = np.full((m,n), True)
      b = np.full((m,n), False)
  • np.newaxis1

  • string array with numpy2

    my_array = numpy.empty([1, 2], dtype="S10")
    # S10 -> 10 characters
    # Add S_num to preallocate number of strings

Pandas

  • Data import

     

data = pd.read_csv(os.path.join(root_folder, filename), sep=‘\t’, skiprows=1, header=None, names=[‘col1’,‘col2’])

to reset indexes (and drop it)

data.reset_index(drop=True, inplace=True)
```

  • Iteration3

    # iterating through rows (NOT RECOMMENDED)
    for i, row in df.iterrows():
    	print(row)
    # iterating through index (probably not recommended)
    for i in df.index:
    	print(i)
  • Nested dictionary to dataframe (from_dict)

    d = dict{1:{'a':0, 'b':1}, 2:{'a': 1, 'b':10}}
    df = pd.DataFrame.from_dict(d ,orient='index')
  • Adding (extra) named indexes (rows) and sorting column based on named indexes

    df.loc["mean"] = df.mean()
    df = df.sort_values("mean", axis=1, ascending=False)

Scipy

Interpolation

Integration

JAX

PyMC

Intro to PyMC

Setup

conda install numpy pandas scipy scikit-learn matplotlib seaborn dill
# optional
conda install ipykernel ipywidgets # for vscode + jupyter

Footnotes

  1. https://stackoverflow.com/questions/29241056/how-do-i-use-np-newaxis

  2. https://stackoverflow.com/questions/13717554/weird-behaviour-initializing-a-numpy-array-of-string-data

  3. https://stackoverflow.com/questions/16476924/how-can-i-iterate-over-rows-in-a-pandas-dataframe