Skip to content

Concise notes

Here, we gather concise notes.

Global variables in imported module

If you want to handle a global variable in an imported module, simply add it to the builtin module. Let's assume that VAR is a global variable in the fun1 function within the test module:

import builtins
import test
builtins.VAR = 3
test.fun1()

How run apply on array

import numpy as np
x = np.array([[5,2,1,3], [2,1,5]])
fun = lambda t: np.argmax(t)
np.array([fun(xi) for xi in x])

Crosstab

To show how generate the cross tabulate, let us categorize the columns; consider two continuous variables ( e.g., housing_median_age and total_rooms), categorize them according their .3 and .7 quantiles, and label the elements as L, M, and H. Then find the cross tabulate of them,

Data-frame

Data-frame via pandas is very useful format for working with dataset, its structure is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). The following codes create a data-frame from a dictionary.