numpy学习2

阿里云双11来了!从本博客参与阿里云,服务器最低只要86元/年!

常用函数
import numpy as np
a = np.arange(15).reshape(3, 5) #变换为3行5列的矩阵
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
a.shape
(3, 5)
#the number of axes (dimensions) of the array
a.ndim
2
a.dtype.name
'int32'
#the total number of elements of the array
a.size
15
np.zeros ((3,4))  #由构造值为0的矩阵
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
np.ones( (2,3,4), dtype=np.int32 ) # 构造值为1的矩阵 dtype指定类型
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],
       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])
#To create sequences of numbers
np.arange( 10, 30, 5 ) #构造出10开始到30的数组,间隔为5
array([10, 15, 20, 25])
np.arange( 0, 2, 0.3 )
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])
np.arange(12).reshape(4,3)
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
np.random.random((2,3)) #随机产生-1到1的数,形成2行3列的矩阵(np下random模块下的random函数)
array([[ 0.40130659,  0.45452825,  0.79776512],
       [ 0.63220592,  0.74591134,  0.64130737]])
from numpy import pi
np.linspace( 0, 2*pi, 100 ) #区间是0-2pi  造100个值
array([ 0.        ,  0.06346652,  0.12693304,  0.19039955,  0.25386607,
        0.31733259,  0.38079911,  0.44426563,  0.50773215,  0.57119866,
        0.63466518,  0.6981317 ,  0.76159822,  0.82506474,  0.88853126,
        0.95199777,  1.01546429,  1.07893081,  1.14239733,  1.20586385,
        1.26933037,  1.33279688,  1.3962634 ,  1.45972992,  1.52319644,
        1.58666296,  1.65012947,  1.71359599,  1.77706251,  1.84052903,
        1.90399555,  1.96746207,  2.03092858,  2.0943951 ,  2.15786162,
        2.22132814,  2.28479466,  2.34826118,  2.41172769,  2.47519421,
        2.53866073,  2.60212725,  2.66559377,  2.72906028,  2.7925268 ,
        2.85599332,  2.91945984,  2.98292636,  3.04639288,  3.10985939,
        3.17332591,  3.23679243,  3.30025895,  3.36372547,  3.42719199,
        3.4906585 ,  3.55412502,  3.61759154,  3.68105806,  3.74452458,
        3.8079911 ,  3.87145761,  3.93492413,  3.99839065,  4.06185717,
        4.12532369,  4.1887902 ,  4.25225672,  4.31572324,  4.37918976,
        4.44265628,  4.5061228 ,  4.56958931,  4.63305583,  4.69652235,
        4.75998887,  4.82345539,  4.88692191,  4.95038842,  5.01385494,
        5.07732146,  5.14078798,  5.2042545 ,  5.26772102,  5.33118753,
        5.39465405,  5.45812057,  5.52158709,  5.58505361,  5.64852012,
        5.71198664,  5.77545316,  5.83891968,  5.9023862 ,  5.96585272,
        6.02931923,  6.09278575,  6.15625227,  6.21971879,  6.28318531])
np.sin(np.linspace( 0, 2*pi, 100 )) #可以对结果做运算操作
array([  0.00000000e+00,   6.34239197e-02,   1.26592454e-01,
         1.89251244e-01,   2.51147987e-01,   3.12033446e-01,
         3.71662456e-01,   4.29794912e-01,   4.86196736e-01,
         5.40640817e-01,   5.92907929e-01,   6.42787610e-01,
         6.90079011e-01,   7.34591709e-01,   7.76146464e-01,
         8.14575952e-01,   8.49725430e-01,   8.81453363e-01,
         9.09631995e-01,   9.34147860e-01,   9.54902241e-01,
         9.71811568e-01,   9.84807753e-01,   9.93838464e-01,
         9.98867339e-01,   9.99874128e-01,   9.96854776e-01,
         9.89821442e-01,   9.78802446e-01,   9.63842159e-01,
         9.45000819e-01,   9.22354294e-01,   8.95993774e-01,
         8.66025404e-01,   8.32569855e-01,   7.95761841e-01,
         7.55749574e-01,   7.12694171e-01,   6.66769001e-01,
         6.18158986e-01,   5.67059864e-01,   5.13677392e-01,
         4.58226522e-01,   4.00930535e-01,   3.42020143e-01,
         2.81732557e-01,   2.20310533e-01,   1.58001396e-01,
         9.50560433e-02,   3.17279335e-02,  -3.17279335e-02,
        -9.50560433e-02,  -1.58001396e-01,  -2.20310533e-01,
        -2.81732557e-01,  -3.42020143e-01,  -4.00930535e-01,
        -4.58226522e-01,  -5.13677392e-01,  -5.67059864e-01,
        -6.18158986e-01,  -6.66769001e-01,  -7.12694171e-01,
        -7.55749574e-01,  -7.95761841e-01,  -8.32569855e-01,
        -8.66025404e-01,  -8.95993774e-01,  -9.22354294e-01,
        -9.45000819e-01,  -9.63842159e-01,  -9.78802446e-01,
        -9.89821442e-01,  -9.96854776e-01,  -9.99874128e-01,
        -9.98867339e-01,  -9.93838464e-01,  -9.84807753e-01,
        -9.71811568e-01,  -9.54902241e-01,  -9.34147860e-01,
        -9.09631995e-01,  -8.81453363e-01,  -8.49725430e-01,
        -8.14575952e-01,  -7.76146464e-01,  -7.34591709e-01,
        -6.90079011e-01,  -6.42787610e-01,  -5.92907929e-01,
        -5.40640817e-01,  -4.86196736e-01,  -4.29794912e-01,
        -3.71662456e-01,  -3.12033446e-01,  -2.51147987e-01,
        -1.89251244e-01,  -1.26592454e-01,  -6.34239197e-02,
        -2.44929360e-16])
