import numpy as np
a1=np.array([1,2,1])
a1
import numpy as np
a2=np.arange(6).reshape(2, 3)
a2
# 示例
import numpy as np
a3=np.arange(15).reshape(3, 5)
a3
- 数组的轴(维度)的个数。在Python世界中,维度的数量被称为rank。
a3.ndim # 獲取a3的rank(軸的個數)
- 数组的维度。
- 这是一个整数的元组,值为数组中每一维(轴)的元素个数(长度),表示每个维度中数组的大小。
- 对于有n行和m列的矩阵,shape将是(n,m)。
- shape元组的长度就是rank或维度的个数 ndim。
a3.shape # 一個元素爲整數的元組
- 描述数组中元素类型的对象
- 可以使用标准的Python类型创建或指定dtype
- 另外NumPy提供它自己的类型。例如numpy.int32、numpy.int16和numpy.float64。
a3.dtype # 數組類型
a3.dtype.name # 數組類型的名稱
np.sctypeDict.keys() # 完整的NumPy数据类型列表
可用作记录电子表格或数据库中一行数据的结构
例如
a3_01=np.dtype([('name',str,40),('numitems', int),('price', float)])
a3_01
a3_02=np.array([('a',10,3.12),('b',15,3.2)],dtype=a3_01)
# 使用dtype=a3_01,即自定义数据类型,来创建数组,必须在参数中指定数据类型
a3_02
print(a3_02)
a3_02.shape # 本質上是一個一維數組(2個元素,每個元素是一個列表)
print(a3_02[1])
print(type(a3_02[1]))
print(type(list(a3_02[1])))
- 数组中每个元素的字节大小,即在内存中所占的字节数。
- 例如,元素为 float64 类型的数组的 itemsize 为8(=64/8)
- 而 complex32 类型的数组的 itemsize 为4(=32/8)。
- 它等于 ndarray.dtype.itemsize 。
a3.itemsize # 數組元素的大小
- 数组元素的总个数
- 等于shape的元素的(連)乘积
a3.size # 數組的大小,即元素個數, 等於shape的元組的元素的連乘積
整个数组所占的存储空间
= np.itemsize * np.size
a3.nbytes
type(a3) # 數組的類型
- 該缓冲区包含数组的实际元素。
- 通常,我们不需要使用此属性,因为我们将 使用索引访问数组中的元素 。
- 实际的数据
- 描述这些数据的元数据
大部分的数组操作,仅仅修改元数据,而不改变底层实际数据
def pythonsum(n):
a=list(range(n))
b=list(range(n))
c=[]
for i in range(len(a)):
a[i]=i ** 2
b[i]=i ** 3
c.append(a[i]+b[i])
return c
pythonsum(3)
import numpy as np
def numpysum(n):
a=np.arange(n)**2 # 使用 arange 函数创建包含0~n的整数的 NumPy 数组
b=np.arange(n)**3
c=a+b
return c
numpysum(3)
import numpy as np
a4 = np.array([2,3,4])
a4
a4.dtype # int64
import numpy as np
a5=np.array([1.2, 3.5, 5.1])
a5
a5.dtype # float64
a6 = np.array( [ [1,2], [3,4] ], dtype=str )
a6
a7=np.array([(1.5,2,3),(4,5,6),(7,8,9)])
a7
a7.ndim
a8=np.array([( (1,2), (3,4) )]) # 序列的個數,由序列的層級確定,序列的層級,即軸(rank)的個數
a8
a8.ndim
a81=np.array([np.arange(3), np.arange(3,6), np.arange(6,9)])
# array根據给定的对象生成数组
# 给定的对象应是 类数组 (arange),这个是 array函数 的唯一 必要参数,其余参数均为有默认值的可选参数
a81
通常,数组的元素最初是未知的,但它的大小是已知的
因此,NumPy提供了几个函数来创建具有初始占位符内容的数组
这就减少了数组增长的必要,因为 数组增长的操作花费很大
默认情况下,创建的数组的dtype是 float64
a12=np.arange( 10, 30, 5 ) # 從 10 開始,到 30 結束,每個元素增加 5
a12
a13=np.arange( 0, 2, 0.3 ) # 從 0 開始,到 2 結束,每個元素增加 0.3
a13
a9=np.zeros( (3,4) )
a9
a10=np.ones( (2,3,4), dtype=np.int16 )
a10
a10_01=np.full(10, 5.666)
a10_01
a10_01.fill(999)
a10_01
a11=np.empty( (2,3) )
a11
import numpy as np
a14=np.linspace( 0, 2, 7 ) # 9 numbers from 0 to 2
a14
import numpy as np
from numpy import pi
a15=np.linspace( 0, 2*pi, 10 ) # useful to evaluate function at lots of points
a16=np.sin(a15)
a16
a161=np.random.rand(2,2,5)
a161
a162=np.random.randint(5, size=(2, 2, 5))
a162
a17=np.arange(6) # 1d array
print(a17)
a18=np.arange(12).reshape(4,3) # 2d array
print(a18)
a19=np.arange(24).reshape(2,3,4) # 3d array 每个切片与下一个用空行分开
print(a19)
import numpy as np
print(np.arange(10))
np.set_printoptions(threshold=np.nan)
print(np.arange(10)) # 將10修改爲10000即可顯示
a20=np.array( [20,30,40,50] )
a20
a20<35
10*np.sin(a20)
a21=np.arange( 4 )
a21
a22=a20-a21
a22
a23=a21**2
a23
a24=np.array( [[1,1,1], [0,1,0]] )
a24
a25=np.array( [[2,0,1], [3,4,1]] )
a25
a24*a25 # 相同位置上的元素相乘
a26=np.array( [[2,0,1], [1,1,2]] )
print(a26)
a26.shape
a27=np.array( [[2,0,1,0], [1,1,4,0], [1,1,4,0]] )
print(a27)
a27.shape
a28=a26.dot(a27)
print(a28)
a28.shape
# 2*2+0*1+1*1=5, 2*0+0*1+1*1=1, 2*1+0*4+1*4=6, 2*0+0*0+1*0=0
# 1*2+1*1+2*1=5, 1*0+1*1+2*1=3, 1*1+1*4+2*4=13, 1*0+1*0+2*0=0
np.dot(a26, a27) # 同 a26.dot(a27)
a29=np.ones((2,3), dtype=int)
print(a29)
a29 *=3
a29
a30=np.ones((2,3), dtype=int)
print(a30)
print(a29)
a30 += a29
a30
a31=np.random.random((3,4))
a31
a31.sum()
a31.sum(axis=0)
a31.max()
a31.max(axis=0)
a31.min()
a31.min(axis=1)
a31.cumsum(axis=0) # 计算轴向元素累加和,返回由中间结果组成的数组,返回值是“由中间结果组成的数组”
a31.cumsum(axis=1) # 行累加
import numpy as np
a311=np.array([-0.746, 4.6, 9.4, 7.447, 10.455, 15.555])
print(a311)
a312=np.around(a311)
print(a312)
a313=np.around(a311, decimals=2)
print(a313)
a314=np.around(a311, decimals=-1)
print(a314)
import numpy as np
a315=np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])
print(a315)
a316=np.floor(a315)
print(a316)
import numpy as np
a317=np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])
print(a317)
a318=np.ceil(a317)
print(a318)
np.where(condition, x, y)
满足条件(condition),输出x,不满足输出y只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标
等价于numpy.nonzero
坐标以tuple的形式给出
通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标
返回幾個元素,元組的每個數組就包含幾個元素,各位數組的元素, 組合 成坐標
import numpy as np
a319=np.random.random([2, 3])
print(a319)
print(a319.ndim)
print('-'*100)
a320=np.where(a319>0.5, a319, 0)
print(a320)
print('-'*100)
a321=np.where(a319>0.5) # 返回索引
print(a321)
print(a319[a321])
a32=np.arange(3)
print(a32)
a32 **=a32 # lim(x→0+) x^x = 1,换句话说,0^0如果从正数方面趋近,用极限思维的话是收敛于1的
a32
np.exp(a32) # 返回e的幂次方,e是一个常数为2.71828
np.sqrt(a32) # 平方根
a33=np.array([2., -1., 4.])
print(a33)
print(a32)
np.add(a32, a33)
print(np.add(a32, a33))
np.cbrt(np.add(a32, a33))
a34=np.arange(10)**3
print(a34)
a34[2]
a34[2:5]
a34=np.arange(10)**3
print(a34)
a34[:6:2] = 1000
print(a34)
a34[ : :-1]
print(a34)
for i in a34:
print(np.cbrt(i)) # np.cbrt() 立方根函數
# np.fromfunction() 从函数中创建数组, x,y 是數組的索引,從 0 到x,從 0 到y
def f(x,y):
return 10*x+y
a35=np.fromfunction(f,(5,4),dtype=int)
a35
a35[2,3]
a35.ndim
a35[0:5, 1]
a35[ : ,1]
a35[1:3, : ]
a35[-1] # 当提供比轴数更少的索引时,缺失的索引被认为是一个完整切片,即,行索引取-1(最後一行),列索引缺失,所以取全部
# 三个点( ... )也可表示产生完整索引元组所需的冒号
a35[4,:] # 驗證上一行代碼
for row in a35:
print(row)
a35_01=np.arange(24).reshape(2,3,4)
a35_01
a35_01.shape
a35_01[1,0,::3] # 在切片数组中间隔选定元素
# 第一個維度的第二個元素 12 ... 15 ... 19 ... 23
# 第二個維度的第一個元素 12 ... 15
# 第三個維度的從0開始間隔3的元素 12, 15
a35_01[::-1] # 僅僅對第一個維度逆序
a35_01[:,:,-1] # 區別:取地一個維度全部,第二個維度全部,第三個維度最後一列
for element in a35.flat[0:5]:
print(element)
a36=np.floor(10*np.random.random((3,4)))
a36
a36.shape
a36.ravel() # returns the array, flattened
a36.flatten()
a36.reshape(6,2) # returns the array with a modified shape
a36.reshape(2,-1)
a36.T # returns the array, transposed
print(a36.T.shape)
print(a36.shape)
理解transpose()中的参数的意义
a36
a36.transpose(1,0) # 只是一个视图,不改变原有的 a36
import numpy as np
a36_01 = np.array(range(30)).reshape(2, 3, 5)
print(a36_01.shape)
print ("a36_01 = ")
print (a36_01)
print ("\n=====================\n")
print(a36_01.transpose(1, 0, 2).shape)
print ("a36_01.transpose() = ")
print (a36_01.transpose(1, 0, 2))
a37=np.array([[ 2., 8., 0., 6.],[ 4., 5., 1., 1.],[ 8., 9., 3., 6.]])
print(a37)
print('-'*100)
a37.resize((2,6))
print(a37)
a38=np.arange(4).reshape(2,2)
print(a38)
print('-'*100)
a39=2*a38 # 广播功能
print(a39)
np.hstack((a38,a39))
np.vstack((a38,a39))
print(np.concatenate((a38,a39), axis=1))
print('-'*100)
print(np.concatenate((a38,a39), axis=0))
a38=np.arange(4).reshape(2,2)
print(a38)
print('-'*100)
a39=2*a38 # 广播功能
print(a39)
print('-'*100)
print(np.dstack((a38,a39)))
print('-'*100)
np.dstack((a38,a39)).shape
print(a39)
a39.shape
print(np.dstack((a39)))
np.dstack((a39)).shape
a40=np.arange(2)
print(a40)
print('-'*100)
a41=2*a40
print(a41)
print(np.column_stack((a40, a41))) # 注意,此处是 [0,0]和[1,2]
np.column_stack((a40, a41)).shape
np.column_stack((a38, a39))
np.hstack((a38, a39))
np.column_stack((a38, a39))==np.hstack((a38, a39))
np.column_stack((a38, a39)).shape
import numpy as np
from numpy import newaxis
np.column_stack((a38,a39)) # with 2D arrays
a38
a38.shape
a38[:,newaxis] # this allows to have a 2D columns vector
a38[:,newaxis].shape
np.column_stack((a38[:,newaxis],a39[:,newaxis]))
np.hstack((a38[:,newaxis],a39[:,newaxis]))
np.column_stack((a38[:,newaxis],a39[:,newaxis])).shape
a38[0:]
a39[1]
np.column_stack((a38[0:],a39[1]))
print(np.row_stack((a40, a41)) ) # 注意,此处是 [0,1]和[0,2]
np.row_stack((a40, a41)).shape
np.row_stack((a40, a41))
np.vstack((a40, a41))
np.row_stack((a40, a41))==np.vstack((a40, a41))
import numpy as np
a42=np.arange(24).reshape(4,6)
a42
print(np.hsplit(a42,3))
print('-'*100)
for i in np.hsplit(a42,3):
print(i)
print('+'*100)
print(np.hsplit(a42,(3,5))) # Split a after the third and the fourth column
print('-'*100)
for i in np.hsplit(a42,(3,5)):
print(i)
print('+'*100)
np.vsplit(a42,2)
np.split(a42,2, axis=0)
np.split(a42,2,axis=1)
a43=np.arange(64).reshape(2,4,8)
print(a43)
print('-'*100)
a44=np.dsplit(a43,2)
print(type(a44))
print(a44)
print('-'*100)
for i in a44:
print(i)
print('-'*100)
import numpy as np
a45=np.arange(12)
a45
a46=a45 # no new object is created
a46
a46 is a45 # a and b are two names for the same ndarray object
a46.shape=3,4
a46.shape
a45.shape
a47=a45.view()
a47
a47 is a45
a47.base is a45 # c is a view of the data owned by a
a45.flags
a47.flags
a45
a47[0,0]=100 # a's data changes
a47
a45
a47.shape=2,6 # a's shape doesn't change
a47
a45
a48=a45[:, 1:3].reshape(2,3)
a48
a48[:]=666
a48
a48.shape=3,2
a48
a45
a49=a45[:,2:4]
a49
a49[:]=666
a49
a45
a45
a50=a45.copy() # a new array object with new data is created
a50
a50 is a45
a50.base is a45
a50[:]=0
a50
a45
a45
a45.tolist()
len(a45.tolist())
print(a45.dtype)
a45_01=a45.astype(str)
a45_01
print(a43)
print(a43.shape)
a43_01 = np.rollaxis(a43, 0, 3)
print(a43_01)
print(a43_01.shape)
a51=np.arange(5)
a51
a51 += 4
a51
a52=np.array([[0],[1],[2],[3]])
a52
a52.ndim
a52.shape
a53=np.array([1,2,3])
a53
a53.shape
a54=a52+a53
a54
a54.shape
a55 = np.arange(12)**2 # the first 12 square numbers
a55
a56 = np.array( [ 1,1,3,8,5 ] ) # an array of indices
a56
a55[a56] # the elements of a55 at the positions a56
a57=np.array( [ [ 3, 4], [ 9, 7 ] ] ) # a bidimensional array of indices
a57
a55[a57] # the same shape as a57
a58 = np.array([
[0,0,0], # black
[255,0,0], # red
[0,255,0], # green
[0,0,255], # blue
[255,255,255] # white
])
a58
a59=np.array([[ 0, 1, 2, 0 ],# each value corresponds to a color in the a58
[ 0, 3, 4, 0 ]])
a59
a58[a59]
a58.shape # 第一個維度,就是包含5個元素的維度
a59.shape
a58[a59].shape
a60 = np.arange(12).reshape(3,4)
a60
# indices for the first dim of a
a61 = np.array( [ [0,1], [1,2] ] )
a61
# indices for the second dim
a62 = np.array( [ [2,1], [3,3] ] )
a62
a60[a61,a62] # a61 and a62 must have equal shape
# 坐標先行後列取值
#(0,2)=2,(1,1)=5
#(1,3)=6,(2,3)=11
a60[a61,2] # 此處的2相當與根據a61的廣播
a60[:,a62] # 對a60的第一個維度的三個元素都進行操作
# 相當與
#[0:a62]
#[1:a62]
#[2:a62]
a63 = np.linspace(20, 145, 5) # time scale
a63
a64 = np.sin(np.arange(20)).reshape(5,4)
# 4 time-dependent series
a64
a65=a64.argmax(axis=0) # index of the maxima for each series
a65
a66 = a63[a65] # times corresponding to the maxima
a66
a67=a64[a65, range(a64.shape[1])] # =>data[ind[0],0],data[ind[1],1]...
a67
a64.shape
a64.shape[1]
range(a64.shape[1])
for i in range(0, 4):
print(i)
print(type(range(0, 4)))
a65 = np.arange(12).reshape(3,4)
np.random.shuffle(a65)
a65
a66=(a65>4)&(a65<9) # a66 is a boolean with a's shape
# a66=(a65>4)|(a65<9) | 是 或 的關系
# where 的多條件實現
a66
a65[a66] # 1d array with the selected elements
a65[a66]=666
a65
a67= np.array([2,3,4,5])
a68= np.array([8,5,4])
a69= np.array([5,4,6,8,3])
print(a67)
print(a68)
print(a69)
print('-'*100)
print(a67.shape)
print(a68.shape)
print(a69.shape)
a671,a681,a691 = np.ix_(a67,a68,a69)
print(a671)
print('-'*100)
print(a681)
print('-'*100)
print(a691)
print('+'*100)
print(a671.shape)
print(a681.shape)
print(a691.shape)
a70=a671+a681*a691
print(a70)
print(a70.shape)
a70[3,2,4]
a67[3]+a68[2]*a69[4]
def ufunc_reduce(ufct, *vectors):
vs = np.ix_(*vectors)
r = ufct.identity
for v in vs:
r = ufct(r,v)
return r
ufunc_reduce(np.add,a67,a68,a69)
np.add.identity
import numpy as np
i2=np.eye(2)
i2
np.savetxt("eye.txt",i2)
c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print(c)
print('-'*100)
print(v)
import numpy as np
c,v=np.loadtxt('data.csv',delimiter=',',usecols=(6,7),unpack=True) # c:收盘价, v:成交量
vwap=np.average(c,weights=v)
print(c)
print('-'*100)
print('VWAP=',vwap)
print('mean=', np.mean(c))
t=np.arange(len(c))
print(t)
print(len(c))
print(c)
print('-'*100)
print('TWAP=', np.average(c, weights=t))
h,l=np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True)
print('highest=', np.max(h))
print('lowest=', np.min(l))
hl=np.array([np.max(h),np.min(l)])
print(hl)
print('mean=', np.mean(hl))
print('spread high price', np.ptp(h)) # 数组元素的最大值和最小值之间的差值
print('spread low price', np.ptp(l))
print('median=', np.median(c))
sorted_close = np.msort(c)
print('sorted=', sorted_close)
sorted_close[15]
N=len(c)
print(N)
n=int(N/2)
n1=int((N-1)/2)
print(n)
print(n1)
print('middle=', sorted_close[n])
print('middle=', sorted_close[n1])
n2=np.array([sorted_close[n], sorted_close[n1]])
print('median=', np.mean(n2))
方差,是指各个数据与所有数据算术平均数的离差平方和除以数据个数,所得到的值
对方差开平方根,得到均方差,又叫标准差
c
print('variance=', np.var(c))
np.std(c) # 标准差
np.std(c)*np.std(c) # 验证 标准差的平方 = 方差
print("variance from definition =", np.mean((c - c.mean())**2))
print("variance from definition =", np.mean((c - np.mean(c))**2))
print(c.mean())
print(np.mean(c))
returns=np.diff(c)/c[:-1] # 返回一个由相邻数组元素的差值构成的数组
# 股票收益率=收益额/原始投资额
print(len(np.diff(c)), 'ok', np.diff(c))
print('-'*100)
print(len(c), 'ok', c)
print('-'*100)
print(len(c[:-1]), 'ok', c[:-1])
print('-'*100)
print(returns)
print('-'*100)
print("Standard deviation =", np.std(returns))
logreturns = np.diff( np.log(c) )
# 对数是对求幂的逆运算
# 如果a的x次方等于N(a>0,且a不等于1),那么数x叫做以a为底N的对数(logarithm),记作x=logaN。
# 其中,a叫做对数的底数,N叫做真数。
print(c)
print('-'*100)
print(len(np.log(c)), np.log(c))
print('-'*100)
print(len(logreturns))
logreturns
print(2.71828**5.81740873) # e=2.71828
np.log10(100)
np.log(np.e)
np.log2(8)
posretindices = np.where(returns > 0)
print("Indices with positive returns", posretindices)
- 根据波动率指标公式计算获得波动率 $Volatility\ Index,\ VIX$;
- 如果所选计算周期为日,$年化 VIX = VIX \times \sqrt{250}$
- 如果所选计算周期为周,$年化 VIX = VIX \times \sqrt{52}$
- 如果所选计算周期为月,$年化 VIX = VIX \times \sqrt{12}$
- 如果所选计算周期为季,$年化 VIX = VIX \times 2$
- 如果所选计算周期为年,$年化 VIX = VIX \times 1$
为什么要使用 对数收益率标准差 除以 均值 ?
变异系数($Coefficient\ of\ Variation$)
- 当需要比较两组数据离散程度大小的时候,如果两组数据的测量尺度相差太大,或者数据量纲的不同,直接使用标准差来进行比较不合适
- 此时就应当消除测量尺度和量纲的影响,而变异系数可以做到这一点,它是原始数据标准差与原始数据平均数的比。
annual_volatility = np.std(logreturns)/np.mean(logreturns) # 年波动率等于对数收益率的标准差除以其均值
print(logreturns)
print('-'*100)
print(np.std(logreturns))
print(np.mean(logreturns))
print('-'*100)
print(annual_volatility)
annual_volatility = annual_volatility / np.sqrt(1./252.) # 注意 sqrt 函数中的除法运算。
# 在Python中,整数的除法和浮点数的除法运算机制不同,
# 必须使用浮点数才能得到正确的结果。
print(annual_volatility)
print(logreturns)
print(np.std(logreturns))
print(np.mean(logreturns))
np.sqrt(12)
from datetime import datetime
def datestr2num(s):
return datetime.strptime(s.decode('ascii'), "%d-%m-%Y").date().weekday()
#dates, close=np.loadtxt('data.csv', delimiter=',', usecols=(1,6), unpack=True, converters={1:datestr2num})
dates, close=np.loadtxt('data.csv', delimiter=',', usecols=(1,6), converters={1:datestr2num}, unpack=True)
print("Dates =",dates)
print('-'*100)
print("Close =",close)
averages = np.zeros(5)
averages
for i in range(5):
indices = np.where(dates == i)
prices = np.take(close, indices)
avg = np.mean(prices)
print("Day", i, "prices", prices, "Average", avg)
averages[i] = avg
top = np.max(averages)
print("Highest average", top, '|',"Top day of the week", np.argmax(averages))
bottom = np.min(averages)
print("Lowest average", bottom, '|',"Bottom day of the week", np.argmin(averages))
dates, open, high, low, close=np.loadtxt('data.csv', delimiter=',', usecols=(1, 3, 4,5, 6), converters={1: datestr2num}, unpack=True)
close = close[:16]
dates = dates[:16]
for i in range(len(list(close))):
print(i,' | ',dates[i],' | ',open[i], ' | ', high[i], ' | ', low[i], ' | ', close[i])
first_monday = np.where(dates == 0)
print(type(first_monday), first_monday)
print(type(first_monday[0]),first_monday[0])
print(type(first_monday[0][0]),first_monday[0][0])
first_monday = np.ravel(np.where(dates == 0))[0]
print(type(np.where(dates == 0)), np.where(dates == 0))
print(type(np.ravel(np.where(dates == 0))),np.ravel(np.where(dates == 0)))
print(type(np.ravel(np.where(dates == 0))[0]),np.ravel(np.where(dates == 0))[0])
print(type(first_monday),"The first Monday index is", first_monday)
last_friday = np.ravel(np.where(dates == 4))[-1]
print("The last Friday index is", last_friday)
weeks_indices = np.arange(first_monday, last_friday + 1)
print("Weeks indices initial", weeks_indices)
weeks_indices = np.split(weeks_indices, 3)
print("Weeks indices after split \n", weeks_indices)
print(weeks_indices[0])
print(weeks_indices[1])
print(weeks_indices[2])
print(weeks_indices[-1])
def summarize(a, o, h, l, c):
monday_open = o[a[0]] # 此处[0]对应的weeks_indices中的[0],值为1,a[0]代表第一天的开盘价 即o[1]
week_high = np.max( np.take(h, a) )
week_low = np.min( np.take(l, a) )
friday_close = c[a[-1]] # a[-1]=15, c[15]代表最后一天的收盘价
return("APPL", monday_open, week_high, week_low, friday_close)
weeksummary = np.apply_along_axis(summarize, 1, weeks_indices,open, high, low, close) # 1 表示在行的方向上进行操作
print("Week summary\n", weeksummary)
print('-'*100)
for i in range(len(list(close))):
print(i,' | ', weeks_indices[(i-1)//5][(i-1)%5],' | ', open[i],' | ', high[i],' | ', low[i],' | ', close[i])
# 第一行的weeks_indices[(i-1)//5][(i-1)%5]是15,因为[(i-1)//5]=-1,代表最后一个元素,即索引值为2,[(i-1)%5]为4,代表组以后一个元素,即15
$np.apply\_along\_axis$ 进阶理解
b = np.array([[1,2,3], [4,5,6], [7,8,9]])
b.shape
b
np.apply_along_axis(np.diff,0,b) # 在列方向进行差分的动作
np.apply_along_axis(np.diff,1,b) # 在行方向进行差分的动作
b = np.array([[8,1,7], [4,3,9], [5,2,6]])
b
np.apply_along_axis(sorted, 1, b) # 在列的方向上排序。改变了排列的结构
np.apply_along_axis(sorted, 0, b) # 在列的方向上排序。改变了排列的结构
for i in range(15):
print(i,' | ',i//5,' | ', i%5)