Numpy的基础用法--常用函数(一)

# encoding=utf-8
import numpy as np


def startMain():
    lst = [[1, 3, 5], [2, 4, 6]]
    np_lst = np.array(lst)
    np_lst1 = np.array(lst, dtype=np.float)
    print(type(np_lst1))
    print(type(np_lst))
    print(np_lst.shape)  # 形状(2, 3)
    print(np_lst.ndim)  # 维度 2
    print(np_lst.dtype)  # int64
    print(np_lst.itemsize)  # 每个元素 size 8 byte
    print(np_lst.size)  # 元素个数
    print(type(lst))

    # some Arrays
    print(np.zeros([2, 4]))
    print(np.ones([3, 4]))
    print("Rand:")
    print(np.random.rand(2, 4))  # 两行四列随机数0-1
    print(np.random.rand())  # 随机数0-1
    print("RandInt:")
    print(np.random.randint(1, 10))  # 1-10之间随机整数
    print(np.random.randint(1, 10, 3))  # 3个随机整数
    print("Randn:")
    print(np.random.randn())
    print(np.random.randn(2, 4))  # 标准正态
    print("Choice:")
    print(np.random.choice([10, 20, 30, 2, 8]))  # 指定数字中随机选
    print("Distribute:")
    print(np.random.beta(1, 10, 100))  # 1001-10β分布
    # some Operation
    print(np.arange(1, 11).reshape([2, -1]))
    lst=np.arange(1,11).reshape([2,-1])
    print("Exp:")
    print(np.exp(lst))
    print("Exp2:")
    print(np.exp2(lst))
    print("Sqrt:")
    print(np.sqrt(lst))
    print("Sin:")
    print (np.sin(lst))
    print("Log:")
    print (np.log(lst))
    print ("basic operation")
    lst=np.array([[[1,2,3,4],
                   [4,5,6,7]],
                  [[7,8,9,10],
                   [10,11,12,13]],
                  [[14,15,16,17],
                   [18,19,20,21]]
                  ])
    print ("sum:")
    print (lst.sum())#默认求所有元素之和
    print (lst.sum(axis=0))#从外向里延伸,axis最大可以等于数组维数-1,axis越大深入程度越大
    print (lst.sum(axis=1))
    print (lst.sum(axis=2))
    print ("max:")
    print (lst.max(axis=0))
    print (lst.max(axis=1))
    print (lst.max(axis=2))
    print ("min:")
    print (lst.min(axis=0))
    print (lst.min(axis=1))
    print (lst.min(axis=2))

    lst1=np.array([10,20,30,40])
    lst2=np.array([4,3,2,1])
    print ("+")
    print (lst1+lst2)
    print ("-")
    print (lst1-lst2)
    print ("Mul:")
    print (lst1*lst2)
    print ("Div")
    print (lst1/lst2)
    print "Square"
    print (lst1**2)#平方
    print "Dot"
    print (np.dot(lst1.reshape([2,2]),lst2.reshape([2,2])))#点乘
    print ("Concatenate:")
    print (np.concatenate((lst1,lst2),axis=0))#数组追加
    print (np.hstack((lst1, lst2)))#后面追加
    print (np.vstack((lst1, lst2)))  # 上下追加
    print ("split:")
    print (np.split(lst1,2))#split
    print (np.split(lst1,4))

    #liner
    from numpy.linalg import *
    print (np.eye(3))#单位矩阵
    lst=np.array([ [1.,2.],
                   [3.,4.]
                ])
    print ("Inv:")
    print (inv(lst))#逆矩阵
    print ("T")
    print (lst.transpose())#转置
    print ("Det:")
    print (det(lst))#行列式
    print (eig(lst))#特征值特征向量
    y=np.array([[5.],[7.]])
    print (solve(lst,y))#方程组的解

    #others
    print ("FFT:")
    print (np.fft.fft(np.array([1,1,1,1,1,1,1,1])))#FFT变换
    print ("Coef:")
    print (np.corrcoef([1,0,1],[0,2,1]))#相关系数
    print "Poly:"
    print (np.poly1d([2,1,3]))#生成多项式



if __name__ == "__main__":
    startMain()

Result:

E:\Anaconda2\python.exe D:/pycharmworkshop/numpyTest.py
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
(2L, 3L)
2
int32
4
6
<type 'list'>
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
Rand:
[[ 0.29120543  0.23287087  0.01710566  0.29841033]
 [ 0.2639998   0.71691793  0.49554608  0.84649052]]
