import numpy as np
a = np.array([1,2,3])
print(a)
a += 1
print (a)
创建数组:
import numpy as np
a = np.array([[1,2,3],[4,5,6]],dtype = np.complex)
print(a)
a.shape = (3,2)
print(a)
import numpy as np
x1 = np.empty([3,2],dtype = int)
print(x1)
x2 = np.zeros([3,2], dtype = np.int, order = 'C')
print(x2)
x3 = np.zeros([2,2],dtype = [('x','i4'),('y','i4')])
print(x3)
x4 = np.ones([2,2],dtype = int,order = 'C')
print(x4)
用各种方法创建不同类型数组:
import numpy as np
arr1 = range(0,5,1)
print(arr1)
arr2 = np.arange(3,9,0.2)
print(arr2)
arr3 = np.linspace(1,5,10)
print(arr3)
用随机函数创建数组:
import numpy as np
x1 = np.random.rand(2,3)
print(x1)
x2 = np.random.randint(0,10,(2,2),dtype = int)
print(x2)
x3 = np.random.random(2,3)
print(x3)
x4 = np.random.uniform(1,2,(2,3))
print(x4)
数组切片:
arr = np.arange(24).reshape(4,6)
print(arr)
arr1 = arr[1:3,1:3]
print(arr1)
arr1 = arr[1: , :3]
print(arr1)
import numpy as np
arr1 = np.arange(12).reshape(3,4)
arr11 = arr1[:1,:]
arr2 = np.arange(12).reshape(3,4)
arr22 = arr2[:1,:]
arr3 = np.array([arr1,arr2])
arr33 = np.array([arr11,arr22])
print(arr3)
print(arr33)
arr5 = np.arange(12).reshape(2,3,4)
arr6 = arr5[0:2,0,0:5]
print(arr6)
|