External Cheatsheets:

Numpy

  • Numpy for matlab users

  • Numpy matrixes are displayed as a list of row vectors.

    • An identity matrix is therefore shown as [[1, 0], [0, 1]] The first axis is the row number, then the column number.
    • axis=0; perform operations along the column, provides a row vector
    • axis=1; perform operations along the row, provides a column vector (though it is still just a list)
  • 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)
  • string array with numpy1

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

  • Numpy Array to list

    np.array.tolist()
  • max and max index

    max_val = np.max(matrix)
    max_ind = np.argmax(matrix)
     

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/13717554/weird-behaviour-initializing-a-numpy-array-of-string-data ↩

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

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