import numpy as np
#dtype:自定义数据类型
d=np.dtype([('age',np.int8),('sex',np.int8)])
a=np.array([(12,1),(23,0)],dtype=d)
print(a)
print(a['age'])
print(a['sex'])
##OR
d=np.dtype({'names':['age','sex'],'formats':[np.int8,np.int8]})
a=np.array([(12,1),(23,0)],dtype=d)
print(a)
print(a['age'])
print(a['sex'])
#ndarray属性
a=np.array([[1,2,3],[4,5,6]]);
print(a.ndim,a.shape,a.size,a.itemsize)#维数,行列数,元素个数,每个元素所占字节
b=np.array([[1,2,3],[4,5,6]],dtype=np.float);
b.shape=(3,-1)
print(b.ndim,b.shape,b.size,b.itemsize)#维数,行列数,元素个数,每个元素所占字节
print(a,'\n',b)
[(12, 1) (23, 0)]
[12 23]
[1 0]
[(12, 1) (23, 0)]
[12 23]
[1 0]
2 (2, 3) 6 4
2 (3, 2) 6 8
[[1 2 3]
[4 5 6]]
[[1. 2.]
[3. 4.]
[5. 6.]]
#创建数组
a=np.array([1,2,3])
print(a)
[1 2 3]
b=np.array([[1,2,3],[4,5,6]])
print(b)
print(b.shape)
[[1 2 3]
[4 5 6]]
(2, 3)
b.shape=(1,-1)
print(b)
b.shape=(3,-1)
print(b)
[[1 2 3 4 5 6]]
[[1 2]
[3 4]
[5 6]]
c=b.reshape((3,2))
print(c)
[[1 2]
[3 4]
[5 6]]
b[1,1]=100
print(b)
print(c)
[[ 1 2]
[ 3 100]
[ 5 6]]
[[ 1 2]
[ 3 100]
[ 5 6]]
d1=np.linspace(0,1,20,endpoint=True)
d2=np.logspace(0,10,11,endpoint=True,base=2)
print(d1)
print(d2)
[0. 0.05263158 0.10526316 0.15789474 0.21052632 0.26315789
0.31578947 0.36842105 0.42105263 0.47368421 0.52631579 0.57894737
0.63157895 0.68421053 0.73684211 0.78947368 0.84210526 0.89473684
0.94736842 1. ]
[1.000e+00 2.000e+00 4.000e+00 8.000e+00 1.600e+01 3.200e+01 6.400e+01
1.280e+02 2.560e+02 5.120e+02 1.024e+03]
e1=np.zeros((3,3),dtype=np.int)
e2=np.ones((3,3),dtype=np.int)
e3=np.empty((4,4),dtype=np.float)
print(e1)
print(e2)
print(e3)
[[0 0 0]
[0 0 0]
[0 0 0]]
[[1 1 1]
[1 1 1]
[1 1 1]]
[[6.23042070e-307 4.67296746e-307 1.69121096e-306 8.34441742e-308]
[1.78022342e-306 6.23058028e-307 9.79107872e-307 6.89807188e-307]
[7.56594375e-307 6.23060065e-307 1.78021527e-306 8.34454050e-308]
[1.11261027e-306 1.15706896e-306 1.33512173e-306 1.69105613e-306]]
e4=np.zeros_like(e3)
e5=np.ones_like(e4)
print(e4,e5)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]] [[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
#通过已有数组创建数组
a=[1,2,3,4]
b=np.asarray(a)
print(b)
a=[[1,2,3,4],(5,6)]
b=np.asarray(a)
print(a)
[1 2 3 4]
[[1, 2, 3, 4], (5, 6)]
a=b'shit'
b=np.frombuffer(a,dtype='S1',count=-1,offset=0)
print(b)
[b's' b'h' b'i' b't']
def f(i,j):
return i+j
a=np.fromfunction(f,(3,5))#返回索引的和
print(a)
[[0. 1. 2. 3. 4.]
[1. 2. 3. 4. 5.]
[2. 3. 4. 5. 6.]]
#元素的存取
a=np.arange(0,10,1)
b=a[7:1:-2]
print(a,'\n',b)
b[1]=10000
print(a,'\n',b)
#通过下标获取的新数组是原始数组的一个视图,共享同一块存储空间
[0 1 2 3 4 5 6 7 8 9]
[7 5 3]
[ 0 1 2 3 4 10000 6 7 8 9]
[ 7 10000 3]
#使用整数序列访问,这里类似matlab的操作
#整数序列可以用列表或数组,由整数序列得到的部分数组不与原数组分享同一块存储空间
a=np.arange(0,10,1)
b=a[np.array([3,5,1,2])]
c=a[[3,5,1,2]]
print(a,'\n',b,'\n',c)
b[1]=100
c[1]=100
print(a,'\n',b,'\n',c)
[0 1 2 3 4 5 6 7 8 9]
[3 5 1 2]
[3 5 1 2]
[0 1 2 3 4 5 6 7 8 9]
[ 3 100 1 2]
[ 3 100 1 2]
#使用布尔数组(不能是列表),不共享存储空间
a=np.arange(0,10,1)
print(a>5)
a=a[a>5]
print(a)
print(np.all(a>5))
[False False False False False False True True True True]
[6 7 8 9]
True
print(a.any(),np.any(a),a.all(),np.all(a),np.all(a>7))
#any:有一个元素等价于true则返回true,
#all:所有元素等价于true则返回true
True True True True False
#多维数组的操作类似:也可以利用整数序列和布尔数组
a=np.linspace(1,64,64,endpoint=True)
a.shape=(8,-1)
print(a)
print(a[::-1,1:7:2])
b=a[:,0]
print(a[b>20,:])
print(a[(0,1,2,3,4,5),(0,1,2,3,4,5)])#组元(可以不加括号)
print(a[3,[0,2,5]])
[[ 1. 2. 3. 4. 5. 6. 7. 8.]
[ 9. 10. 11. 12. 13. 14. 15. 16.]
[17. 18. 19. 20. 21. 22. 23. 24.]
[25. 26. 27. 28. 29. 30. 31. 32.]
[33. 34. 35. 36. 37. 38. 39. 40.]
[41. 42. 43. 44. 45. 46. 47. 48.]
[49. 50. 51. 52. 53. 54. 55. 56.]
[57. 58. 59. 60. 61. 62. 63. 64.]]
[[58. 60. 62.]
[50. 52. 54.]
[42. 44. 46.]
[34. 36. 38.]
[26. 28. 30.]
[18. 20. 22.]
[10. 12. 14.]
[ 2. 4. 6.]]
[[25. 26. 27. 28. 29. 30. 31. 32.]
[33. 34. 35. 36. 37. 38. 39. 40.]
[41. 42. 43. 44. 45. 46. 47. 48.]
[49. 50. 51. 52. 53. 54. 55. 56.]
[57. 58. 59. 60. 61. 62. 63. 64.]]
[ 1. 10. 19. 28. 37. 46.]
[25. 27. 30.]
#广播机制
##若一个数组的维度小于另一个,则在左边补1直至维度相同
##然后针对不相同的维度,从1开始扩展,若不相同的维度中没有1,那么这就不符合广播机制
#例子:(2,3,4)&(4)->(2,3,4)&(1,1,4)->(2,3,4)&(2,1,4)->(2,3,4)&(2,3,4)
a=np.arange(1,10)
b=np.arange(1,10)
b.shape=(b.size,1)
print(a.shape,b.shape)
print(a+b)
#(1,9)&(9,1)->(9,9)&(9,1)->(9,9)&(9,9)
(9,) (9, 1)
[[ 2 3 4 5 6 7 8 9 10]
[ 3 4 5 6 7 8 9 10 11]
[ 4 5 6 7 8 9 10 11 12]
[ 5 6 7 8 9 10 11 12 13]
[ 6 7 8 9 10 11 12 13 14]
[ 7 8 9 10 11 12 13 14 15]
[ 8 9 10 11 12 13 14 15 16]
[ 9 10 11 12 13 14 15 16 17]
[10 11 12 13 14 15 16 17 18]]
# 内置操作函数
import math
print(math.pi)
#数学函数(基本和matlab一样)
a=np.linspace(math.pi/2,math.pi,10,endpoint=True)
print(a)
print(np.log(a),'\n',np.log2(a),'\n',np.log10(a),'\n',np.exp(a))
print(np.sin(a),'\n',np.cos(a),'\n',np.tan(a))
b=np.linspace(0.25,0.75,10)
print(np.arcsin(b),'\n',np.arccos(b),'\n',np.arctan(a))
3.141592653589793
[1.57079633 1.74532925 1.91986218 2.0943951 2.26892803 2.44346095
2.61799388 2.7925268 2.96705973 3.14159265]
[0.45158271 0.55694322 0.6522534 0.73926478 0.81930749 0.89341546
0.96240833 1.02694685 1.08757147 1.14472989]
[0.65149613 0.80349922 0.94100275 1.06653363 1.18201085 1.28892605
1.38846172 1.48157113 1.56903397 1.65149613]
[0.19611988 0.24187737 0.28327005 0.32105861 0.35582072 0.3880054
0.41796863 0.44599735 0.47232629 0.49714987]
[ 4.81047738 5.72778705 6.82001845 8.1205274 9.66903032 11.51281719
13.70819567 16.32221075 19.43469223 23.14069263]
[1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
1.73648178e-01 1.22464680e-16]
[ 6.12323400e-17 -1.73648178e-01 -3.42020143e-01 -5.00000000e-01
-6.42787610e-01 -7.66044443e-01 -8.66025404e-01 -9.39692621e-01
-9.84807753e-01 -1.00000000e+00]
[ 1.63312394e+16 -5.67128182e+00 -2.74747742e+00 -1.73205081e+00
-1.19175359e+00 -8.39099631e-01 -5.77350269e-01 -3.63970234e-01
-1.76326981e-01 -1.22464680e-16]
[0.25268026 0.31052183 0.36945913 0.42977543 0.49181009 0.55598215
0.62282659 0.69305308 0.76764752 0.84806208]
[1.31811607 1.26027449 1.2013372 1.1410209 1.07898623 1.01481417
0.94796974 0.87774325 0.80314881 0.72273425]
[1.00388482 1.05049817 1.09059189 1.12533883 1.15567236 1.18233656
1.20592739 1.226925 1.24571888 1.26262726]
#运算函数,例如diff
a=np.array([[2,6,9],[1,4,2],[6,9,3]])
print(a)
print(np.diff(a,axis=0))#沿着第一个维度
print(np.diff(a,axis=1))#沿着第二个维度
[[2 6 9]
[1 4 2]
[6 9 3]]
[[-1 -2 -7]
[ 5 5 1]]
[[ 4 3]
[ 3 -2]
[ 3 -6]]
#统计函数(这里就是举个例子,具体的函数太多了)
a=np.arange(10)
print(np.std(a))#标准差
2.8722813232690143
#线性代数模块
a=np.arange(1,10)
a.shape=(3,3)
b=np.array([3,4,2])
print(np.linalg.norm(a))
print(np.dot(a,b))
print(np.trace(a))
print(np.linalg.det(a))
print(np.linalg.matrix_rank(a))
c=np.array([[2,1],[1,2]])
u,v=np.linalg.eig(c)
l=np.linalg.cholesky(c)#要求是正定的
U,s,V=np.linalg.svd(c)
16.881943016134134
[17 44 71]
15
-9.51619735392994e-16
2
#随机模块random
print(np.random.rand(3,3))
print(np.random.randint(1,10))
A=[];
L=[];
for i in range(10):
local_seed=np.random.RandomState(5)#局部种子
A.append(np.random.randint(1,10));#每次运行结果不一样
L.append(local_seed.rand(1,1));#每次运行结果一样
print(A,'\n',L)
np.random.seed(666)#全局种子,无论程序运行几次,产生的随机数都是一样的
A=[];
L=[];
for i in range(10):
A.append(np.random.randint(1,10));#每次运行结果不一样
L.append(local_seed.rand(1,1));#每次运行结果一样
print(A,'\n',L)
[[0.2427146 0.57043673 0.57773404]
[0.89274848 0.12138544 0.66083873]
[0.16459271 0.09435837 0.5876144 ]]
5
[2, 8, 6, 3, 3, 3, 7, 9, 6, 9]
[array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]]), array([[0.22199317]])]
[3, 7, 5, 4, 2, 1, 9, 8, 6, 3]
[array([[0.87073231]]), array([[0.20671916]]), array([[0.91861091]]), array([[0.48841119]]), array([[0.61174386]]), array([[0.76590786]]), array([[0.51841799]]), array([[0.2968005]]), array([[0.18772123]]), array([[0.08074127]])]
#随机分布
print(np.random.binomial(n=5,p=0.5,size=10))#二项分布
print(np.random.uniform(-1,1,5))#均匀分布
print(np.random.normal(loc=0,scale=5,size=(2,3)))#正态分布
[2 2 1 3 3 4 3 4 2 4]
[-0.30047619 0.84779385 -0.41021094 0.04876123 0.88507793]
[[-0.92964911 -1.76857603 -9.7666497 ]
[-1.71882432 -7.38465809 -3.50114856]]
#Monte Corle
#三扇门,其中一扇门后有奖品,这扇门只有主持人知道。选手先随机选一扇门,但并不打开,主持人看到后,会打开其余两扇门中没有奖品的一扇门。然后,主持人问选手,是否要改变一开始的选择?
#你会改变选择吗?
np.random.seed(666)
N=100000
prize_door=np.random.randint(1,4,N)
win_time1=0#不改,赢的次数
win_time2=0#改,赢的次数
for i in range(N):
prize=prize_door[i];
choice=np.random.randint(1,4,1)
remain=[j for j in [1,2,3] if j!=choice]
canopen=[j for j in remain if j!=prize]
finalopen=np.random.choice(canopen)
if choice==prize:
win_time1=win_time1+1;
else:
win_time2=win_time2+1;
print(win_time1/N,win_time2/N)
0.33165 0.66835
版权声明:本文为captaincoke原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。