Numpy Flashcards
compare two numpy arrays using
And
Or
Not
a = np.array([True,True,False]) b = np.array([True,False,True])
np.logical_and(a,b)
result = [True,False,False]
np. logical_or
np. logical_not
return DataFrame rows tha satisfy two conditons - larger than 10 and smaller than 20.
selection = np.logical_and(df.loc[:,’my_col’] > 10, df.loc[:,’my_col’] < 10)
df[selection]
Find is the probability to get to a step higher than 60, if the rules are:
throwing a dice:
1 + 1 step
2 + 1 step
3, 4, 5 - 1 step
6 + throw again and step up the thrown amount
you have 1% of slipping a step and you must start from 0.
You can’t go bellow step 0
Show the probability distribution and calculate the median and the standart deviation.
Each time you reset the system, the result must be the same.
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
all_walks = [] for i in range(500) : random_walk = [0] for x in range(100) : step = random_walk[-1] dice = np.random.randint(1,7) if dice <= 2: step = max(0, step - 1) elif dice <= 5: step = step + 1 else: step = step + np.random.randint(1,7) if np.random.rand() <= 0.001 : step = 0 random_walk.append(step) all_walks.append(random_walk)
# Create and plot np_aw_t np_aw_t = np.transpose(np.array(all_walks))
# Select last row from np_aw_t: ends ends = np_aw_t[-1,:]
Plot histogram of ends, display plot
plt. hist(ends)
plt. show()
np. average(ends)
np. std(ends)