np.array([range(200)], dtype=np.uint8).T,
T是对矩阵转置。
[[ 0 1 2 3 ]]
range(200)生成一个数组,指定范围内的。
[[ 0]
[ 1]
[ 2]
[ 3]]
unpackbits会把数字分解为二进制表示
np.unpackbits(np.array([range(256)], dtype=np.uint8).T,axis=1)
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 1]
[0 0 0 ... 0 1 0]
...
[1 1 1 ... 1 0 1]
[1 1 1 ... 1 1 0]
[1 1 1 ... 1 1 1]]
random生成n维数组,输入是形状。
np.random.random((2,16))
(2*np.random.random((2,16))-1)*0.05生成 -0.05到0.05的随机数组
[[0.2170089 0.73113269 0.59939739 0.15830653 0.32873686 0.4871056
0.92856155 0.08909651 0.13025319 0.50191195 0.24559249 0.05643269
0.73373609 0.65727756 0.68667674 0.87514801]
[0.17710239 0.06304367 0.45636889 0.03528103 0.07759175 0.54845893
0.37659386 0.39009352 0.12031998 0.91609651 0.44276675 0.36816511
0.66146881 0.78629239 0.44745272 0.78260636]]
np.dot() 对于二维数组,它就是矩阵相乘
np.dot(X, synapse_0),
X: [[0,1]]
synapse_0:
[[ 0.00488135 0.02151894 0.01027634 0.00448832 -0.00763452 0.01458941
-0.00624128 0.0391773 0.04636628 -0.01165585 0.0291725 0.00288949
0.00680446 0.04255966 -0.04289639 -0.04128707]
[-0.04797816 0.03326198 0.02781568 0.03700121 0.04786183 0.02991586
-0.00385206 0.02805292 -0.03817256 0.0139921 -0.03566467 0.04446689
0.00218483 -0.00853381 -0.02354444 0.02742337]]
结果为:
[[-0.04797816 0.03326198 0.02781568 0.03700121 0.04786183 0.02991586
-0.00385206 0.02805292 -0.03817256 0.0139921 -0.03566467 0.04446689
0.00218483 -0.00853381 -0.02354444 0.02742337]]
choice,生成随机数组
a是原始数组取值的范围 0-a的数组,p是数组中每个值的概率,size是生成的数组的值个数
5就是[0,1,2,3,4] ,例子中4的取值是0,所以结果中不含有4,只有0-3。3出现的比较多,因为概率高
a2 = np.random.choice(a=5, size=30, p=[0.2, 0.1, 0.3, 0.4, 0.0])
[2 1 2 3 1 3 3 2 3 3 0 2 0 1 0 3 2 2 3 2 3 3 0 1 2 3 0 3 0 1]
zip这个是python自身的方法,从2个输入中各取一行。
a =[[2,3],[4,5]]
b=[[4,5],[6,7]]
for c,d in zip(a,b):
print(c,d)
效果
[2, 3] [4, 5]
[4, 5] [6, 7]
数组比较
martix = np.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
second_column_25 = (martix[:,1]==25)
print(second_column_25)
print(martix[second_column_25,:])
print(martix[[True,False,True],:])
结果
[False True False]
[[20 25 30]]
[[ 5 10 15]
[35 40 45]]
把矩阵中等于10或者5的值,修改为50
vector = np.array([5,10,15,29])
equal_to_10_5= (vector==10) | (vector==5)
vector[equal_to_10_5]=50
print(vector)
结果
[50 50 15 29]
替换矩阵中指定值
second_column_25 = (martix==25)
martix[second_column_25]=4
print(martix)
求和
vector = np.array([5,10,15,29])
print(vector.sum())
对矩阵行或者列求和
martix = np.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
print(martix.sum(axis=1)) #行求和
print(martix.sum(axis=0))