Numpy的简单运算

Numpy的简单输出:

import numpy as np

a=np.array([10,20,30,40])
b=np.arange(4)


print(a,b)
c=b**2
c=10*np.tan(a)
print(c) 
print(b<3)
print(b==3)

输出结果:

[10 20 30 40] [0 1 2 3]
[  6.48360827  22.37160944 -64.05331197 -11.17214931]
[ True  True  True False]
[False False False  True]

矩阵的运算:

import numpy as np
a=np.array([[1,1],
            [0,1]])
b=np.arange(4).reshape((2,2))    转换格式
c=a*b                            
c_dot=np.dot(a,b)
c_dot_2=a.dot(b)
print(c)
print(c_dot)
print(c_dot_2)

输出结果:

[[0 1]
 [0 3]]
[[2 4]
 [2 3]]
[[2 4]
 [2 3]]

找出矩阵的最大值,最小值等运用axis,axis=0行,axis=1列

import numpy as np
a=np.random.random((2,4))
print(a)
print(np.sum(a,axis=1))
print(np.max(a,axis=0))
print(np.min(a,axis=1))

输出结果:
[[0.07159696 0.51126462 0.27345316 0.51423297]
 [0.05284192 0.9367308  0.50835692 0.52553459]]
[1.37054772 2.02346423]
[0.07159696 0.9367308  0.50835692 0.52553459]
[0.07159696 0.05284192]

矩阵的各种操作:

are-索引 mean-平均值 cumsum-第几个位置一直加到第几个数 diff-当前和下一个相减 nonzero-寻找不为0的值 sort-排序 transpose-矩阵的转置

import numpy as np
A=np.arange(14,2,-1).reshape((3,4))
np.argmin(A)
print(A)
print(np.argmin(A))
print(np.argmax(A))
print(A.mean())
print(np.cumsum(A))
print(np.diff(A))
print(np.nonzero(A))
print(np.sort(A))
print(np.transpose(A))
print(np.clip(A,5,9))
print(np.mean(A,axis=0))

输出结果:

[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
11
0
8.5
[ 14  27  39  50  60  69  77  84  90  95  99 102]
[[-1 -1 -1]
 [-1 -1 -1]
 [-1 -1 -1]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]
[10.  9.  8.  7.]

今天我们介绍两个科学运算当中最为重要的两个模块,一个是 numpy,一个是 pandas。任何关于数据分析的模块都少不了它们两个。

This is one of the important modules in opencv. In other words, opencv must via it to act. So it is essential to have excellent control of it. I have grasped the basic operation of it, then I will learn more about it.

上一篇:机器学习-科学数据库day3


下一篇:极客教程 支持向量机