Josherich's Blog

HOME SHORTS PODCAST SOFTWARES DRAWING ABOUT RSS

Basic

Matrix

dot product

inner product

transpose

Identity Matrix

np api

np.repeat(n, 3)
np.arange(n)
np.full((3, 3), 1, dtype=bool)
out = np.where(arr % 2 == 1, -1, arr)
np.reshape()
np.repeat(a, 3)
np.tile(a, 3)
np.concatenate
np.vstack
np.hstack
np.r_[a, b]
np.intersect1d(a, b)

- get index meeting condition

Method 1 index = np.where((a >= 5) & (a <= 10)) a[index]

Method 2: index = np.where(np.logical_and(a>=5, a<=10)) a[index] ```

i, j contain the row numbers and column numbers of 600 elements of iris_x np.random.seed(100) iris_2d[np.random.choice((i), 20), np.random.choice((j), 20)] = np.nan

Method 2 np.random.seed(100) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan ```

Solution 2 from scipy.stats.stats import pearsonr corr, p_value = pearsonr(iris[:, 0], iris[:, 2]) print(corr) ```

Map it to respective category label_map = {1: ‘small’, 2: ‘medium’, 3: ‘large’, 4: np.nan} petal_length_cat = [label_map[x] for x in petal_length_bin] ```

Introduce new dimension to match iris_2d’s volume = volume[:, np.newaxis]

Add the new column

out = np.hstack([iris_2d, volume])


- probabilistic sampling

Get the species column species = iris[:, 4]

Approach 1: Generate Probablistically np.random.seed(100) a = np.array([‘Iris-setosa’, ‘Iris-versicolor’, ‘Iris-virginica’]) species_out = np.random.choice(a, 150, p=[0.5, 0.25, 0.25])

Approach 2: Probablistic Sampling (preferred)

np.random.seed(100) probs = np.r_[np.linspace(0, 0.500, num=50), np.linspace(0.501, .750, num=50), np.linspace(.751, 1.0, num=50)] index = np.searchsorted(probs, np.random.random(150)) species_out = species[index] print(np.unique(species_out, return_counts=True))


- get value grouped by another column

Get the species and petal length columns petal_len_setosa = iris[iris[:, 4] == b’Iris-setosa’, [2]].astype(‘float’)

Get the second last value np.unique(np.sort(petal_len_setosa))[-2] ```

Solution 2: Using np.where print(np.where(a < 10, 10, np.where(a > 30, 30, a))) ```

Method 2: (arr[:, None] == np.unique(arr)).view(np.int8) ```

Solution: For Loop version output = [] uniqs = np.unique(species_small)

for val in uniqs: # uniq values in group for s in species_small[species_small==val]: # each element in group groupid = np.argwhere(uniqs == s).tolist()[0][0] # groupid output.append(groupid)

print(output) ```