0.951563007788
RandInt:
4
[2 9 3]
Randn:
0.425109934427
[[ 0.01719362  1.71394845 -0.37994369  0.12364691]
 [-2.16321353  1.19117302  0.62280214  0.53385122]]
Choice:
10
Distribute:
[ 0.10430826  0.10317682  0.06692362  0.02690016  0.05924724  0.01688703
  0.00121706  0.01370391  0.2552863   0.1357789   0.05804568  0.05918706
  0.00552508  0.15937229  0.01715294  0.02107544  0.02332672  0.17029058
  0.31730752  0.00724205  0.08062584  0.00681409  0.07316743  0.07862907
  0.12211717  0.186547    0.11074709  0.07792509  0.22742639  0.1035629
  0.18588081  0.04973562  0.21971975  0.00422768  0.22003966  0.00845531
  0.17478788  0.21513653  0.02819352  0.09461889  0.08298073  0.03592244
  0.03169757  0.08908428  0.00833374  0.33950697  0.06305519  0.15569642
  0.04105301  0.07164425  0.0965719   0.01079228  0.02921948  0.02514234
  0.00196252  0.17492045  0.01544928  0.00236851  0.16708493  0.05500542
  0.12625409  0.09702732  0.00261213  0.04297447  0.04211102  0.21528336
  0.13172788  0.06001411  0.18829769  0.07654145  0.09779534  0.07818139
  0.10957997  0.23450815  0.01948875  0.31369635  0.06184116  0.010487
  0.04140466  0.1127759   0.04390082  0.24761359  0.01846446  0.22555086
  0.00749743  0.22379724  0.05105565  0.10603474  0.34819694  0.06192596
  0.03748202  0.0693772   0.00107837  0.0330836   0.04818977  0.01118469
  0.03094223  0.25454664  0.01381132  0.07588954]
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
Exp:
[[  2.71828183e+00   7.38905610e+00   2.00855369e+01   5.45981500e+01
    1.48413159e+02]
 [  4.03428793e+02   1.09663316e+03   2.98095799e+03   8.10308393e+03
    2.20264658e+04]]
Exp2:
[[    2.     4.     8.    16.    32.]
 [   64.   128.   256.   512.  1024.]]
Sqrt:
[[ 1.          1.41421356  1.73205081  2.          2.23606798]
 [ 2.44948974  2.64575131  2.82842712  3.          3.16227766]]
Sin:
[[ 0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427]
 [-0.2794155   0.6569866   0.98935825  0.41211849 -0.54402111]]
Log:
[[ 0.          0.69314718  1.09861229  1.38629436  1.60943791]
 [ 1.79175947  1.94591015  2.07944154  2.19722458  2.30258509]]
basic operation
sum:
252
[[22 25 28 31]
 [32 35 38 41]]
[[ 5  7  9 11]
 [17 19 21 23]
 [32 34 36 38]]
[[10 22]
 [34 46]
 [62 78]]
max:
[[14 15 16 17]
 [18 19 20 21]]
[[ 4  5  6  7]
 [10 11 12 13]
 [18 19 20 21]]
[[ 4  7]
 [10 13]
 [17 21]]
min:
[[1 2 3 4]
 [4 5 6 7]]
[[ 1  2  3  4]
 [ 7  8  9 10]
 [14 15 16 17]]
[[ 1  4]
 [ 7 10]
 [14 18]]
+
[14 23 32 41]
-
[ 6 17 28 39]
Mul:
[40 60 60 40]
Div
[ 2  6 15 40]
Square
[ 100  400  900 1600]
Dot
[[ 80  50]
 [200 130]]
Concatenate:
[10 20 30 40  4  3  2  1]
[10 20 30 40  4  3  2  1]
[[10 20 30 40]
 [ 4  3  2  1]]
split:
[array([10, 20]), array([30, 40])]
[array([10]), array([20]), array([30]), array([40])]
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
Inv:
[[-2.   1. ]
 [ 1.5 -0.5]]
T
[[ 1.  3.]
 [ 2.  4.]]
Det:
-2.0
(array([-0.37228132,  5.37228132]), array([[-0.82456484, -0.41597356],
       [ 0.56576746, -0.90937671]]))
[[-3.]
 [ 4.]]
FFT:
[ 8.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]
Coef:
[[ 1.        -0.8660254]
 [-0.8660254  1.       ]]
Poly:
   2
2 x + 1 x + 3

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值