合并
- numpy.concatenate((a1, a2, ...) axis=1按列合并,axis=0按行合并
- numpy.hstack(tup) 按列合并
- numpy.vstack(tup) 按行合并
分割
- numpy.split(ary, indices_or_sections, axis=0) 将数组拆分为多个子数组
代码分析:
import numpy as np
# 创建一个符合正态分布的500个股票504天的涨跌幅数据
stock_day_rise = np.random.normal(0, 1, (500, 504))
a = stock_day_rise[:2, 0:4]
b = stock_day_rise[10:12, 0:4]
# axis=1时候,按照数组的列方向拼接在一起
# axis=0时候,按照数组的行方向拼接在一起
ab = np.concatenate([a, b], axis=0)
print(ab)
"""
[[ 1.31310416 -1.21522993 -0.62558622 1.32071579]
[-0.01132551 1.51174213 -0.64821137 -0.78082249]
[ 1.21860002 0.89126428 -0.60071647 -1.15030537]
[ 1.37237328 -0.38175207 1.03866532 0.85553051]]
"""
# hstack, 按照数组的列方向拼接在一起
c = np.hstack([a, b])
print(c)
"""
[[ 1.31310416 -1.21522993 -0.62558622 1.32071579 1.21860002 0.89126428
-0.60071647 -1.15030537]
[-0.01132551 1.51174213 -0.64821137 -0.78082249 1.37237328 -0.38175207
1.03866532 0.85553051]]
"""
# vstack, 按照数组的行方向拼接在一起
d = np.vstack([a, b])
print(d)
"""
[[ 1.31310416 -1.21522993 -0.62558622 1.32071579]
[-0.01132551 1.51174213 -0.64821137 -0.78082249]
[ 1.21860002 0.89126428 -0.60071647 -1.15030537]
[ 1.37237328 -0.38175207 1.03866532 0.85553051]]
"""
# 分隔成2个数组
e = np.split(ab, 2, axis=0)
print(e)
"""
[array([[ 1.31310416, -1.21522993, -0.62558622, 1.32071579],
[-0.01132551, 1.51174213, -0.64821137, -0.78082249]]), array([[ 1.21860002, 0.89126428, -0.60071647, -1.15030537],
[ 1.37237328, -0.38175207, 1.03866532, 0.85553051]])]
"""
计算实践:实现sigmoid function, 利用np.exp()
import numpy as np
def sigmoid(x):
"""
定义一个sigmoid函数
x is input: An array
This function is to compute the sigmoid function value
"""
s = 1.0 / (1 + (1/np.exp(x)))
return s
m = np.array([1,2,3])
print(sigmoid(m))