# It took so long for Numpy to really use the ndarray object

## ndarray object of Numpy Library

### 1, ndarray object of numpy

One of the most important features of numpy is its

The dimensional array object ndarray is a collection of a series of data of the same type. The index of the elements in the collection starts with the subscript 0

To create an ndarray, you only need to call Numpy's array function:

```numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
```

Parameter Description:

parameterexplain
objectArray or nested sequence
dtypeType of array element
copyWhether the object needs to be copied is optional
orderCreate an array style. C is the row direction, F is the column direction, and A is any direction
subokBy default, an array consistent with the base class type is returned
ndminSpecifies the minimum dimension of the generated array

Test:

```a = np.array([1,2,3,4])  #one-dimensional
b = np.array([[1,2],[3,4],[5,6]])  # two-dimensional
print(a)
print(b)
```

Output:

```[1 2 3 4]
[[1 2]
[3 4]
[5 6]]
```

```# Parameter ndmin
a1 = np.array([1,2,3], ndmin=1)
a2 = np.array([1,2,3], ndmin=2)
a3 = np.array([1,2,3], ndmin=3)
print(a1)
print(a2)
print(a3)
```

result:

```[1 2 3]
[[1 2 3]]
[[[1 2 3]]]
```

### 2, Numpy create array

#### 1. Call numpy Empty (shape, dtype, order) method

Note: order has two values. C and F represent row priority and column priority respectively

```numpy.empty([3,2], dtype = int)
```
```# Mode 1: numpy empty()
a4 = np.empty([3,2], dtype = int)    #[3,2] represents three rows and two columns, that is, a two-dimensional array with three elements
print(a4)
```

Result: the value of random output is not initialized

```[[       28090704               0]
[       27547584 140697935844848]
[140697954999808 140697982180144]]
```

#### 2.numpy.zeros()

The creation method is the same as empty(). The elements after creating an array of specified size are filled with 0

```# Mode 2: numpy zeros()
a5 = np.zeros([3,2], dtype = int)
print(a5)
```

result:

```[[0 0]
[0 0]
[0 0]]
```

#### 3.numpy.ones()

```# Mode 3: numpy ones()
a6 = np.ones([3,2], dtype=int)
print(a6)
```

result:

```[[1 1]
[1 1]
[1 1]]

```

#### 4.numpy.asarray(), create from the existing array

```# Mode 4: numpy Asarray(), create from the existing array
array = [1,2,3]
a7 = np.asarray(array, dtype=float)
print(a7)
```

result:

```[ 1.  2.  3.]
```

#### 5.numpy.frombuffer: used for dynamic array implementation

```# Mode 5: numpy Frombuffer: used for dynamic array implementation
# Parameter Description: count -- "number of reads per time, - 1 represents all data, and offset --" starts indexing
a8 = np.frombuffer(b"hello world", dtype='S1', count=1, offset=0)
print(a8)
```

result:

```[b'h']
```

#### 6.numpy.fromiter: create from iterator

```# Mode 6: numpy Fromiter: create from iterator
it = iter(array)
a9 = np.fromiter(it, dtype=float, count=-1)
print(a9)
```

result:

```[ 1.  2.  3.]
```

#### 7.numpy.arange()

```# Mode 7: numpy arange()
a0 = np.arange(10,20,2)
print(a0)
```

result:

```[10 12 14 16 18]
```

### 3, Numpy slice and index

Take a chestnut

```s = slice(2,7,2)  #[2 4 6]
b1 = np.arange(10)  #[0 1 2 3 4 5 6 7 8 9]
print(b1[s])  # [2 4 6]
```

b1[s]: this method is similar to that b1 is the array to be sliced, s is the index of the slice, and the value of b1 is retrieved according to the index of S

#### 1. Use of colon ':'

```# Use of:
print(b1[1:])
print(b1[1:2])
print(b1[:3])

result:
[1 2 3 4 5 6 7 8 9]

[1]

[0 1 2]
```

Colon is similar to slicing an array

Think: what if it's a two-dimensional or three-dimensional array?

#### 2. Use of colons in multidimensional arrays

```# Multidimensional array colon: use of
b2 = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(b2[1:])
print(b2[1:2])
print(b2[:2])

# result
[[4 5 6]
[7 8 9]]

[[4 5 6]]

[[1 2 3]
[4 5 6]]
```

In a multi-dimensional array, the same is true. Take out the outermost layer by slice

#### 3. Use of ellipsis

Thinking: the above use: slice can take out the outer data. What if you want to take out the internal data for a two-dimensional data

For example: b2 = [[1,2,3], [4,5,6],[7,8,9]], want to take out [8,9]

```print(b2[2][1:])

# result
[8,9]
```

What if you want to take out [2,3], [5,6], [8,9]

Use the above slice: can it be realized

```print(b2[:,1:])

#result
[[2 3]
[5 6]
[8 9]]
```

In addition, the omission of [6 7 8 9] can also be used

If you need all the rows for a row, you can use the ellipsis... Instead of ":"

```print(b2[...,1:])

#result
[[2 3]
[5 6]
[8 9]]
```

Similarly, if you want only the data in the second row, you can use ellipsis on the column

```print(b2[1,...])

# result
[4 5 6]
```

#### 4. Integer array index

```print(b[[0,1,2],[0,1,0]])

result:
[1 5 7]
```

The integer array index is to combine the indexes at the same position for segmentation. For example, for b2 = [[1,2,3], [4,5,6],[7,8,9]], it is divided according to the integer indexes [[0,1,2], [0,1,0]], that is to get the values of [[0,0], [1,1], [2,0]] of b2

#### 5. Boolean index

```# Boolean index
print(b2[b2>5])

# result
[6 7 8 9]
```

Filter out the values with B2 > 5

Keywords: Python Back-end

Added by mgmoses on Sun, 27 Feb 2022 17:27:04 +0200