Fintech金融工程课程笔记(二:NumPy的强大优势)

注:这是我参加招行Fintech精英训练营金融工程课程跟着做的笔记,代码是在Pycharm上写的。

里面用到的股票数据均来自雅虎财经(https://finance.yahoo.com/),数据下载方法我在(一)中有介绍。


量化交易基础:使用python处理金融数据

01-03:NumPy的强大优势


import  numpyas np

#6/7/8.creating NumPy arrays

def test_run():

print(np.array([(2,3,4),(5,6,7)]))

#empty array

    print(np.empty((5.4)))

print(np.ones((5,4)),dtype=np.int_)

if __name__=="__main__":

test_run()

#9. generating random numbers

def test_run():

#产生[0,1)之间的随机数

    print(np.random.random((5,4)))

print(np.random.rand(5, 4))#function argumemts(not a tuple)

#sample numbers from a Gaussion (normal) distribution

    print(np.random.normal(50,10,size=(2,3)))#mean=50,std=10,row=2,column=3

#random integers

    print(np.random.randint(10))#a single integer in [0,10)

    print(np.random.randint(0,10))# a single integer in [0,10),same as above, specifying [low,high) explicit

    print(np.random.randint(0,10,size=5))# 5 random integers as a 1D array

    print(np.random.randint(0, 10, size=(2,3)))# 2x3 array of random integers

if __name__=="__main__"

    test_run()

#10. arrasy attributes

def test_run():

a=np.random.random((5,4))

print(a.shape)

print(a.shape[0])#number of rows

    print(a.shape(1))#number of columns

    print(len(a.shape))

print(a.size)#array 中值的个数

    print(a.dtype)#data type of the values present in array

# 11.ndarray 操作

def test_run():

np.random.seed(693)#seed the random number generator,为了每次得到相同的序列数字

    a = np.random.randint(0,10,size=(5,4))

print("Array:\n",a)

#sum of all elements

    print("Sum of all elements:",a.sum())

#iterate over rows, to compute sum of each column

    print("Sum of each column:\n",a.sum(axis=0))

# iterate over columns, to compute sum of each row

    print("Sum of each column:\n", a.sum(axis=1))

#statistics:min,max,mean(across rows,cols, and overall)

    print("Minimum of each column:\n",a.min(axis=0))

print("Maximum of each row:\n", a.max(axis=1))

print("Mean of all elements:\n", a.mean())#leave out axis arg

#12. exercise: locate maximum value

def get_max_index(a):

"""Return the index of the maximum value in given 1D array."""

    return np.argmax(a)

# 或 return a.argmax()

def test_run():

a = np.array([9, 6, 2, 3, 12, 14, 7, 10], dtype=np.int32)# 32-bit integer array

    print

    "Array:", a

# Find the maximum and its index in array

    print

    "Maximum value:", a.max()

print

    "Index of max.:", get_max_index(a)

if __name__ =="__main__":

test_run()

13. 记录python操作花费的时间

"""using time function """

import time

def test_run():

t1 = time.time()

print("ML4T")

t2 = time.time()

print("The time taken by print statement is", t2-t1, "seconds")

if __name__=="__main__":

test_run()

14. NumPy的运行速度有多快?

def manual_mean(arr):

sum=0

    for iin xrange(0,arr.shape[0]):

for jin arange(0,arr.shape[1]):

sum=sum+arr[i,j]

return sum/arr.size

def numpy_mean(arr):

return arr.mean()

def how_long(func,*args):

t0=time()

result=func(*args)

ti =time()

return result,t1-t0

def test_run():

nd1=np.random.random((1000,10000))

res_manual,t_manual=how_long(manual_mean,nd1)

res_numpy, t_numpy = how_long(numpy_mean, nd1)

#15. accessing array elements

#slicing

def test_run():

a = np.random.rand(5,4)

print(a[:,0:3:2])#1star,3-1end,2step

#16. modifying array elements

#assigning a list to a column in and array

a[:,3]=[1,2,3,4,5]

#17. indexing an array with another array

def test_run():

a=np.random.rand(5)

indices=np.array([1,1,2,3])

print(a[indices])

if __name__=="__main__":

test_run()

#18. Bboolean or _mask_index arrays

mean=a.mean()

#masking

a[a

print(a)

#18. arithmetic operation:all element wise(+,-,*,/)

你可能感兴趣的:(Fintech金融工程课程笔记(二:NumPy的强大优势))