numpy

## numpys属性

* ndim:维度

* shape:行数和列数

* size:元素个数

```python

import numpy as np #导入numpy

array = np.array([[1,2,3],[4,5,6]]) #创建数组

print(array)

```

[[1 2 3]

[4 5 6]]

```python

print('number of dim:',array.ndim) # 维度

print('shape :',array.shape) # 行数和列数

print('size:',array.size) # 元素个数

```

number of dim: 2

shape : (2, 3)

size: 6

## 指定数据 dtype

```python

a = np.array([2,23,4],dtype=np.int)

print(a.dtype)

a = np.array([2,23,4],dtype=np.int32)

print(a.dtype)

a = np.array([2,23,4],dtype=np.float)

print(a.dtype)

a = np.array([2,23,4],dtype=np.float32)

print(a.dtype)

```

int32

int32

float64

float32

## 创建特定数据

```python

a = np.array([[2,23,4],[2,32,4]]) # 2d 矩阵 2行3列

print(a)

```

[[ 2 23 4]

[ 2 32 4]]

### 创建全零数组

```python

a = np.zeros((3,4)) # 数据全为0,3行4列

print(a)

```

[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

### 创建全一数组, 同时也能指定这些特定数据的 dtype:

```python

a = np.ones((3,4),dtype = np.int) # 数据为1,3行4列

print(a)

```

[[1 1 1 1]

[1 1 1 1]

[1 1 1 1]]

### 创建全空数组, 其实每个值都是接近于零的数:

```python

a = np.empty((3,4)) # 数据为empty,3行4列

print(a)

```

[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

### 用 arange 创建连续数组:

```python

a = np.arange(10,20,2) # 10-19 的数据,2步长

print(a)

```

[10 12 14 16 18]

### 使用 reshape 改变数据的形状:

```python

a = np.arange(12).reshape((3,4)) # 3行4列,0到11

print(a)

```

[[ 0 1 2 3]

[ 4 5 6 7]

[ 8 9 10 11]]

### 用 linspace 创建线段型数据:

```python

a = np.linspace(1,10,20) # 开始端1,结束端10,且分割成20个数据,生成线段

print(a)

```

[ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105

3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632

6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158

9.52631579 10. ]

### 同样也能进行 reshape 工作:

```python

a = np.linspace(1,10,20).reshape((5,4)) # 更改shape

print(a)

```

[[ 1. 1.47368421 1.94736842 2.42105263]

[ 2.89473684 3.36842105 3.84210526 4.31578947]

[ 4.78947368 5.26315789 5.73684211 6.21052632]

[ 6.68421053 7.15789474 7.63157895 8.10526316]

[ 8.57894737 9.05263158 9.52631579 10. ]]

## 随机数填充数组

```

x = np.random.random((3,3))

print(x)

```

[[0.14081761 0.84054832 0.81950547]

[0.64149529 0.36935188 0.79136848]

[0.67530063 0.60766317 0.45713333]]

## numpy 的几种基本运算

```python

import numpy as np

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

b=np.arange(4)

print(a)

print(b)

```

[10 20 30 40]

[0 1 2 3]

### 求两个矩阵之间的减法

```python

c=a-b

print(c)

```

[10 19 28 37]

### 求两个矩阵之间的加法

```python

c=a+b

print(c)

```

[10 21 32 43]

### 求两个矩阵之间的乘法

```python

c=a*b

print(c)

```

[ 0 20 60 120]

### 求两个矩阵之间的乘方

```python

c=b**2

print(c)

```

[0 1 4 9]

### 进行函数运算(以sin函数为例)

```python

c=10*np.sin(a)

print(c)

```

[-5.44021111 9.12945251 -9.88031624 7.4511316 ]

### 对print函数进行一些修改可以进行逻辑判断:

```python

print(b<3)

```

[ True True True False]

### 多行多维度的矩阵计算

```python

a=np.array([[1,1],[0,1]])

b=np.arange(4).reshape((2,2))

print(a)

print(b)

```

[[1 1]

[0 1]]

[[0 1]

[2 3]]

### 矩阵积(对应行乘对应列得到相应元素):

```python

c_dot = np.dot(a,b)

print(c_dot)

```

[[2 4]

[2 3]]

```python

c_dot_2 = a.dot(b)

print(c_dot_2)

```

[[2 4]

[2 3]]

### 自增运算符(+=,-=)

- 运算的道德结果不是赋值给一个新数组而是赋给参与运算的数组本身

### sum(), min(), max()的使用:

```python

import numpy as np

a=np.random.random((2,4))

print(a)

```

[[0.35988859 0.90074043 0.84605958 0.6178183 ]

[0.1524533 0.26544464 0.78397802 0.64831684]]

```python

np.sum(a)

```

4.574699696130096

```python

np.min(a)

```

0.15245330073817986

```python

np.max(a)

```

0.9007404268486401

### 对行或者列进行查找运算:

```python

print("a =",a)

print("sum =",np.sum(a,axis=1))

print("min =",np.min(a,axis=0))

print("max =",np.max(a,axis=1))

```

a = [[0.35988859 0.90074043 0.84605958 0.6178183 ]

[0.1524533 0.26544464 0.78397802 0.64831684]]

sum = [2.7245069 1.8501928]

min = [0.1524533 0.26544464 0.78397802 0.6178183 ]

max = [0.90074043 0.78397802]

posted @ 2019-04-21 12:26  乄一叶知秋  阅读(165)  评论(0编辑  收藏  举报