import numpy as np
a=np.arange(20).reshape(4,5) #数组n不变,返回维度为(x,y)的数组
np.savetxt('test.csv',a,fmt='%d',delimiter=',')
np.loadtxt('test.csv',delimiter=',')
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.]])
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
a=np.zeros((5,5))
for i in range(5):
for j in range(5):
a[i,j]=j
print(a)
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
import numpy as np
arr=np.linspace(0,1,12)
display(arr)
arr2=np.logspace(0,10,base=2,num=11,dtype='int')
print(arr2)
array([0. , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
0.90909091, 1. ])
[ 1 2 4 8 16 32 64 128 256 512 1024]
arr9=np.random.randint(1,10,size=10)
cond= arr9==arr9.max()
arr9[cond]=-100
print(arr9)
[ 5 4 7 -100 6 5 -100 7 6 5]
import numpy as np
arr=np.random.rand(5,5)
index=np.argsort(arr[:,2]) # np.argsort()方法
print(index)
arr[index]
[4 3 1 0 2]
array([[0.54723143, 0.35730132, 0.33367611, 0.26447109, 0.72313824],
[0.18892659, 0.37189964, 0.33428081, 0.73356775, 0.1459447 ],
[0.95830132, 0.14138699, 0.43426274, 0.20163973, 0.07713201],
[0.11808383, 0.05367191, 0.60308876, 0.39207281, 0.14945413],
[0.79998189, 0.32714614, 0.61650152, 0.3904105 , 0.07404079]])
numpy.arange(n).reshape(a, b) 依次生成n个自然数,并且以a行b列的数组形式显示
import numpy as np
a = np.arange(1,11)
b = np.arange(1,11)
c = np.arange(1,11)
a1 = a.reshape(10,1)
b1 = a.reshape(2,5)
c1 = a.reshape(5,2)
print ("数组10*1:")
print (a1)
print ("数组2*5:")
print (b1)
print ("数组5*2:")
print (c1)
数组10*1:
[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
数组2*5:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
数组5*2:
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]]
Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
0.015863588394151695 0.9753507582983225
Z = np.dot(np.ones((2,3)), np.ones((3,4)))
print(Z)
[[3. 3. 3. 3.]
[3. 3. 3. 3.]]
今天下午六点开始数学建模啦