numpy = Numeric Python
import numpy as pn #typical import
.array() #creates an array
.column_stack() #stacks values
.logical_and() #and in numpy for arrays
.logical_not() #notin numpy for arrays
.logical_or() #or in numpy for arrays
.mean() #calculates the mean i.e. arithmetic average
.median() #calculates the median
.nditer #iterates over a numpy array
.random() #creates random values
.random.rand() #creates a random number
.random.randint() #creates a random integer in numpy
.random.seed(n) #sets seed n for random generator
.shape #gives the dimension of an array
.sum() #sorts values
.std() #standard deviation
.sum() #sums values
.transpose() #transposes a numpy array
you cannot calculate with a list, but you can turn a list into an array, and you can caluclate with arrays
the data types have to be all the same
.array() #the methods can hold as list
.arry([ 1 , 2 , 3 ]) #creates the array 1, 2, 3 from the list of 1, 2, 3
.corrcoef() #correlation
.nditer #iterates over a numpy array
.shape #gives the dimension of an array
.std() #standard deviation
[ row , col ] #accesses an array by row and column index
[ : , col ] #accesses all rows for the column
[ row , : ] #accesses all columns fotr the row
for value in np.nditer(my_np_array):
print(value)
np_height_in = numpy.array(np_baseball[:,0]) #fetches the first column over all rows
np.logical_and(myarray > 10, myarray <15) #and between arrays
myarray[np.logical_and(myarray > 10, myarray <15) #and between arrays] #makes a list of True values
print(np.mean(np_height_in))
random_walk = [0]
for event in range(100):
step = random_walk[-1]
dice = np.random.randint(1,7)
if dice <= 2:
step = step - 1
elif dice <= 5:
step = step + 1
else:
step = step + np.random.randint(1,7)
random_walk.append(step)
print(random_walk)
for repeat in range(5):
random_walk = [0]
for event 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)
random_walk.append(step)
all_walks.append(random_walk)
print(all_walks)