numpy初探:
import random import numpy as np t1 = np.array([1,2,3]) print(t1) print(type(t1)) t2 = np.array(range(10),dtype='i2') print(t2) print(type(t2)) print(t2.dtype) t3 = t2.astype(dtype='i1') print(t3) print(t3.dtype) t4 = np.arange(10) print(t4) t5 = np.array( [round(random.random() , 3) for i in range(10)] ) print(t5)
进阶:
import random import numpy as np t1 = np.array([[1,2,3],[4,5,6]]) print(t1.shape) t1.reshape(3,2) print(t1) print(t1.shape) print(t1.reshape(3,2))#这种有return的函数,一般不对原数组做改变,extend等方法是例外 print(t1.reshape(3,2).shape) print(t1.shape[0] * t1.shape[1]) print(t1.flatten())#转换成一维 print(t1.flatten().shape[0]) print(t1+2)#广播机制 print(t1*2) print(t1 + t1) t2 = np.array([[[1,2,3],[4,5,6],[7,8,9]], [[1,2,3],[4,5,6],[7,8,9]], [[1,2,3],[4,5,6],[7,8,9]]]) t3 = np.array([[0,1,2],[3,4,5],[7,8,9]]) print(t2 - t3) t2 = np.array([[[1,2],[4,5],[7,8]], [[1,2],[4,5],[7,8]], [[1,2],[4,5],[7,8]]]) t3 = np.array([[0],[1],[2]]) print("此之谓广播机制,尾号相同或者为1即可运算") print(t2.shape) print(t3.shape) print(t2-t3)
索引与切片:
标签:t2,t1,np,shape,print,array,numpy From: https://www.cnblogs.com/-ark/p/16594770.html