#the product operator * operates elementwise in NumPy arrays
a = np.array( [20,30,40,50] )
b = np.arange( 4 )
#print a 
#print b
#b
c = a-b #减去对应位置的值
#print c
c = c-1  #每个位置减去1
#print c
b**2 # 每个位置的值求平方
#print b**2
print a<35 #判断每个值是否小于35
[ True  True False False]
#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
               [0,1]] )
B = np.array( [[2,0],
               [3,4]] )
print A
print B
#print A*B #对应位置相乘  星乘
#行乘列 点乘
#第一行乘第1列和为5  第一行乘第2列和为4  第二行乘第1列和为3 第二行乘第2列和为4
print A.dot(B)   
print np.dot(A, B) 
[[1 1]
 [0 1]]
[[2 0]
 [3 4]]
[[5 4]
 [3 4]]
[[5 4]
 [3 4]]

其他操作

import numpy as np
B = np.arange(3)
print B
#print np.exp(B) #e的多少次幂
print np.sqrt(B)  #根号
[0 1 2]
[ 0.          1.          1.41421356]
#Return the floor of the input
a = np.floor(10*np.random.random((3,4))) #向下取整,对随机生成的矩阵
#print a
#a.shape
## flatten the array
#print a.ravel() #矩阵拉成向量
#a.shape = (6, 2) #向量变矩阵
#print a 
#print a.T #a的转置矩阵
print a.resize((2,6))
print a
#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
#a.reshape(3,-1) #3行,-1表示列自动计算:如12个元素,3行 4列
None
[[ 9.  7.  6.  4.  9.  0.]
 [ 2.  9.  1.  3.  4.  0.]]
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print a
print '---'
print b
print '---'
print np.hstack((a,b)) #横着拼接矩阵  vstack纵向拼接
#np.hstack((a,b))
[[ 5.  6.]
 [ 1.  5.]]
---
[[ 8.  6.]
 [ 9.  0.]]
---
[[ 5.  6.  8.  6.]
 [ 1.  5.  9.  0.]]
a = np.floor(10*np.random.random((2,12)))
#print a
#print np.hsplit(a,3)  # 横着切分
#print np.hsplit(a,(3,4))   # Split a after the third and the fourth column
#(3,4)在第3和4的位置切
a = np.floor(10*np.random.random((12,2)))
print a
np.vsplit(a,3) #vsplit竖着切
[[ 5.  2.]
 [ 1.  3.]
 [ 9.  6.]
 [ 2.  2.]
 [ 7.  2.]
 [ 8.  2.]
 [ 1.  7.]
 [ 2.  8.]
 [ 4.  4.]
 [ 8.  5.]
 [ 4.  3.]
 [ 2.  3.]]
[array([[ 5.,  2.],
        [ 1.,  3.],
        [ 9.,  6.],
        [ 2.,  2.]]), array([[ 7.,  2.],
        [ 8.,  2.],
        [ 1.,  7.],
        [ 2.,  8.]]), array([[ 4.,  4.],
        [ 8.,  5.],
        [ 4.,  3.],
        [ 2.,  3.]])]
#Simple assignments make no copy of array objects or of their data.
a = np.arange(12)
b = a  # a,b指向同一个区域  =复制
# a and b are two names for the same ndarray object
b is a
b.shape = 3,4
print a.shape
print id(a) #id为创建位置的编号
print id(b)
(3, 4)
82691200
82691200
#The view method creates a new array object that looks at the same data.
c = a.view()  #复制 指向的地方不同   值共用的  浅复制
c is a
c.shape = 2,6
#print a.shape
c[0,4] = 1234
a
array([[   0,    1,    2,    3],
       [1234,    5,    6,    7],
       [   8,    9,   10,   11]])
#The copy method makes a complete copy of the array and its data.
d = a.copy() # 用a初始化 复制给 d  指向不同  值不同 独立(常用!!) 深复制
d is a
d[0,0] = 9999
print d 
print a
[[9999    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
import numpy as np
#data = np.sin(np.arange(20)).reshape(5,4)
#print data
#ind = data.argmax(axis=0)  #按列找最大值的索引
#print ind
#data_max = data[ind, xrange(data.shape[1])]
#print data_max
all(data_max == data.max(axis=0))
True
a = np.arange(0, 40, 10)
b = np.tile(a, (3, 5))   #扩展  :行3倍  列变5倍
print b
[[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]]
a = np.arange(0, 40, 10)
print a
print '---'
b = np.tile(a, (1, 4))
print b
#print a
#print b
a = np.array([[4, 3, 5], [1, 2, 1]])
#print a
#b = np.sort(a, axis=1)  按行排序 从小到大
#print b
#b
#a.sort(axis=1)
#print a
a = np.array([4, 3, 1, 2])
j = np.argsort(a)  # 获取到的从小到大的索引值
print j
print a[j]
[2 3 1 0]
[1 2 3 4]

https://www.jianshu.com/p/564b87b223ae

Python量化投资网携手4326手游为资深游戏玩家推荐:《《赛特斯2》:Cytus II X Hatsune Miku 收录歌曲预告

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
Python
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论