The ToList function converts the numpy array into a python list.
In:b
Out:array ([1.+1.J, 3.+2.J])
in:b.tolist () out
: [(1+1j), (3+2J)]
The Astype function can specify the data type when converting an array.
In:b
Out:array ([1.+1.J, 3.+2.J])
in:b.astype (int)
/usr/local/bin/ipython:1: complexwarning:casting Complex discards the imaginary part #虚部丢失, the conversion to B.astype (' complex ') does not occur.
#!/usr/bin/python
Out:array ([1, 3])
3.2 Read and write filesSavetxt
Import NumPy as np
i2 = Np.eye (2)
np.savetxt ("Eye.txt", I2)
# aapl,28-01-2011, 344.17,344.4,333.53,336.1,21144800
c,v=np.loadtxt (' data.csv ', delimiter= ', ', usecols= (6,7 ), Unpack=true) #index从0开始
Averages = Np.zeros (5) for
I in range (5):
indices = np.where (dates = i)
prices = Np.take (Close, indices)
#按数组的元素运算, produces an array as output.
>>> A = [4, 3, 5, 7, 6, 8]
>>> indices = [0, 1, 4]
>>> Np.take (A, indices)
array ([4, 3, 6])
The format string begins with a percent semicolon. Next is an optional flag character:-Indicates the result is left-aligned, and 0 indicates that the left complement 0,+ represents the output symbol (plus + or minus sign-). The third part is an optional output width parameter that represents the minimum number of digits for the output. The forth part is the precision format character, with the "." The beginning, followed by an integer representing the precision. Finally, a type specifies the character, which is specified as a string type in the example.
Numpy.apply_along_axis (func1d, axis, arr, *args, **kwargs)
>>> def My_func (a):
... "" Average and last element of a 1-d array ""
... Return (A[0] + a[-1]) * 0.5
>>> b = Np.array ([[[1,2,3], [4,5,6], [7,8,9]])
>>> Np.apply_along_ax Is (My_func, 0, b) #沿着X轴运动, taking column slice
array ([4., 5., 6.])
>>> Np.apply_along_axis (My_func, 1, b) #沿着y轴运动, fetch row slice
Array ([2, 5, 8.])
>>> B = Np.array ([[[8,1,7], [4,3,9], [5,2,6]])
>>> Np.apply_along_axis (sorted, 1, b)
Array ( [[1, 7, 8],
[3, 4, 9],
[2, 5, 6]]
(1) Use the ones function to create a length of n elements are initialized to 1 of the array, and then the entire array divided by N, you can get the weight. As shown below:
n = Int (sys.argv[1])
weights = Np.ones (n)/n
print "weights", weights
at n = 5 o'clock, the output is as follows:
Weights [0.2 0.2 0.2 0.2 0.2] #权重相等
(2) Using these weights, call the Convolve function:
c = np.loadtxt (' data.csv ', delimiter= ', ', usecols= (6,), unpack=true)
SMA = Np.convolve (weights, c) [n-1:-n+1] # Convolution is an important operation in analytic mathematics, defined as an integral of the product of a function and another function that has been reversed and moved.
t = np.arange (N-1, Len (c)) #作图
plot (T, c[n-1:], lw=1.0)
plot (T, SMA, lw=2.0) show
()
Exponential moving average line (exponential moving average). The weights used by the exponential moving averages are exponential decay. The weights given to historical data points are reduced exponentially, but never reach 0.
x = Np.arange (5)
print "Exp", Np.exp (x)
#output
exp [1.2.71828183 7.3890561 20.08553692 54.59815003]
Linspace returns an array of element values that are evenly distributed within the specified range.
Print "Linspace", Np.linspace ( -1, 0, 5) #起始值, terminating value, optional number of elements
#output
linspace [-1 -0.75-0.5-0.25 0.]
(1) Weight calculation
N = Int (sys.argv[1])
weights = Np.exp (Np.linspace ( -1., 0., N))
(2) Normalization of weight treatment
Weights/= weights.sum ()
print "weights", weights
#output
weights [0.11405072 0.14644403 0.18803785 0.24144538 0.31002201]
(3) Calculation and drawing
c = np.loadtxt (' data.csv ', delimiter= ', ', usecols= (6,), unpack=true)
ema = Np.convolve (weights, c) [n-1:-n+1]
t = np.arange (N-1, Len (c))
plot (T, c[n-1:], lw=1.0)
plot (T, EMA, lw=2.0) show
()
(x, residuals, rank, s) = NP.LINALG.LSTSQ (A, B) #系数向量x, the rank of a residual group, a and the singular value of a of
print X, residuals, rank, s
#计算下一个预测值
print Np.dot (b, X)
>>> x = Np.arange (6)
>>> x = X.reshape ((2, 3))
>>> x
Array ([[0, 1, 2],
[3, 4 , 5]]
>>> np.ones_like (x) #用1填充数组
Array ([[1, 1, 1],
[1, 1, 1]])
A = Np.arange (4)
print a
print "Compressed", A.compress (A > 2) #返回一个根据给定条件筛选后的数组
#output
[0 1 2 3]
compressed [3]
b = Np.arange (1, 9)
print "b =", B
print "factorial", B.prod () #输出数组元素阶乘结果
#output
b = [1 2 3 4 5 6-7 8]
factorial 40320
print "Factorials", B.cumprod ()
#output
factorials [1 2 6 720 5040 40320] #数组元素 Traversal factorial
4.2 Stock Correlation Analysiscovariance = Np.cov (a,b)
Detailed covariance and covariance matrices
Derivation of polynomial function by using Polyder function (in order to minimize the value)
Der = Np.polyder (poly)
print "derivative", der
#output
derivative [0.00334967-0.10571635 0.58068464]
Finding the root of derivative function, that is to find the extremum point of the original polynomial function
Print "Extremas", Np.roots (der)
#output
extremas [24.47820054 7.08205278]
Check consistency
Np.array_equal (A, B)
Np.vectorize substitution Cycle
>>> def myfunc (A, B):
... " Return A-b if a>b, otherwise return a+b "
... If a > B:
... Return a-b ... else: ... Return a + b
>>> vfunc = np.vectorize (myfunc)
>>> Vfunc ([1, 2, 3, 4], 2)
Array ([3, 4, 1, 2])
Two polynomial to do the difference operation
Poly_sub = Np.polysub (A, B)
Select function
Numpy.select (Condlist, ChoiceList, default=0)
>>> x = Np.arange (Ten)
>>> condlist = [X<3, x>5]
#输出两个array [True...false...],[false, .. true]
>>> choicelist = [x, x**2]
>>> np.select (condlist, choicelist)
Array ([0, 1, 2, 0, 0, 0, 36, 49, 64, 81]