下边代码是关于numpy的一些基本用法,包括数组和矩阵操作等...
1 import numpy as np 2 print "<== print version ==>" 3 print np.version.version 4 print "<== 1-dimensional array ==>" 5 print np.array([1, 2, 3, 4, 5]) 6 print "<== 2-dimentional array ==>" 7 print np.array([[1,2],[3,4]]) 8 print "<== int32,int16,etc. ==>" 9 print np.array((1, 2, 3, 4), dtype = np.float64) 10 print "<== get a 3*5 array ==>" 11 print np.arange(15).reshape(3, 5) 12 print "<== generate 4 data from 1 to 5 ==>" 13 print np.linspace(1, 5, 4) 14 print "<== like what in matlab ==>" 15 print np.zeros((2, 5))# 16 print ‘\n‘ 17 print np.ones((2,5)) 18 print ‘\n‘ 19 print np.eye(3) 20 21 a = np.eye(4) 22 print "<== sum ==>" 23 a.sum() 24 a.sum(axis=0) 25 print "<== min and max ==>" 26 a.min() 27 a.max() 28 np.sin(a) 29 np.floor(a) 30 np.exp(a) 31 np.dot(a, a) 32 33 a = np.ones((2,2)) 34 b = np.eye(2) 35 print "<== visit array ==>" 36 print a[0, 0] 37 print "<== merge: shallow copy: learn from v and h ==>" 38 print np.vstack((a,b)) 39 print np.hstack((a,b)) 40 print "<== deep copy ==>" 41 c = a.copy() 42 print "<== transpose ==>" 43 print c.transpose() 44 print "<== trace ==>" 45 print c.trace() 46 print "<== more matrix operations in linalg ==>" 47 import numpy.linalg as nplg 48 print nplg.eig(a)
时间: 2024-12-25 02:05:14