import numpy as np
lst = [[1,3,5],[2,4,6]]
print(type(lst))
np_lst = np.array(lst)
print(type(np_lst))
np_lst = np.array(lst,dtype = np.float32)
print(np_lst.shape)
print(np_lst.ndim)#维度
print(np_lst.dtype)
print(np_lst.itemsize)#每个元素的字节大小,64就是8,32就是4
print(np_lst.size)
输出结果:
<class 'list'>
<class 'numpy.ndarray'>
(2, 3)
2
float32
4
6
数组
print ('数组:')
print(np.zeros([2,4]))
print(np.ones([6,4]))
rand
print ('rand:')
print(np.random.rand(2,4))
randint
print ('randint:')
print(np.random.randint(1,10,3))#生成3个1——10的随机整数
randn 标准正态随机数
print ('randn:')
print(np.random.randn(2,4))
choice
print('choice:')
print(np.random.choice([10,20,30,50,46,85],3))#在这中间随机选择3个数字
distribute
print('disritube:')
print(np.random.beta(1,10,100))#生成贝塔分布
输出结果:
数组:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
rand:
[[ 0.51134183 0.68640869 0.30745971 0.42751586]
[ 0.39394628 0.51500959 0.93783152 0.33093498]]
randint:
[7 7 6]
randn:
[[-0.0629641 1.81308963 0.17863036 0.2995394 ]
[ 0.38179353 -1.09422534 -0.04260484 0.12850522]]
choice:
[50 30 46]
disritube:
[ 0.30105046 0.22466447 0.00312794 0.04867815 0.23824019 0.22276963
0.06994241 0.08153077 0.04210733 0.15298111 0.13114783 0.19231987
0.08590524 0.02384821 0.05994102 0.01940616 0.01914576 0.08006452
0.09477109 0.03481951 0.17449212 0.06711711 0.18885787 0.03662316
0.1226368 0.20623612 0.1371511 0.06301977 0.05377805 0.02044071
0.07306211 0.00730291 0.02932294 0.0193199 0.02619412 0.0204607
0.02536301 0.02602958 0.0815886 0.07754666 0.01302227 0.15267093
0.08529373 0.00839241 0.08830106 0.04258077 0.06510606 0.08714733
0.09652298 0.16992335 0.00873204 0.04909573 0.01785387 0.01237347
0.08418552 0.24211199 0.0422243 0.1793361 0.0154549 0.00799404
0.01506692 0.17877294 0.20004141 0.13304119 0.14527565 0.08852351
0.05173086 0.02800703 0.10004384 0.05408717 0.0157574 0.18044461
0.04537623 0.07583842 0.15127688 0.06006866 0.04503374 0.07682175
0.00204775 0.13950375 0.0458627 0.13266404 0.06712269 0.03587277
0.02672294 0.05953323 0.02113297 0.016502 0.20327103 0.04900302
0.02238234 0.00264506 0.01121504 0.18968254 0.04769414 0.01039635
0.0551518 0.06952842 0.09926505 0.00047308]
numpy操作
import numpy as np
#numpy的操作
print(np.arange(1,11).reshape([2,-1]))#5可以用-1代替
lst = np.arange(1,11).reshape([2,-1])
print('exp')
print(np.exp(lst))#自然指数
print('exp2')
print(np.exp2(lst))#自然指数的平方
print('sqrt:')
print(np.sqrt(lst))#开方
print('sin:')
print(np.sin(lst))
print ('log:')
print(np.log(lst))
print ('sum:')
lst1 = np.array([[[1,2,3,4],
[5,6,7,8]],
[[9,10,11,12],
[13,14,15,16]],
[[17,18,19,20],
[21,22,23,24]]])
print ('axis=0:')
print(lst1.sum(axis=0))
print ('axis=1:')
print(lst1.sum(axis=1))
print ('axis=2:')
print(lst1.sum(axis=2))
print ('max:')
print ('无内容:')
print(lst1.max())
print ('axis=0:')
print(lst1.max(axis=0))
print ('axis=1:')
print(lst1.max(axis=1))
print ('axis=2:')
print(lst1.max(axis=2))
print ('add:')
lst2 = np.array([1,2,3,4])
lst3 = np.array([7,8,9,10])
print(lst2+lst3)
print('dot:')
print(np.dot(lst2.reshape([2,2]),lst3.reshape([2,2])))
print('concatenate追加:')
print (np.concatenate((lst2,lst3),axis=0))
print (np.concatenate((lst1,lst1),axis=1))
print('vstack:')
print(np.vstack((lst2,lst3)))
print('hstack:')
print(np.hstack((lst2,lst3)))
print('split:')
print(np.split(lst1,3))
print('copy:')
print(np.copy(lst1))
输出结果:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
exp
[[ 2.71828183e+00 7.38905610e+00 2.00855369e+01 5.45981500e+01
1.48413159e+02]
[ 4.03428793e+02 1.09663316e+03 2.98095799e+03 8.10308393e+03
2.20264658e+04]]
exp2
[[ 2. 4. 8. 16. 32.]
[ 64. 128. 256. 512. 1024.]]
sqrt:
[[ 1. 1.41421356 1.73205081 2. 2.23606798]
[ 2.44948974 2.64575131 2.82842712 3. 3.16227766]]
sin:
[[ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427]
[-0.2794155 0.6569866 0.98935825 0.41211849 -0.54402111]]
log:
[[ 0. 0.69314718 1.09861229 1.38629436 1.60943791]
[ 1.79175947 1.94591015 2.07944154 2.19722458 2.30258509]]
sum:
axis=0:
[[27 30 33 36]
[39 42 45 48]]
axis=1:
[[ 6 8 10 12]
[22 24 26 28]
[38 40 42 44]]
axis=2:
[[10 26]
[42 58]
[74 90]]
max:
无内容:
24
axis=0:
[[17 18 19 20]
[21 22 23 24]]
axis=1:
[[ 5 6 7 8]
[13 14 15 16]
[21 22 23 24]]
axis=2:
[[ 4 8]
[12 16]
[20 24]]
add:
[ 8 10 12 14]
dot:
[[25 28]
[57 64]]
concatenate追加:
[ 1 2 3 4 7 8 9 10]
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 1 2 3 4]
[ 5 6 7 8]]
[[ 9 10 11 12]
[13 14 15 16]
[ 9 10 11 12]
[13 14 15 16]]
[[17 18 19 20]
[21 22 23 24]
[17 18 19 20]
[21 22 23 24]]]
vstack:
[[ 1 2 3 4]
[ 7 8 9 10]]
hstack:
[ 1 2 3 4 7 8 9 10]
split:
[array([[[1, 2, 3, 4],
[5, 6, 7, 8]]]), array([[[ 9, 10, 11, 12],
[13, 14, 15, 16]]]), array([[[17, 18, 19, 20],
[21, 22, 23, 24]]])]
copy:
[[[ 1 2 3 4]
[ 5 6 7 8]]
[[ 9 10 11 12]
[13 14 15 16]]
[[17 18 19 20]
[21 22 23 24]]]