This session we will move to the python data analysis. First about numpy package. Numpy, means Numerical Python, is the fundamental package for math processing including pandas, whihc is built based upon numpy.
The numpy package, one term you constantly see is numpy ndarray, that’s how numpy construct data, nd, n dimension, multiple dimensional. It’s a basic concept for understanding of ML in the future, the concept of tensor. Showing with examples
listtest = [1, 2, 3, 4], np.array(listtest)
listtest = [[1,2, 3, 4], [5, 6, 7, 8]] arraytest = np.array(listtest) arraytest.shape
That well relates to the concept of tensor, which is visually intuitive for everyone.Note zero rank tensor would be a scalar, no direction angle metric.

Using Numpy, easily build whatever dimensions one needs, np.zeros((3, 4, 2, 5))
About Numpy, we’d learn the concept of indexing and slicing, monumentally important in data parsing:
#indexing and slicing
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2d
arr2d.shape
arr2d[:2, 1:]

#boolean indexing
names = np.array([‘Bob’, ‘Joe’, ‘Will’, ‘Bob’, ‘Will’, ‘Joe’, ‘Joe’])
names == ‘Bob’
# array([ True, False, False, True, False, False, False], dtype=bool)
data[names == ‘Bob’]
data[names == ‘Bob’, 2:]
In Numpy, you can apply lots of basic function across the board., for example, let arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.sqrt(arr2d), np.sqrt(arr2d)np.isfinite(arr2d)np.isnan(arr2d)
Conditional logic application in numpy array, it’s a bit of mind boggling, however, the usage is wide, hence worth to take some time to sort it out
#array operations conditional logic
xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])
result = np.where(cond, xarr, yarr)
Lastly, use the numpy knowledge to create a random walking model hope you will be greatly interested in learning more. Some mathematicians postulate that the stock market as a whole is quite random like a drunken derelict. To prove their point, first they come up with what a random walk pattern look like.
#random genration random walk
import matplotlib.pyplot as plt
import random
position = 0
walk = [position]
steps = 1000
for i in range(steps):
step = 1 if random.randint(0, 1) else -1
position += step
walk.append(position)
#random walk 1000 steps
nsteps = 1000
draws = np.random.randint(0, 2, nsteps)
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum()
plt.plot(walk[:100])
#random walk 1000 steps iterate 5000 times
nwalks = 5000
nsteps = 1000
draws = np.random.randint(0, 2, size=(nwalks, nsteps))
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(1)