# Numpy Foundation (novice come quickly!)

NumPy is a scientific computing library that provides python with high-performance vectors, matrices and high-dimensional data structures. It is realized by C and Fortran, so it has a very good performance to establish equations by vector and matrix and realize numerical calculation. NumPy is basically the foundation of all frameworks and packages that use Python for numerical calculation, such as TensorFlow and PyTorch. The most basic content of building machine learning model is to learn to use NumPy to build calculation process.

### split array

hsplit can be used to split an array along the horizontal axis. We can specify the number of arrays to be output after splitting, or specify which column to split the array in:

```>>> a = np.floor(10*np.random.random((2,12)))
>>> a
array([[ 9.,  5.,  6.,  3.,  6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],
[ 1.,  4.,  9.,  2.,  2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])
>>> np.hsplit(a,3)   # Split a into 3
[array([[ 9.,  5.,  6.,  3.],
[ 1.,  4.,  9.,  2.]]), array([[ 6.,  8.,  0.,  7.],
[ 2.,  1.,  0.,  6.]]), array([[ 9.,  7.,  2.,  7.],
[ 2.,  2.,  4.,  0.]])]
>>> np.hsplit(a,(3,4))   # Split a after the third and the fourth column
[array([[ 9.,  5.,  6.],
[ 1.,  4.,  9.]]), array([[ 3.],
[ 2.]]), array([[ 6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],
[ 2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])]```

vsplit split along vertical axis, array_split specifies which axis to split along.

## Replication and views

When doing array operations, it is often difficult for the entrant to determine whether the data is copied to a new array or modified directly on the original data. This has a great impact on further operations, so sometimes we need to copy content to the new variable memory, rather than just point the new variable to the original memory. At present, there are generally three replication methods, that is, do not copy memory, shallow copy and deep copy.

### Do not actually copy

Simple tasks do not copy array targets or their data. As follows, first assign variable a to b, and then modify variable b to modify variable a at the same time. This general assignment method will make variables associated.

```>>> a = np.arange(12)
>>> b = a            # no new object is created
>>> b is a           # a and b are two names for the same ndarray object
True
>>> b.shape = 3,4    # changes the shape of a
>>> a.shape
(3, 4)```

Python passes indefinite objects as references, so the calling function does not change the target ID, nor does it copy the actual content.

```>>> def f(x):
...     print(id(x))
...
>>> id(a)                           # id is a unique identifier of an object
148293216
>>> f(a)
148293216```

### View or light copy

Different array objects can share the same data. The view method can create a new array object to view the same data. The target identifiers of c and a below are inconsistent, and changing the shape of one variable does not change the other. But these two arrays share all elements, so changing one element of an array will also change the corresponding element of another array.

```>>> c = a.view()
>>> c is a
False
>>> c.base is a                        # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6                      # a s shape doesn t change
>>> a.shape
(3, 4)
>>> c[0,4] = 1234                      # a s data changes
>>> a
array([[   0,    1,    2,    3],
[1234,    5,    6,    7],
[   8,    9,   10,   11]])```

The output of splitting array is a view. A s follows, splitting array a into sub array s, then s is a view of A. modifying the element in s will also modify the corresponding element in a.

```>>> s = a[ : , 1:3]     # spaces added for clarity; could also be written "s = a[:,1:3]"
>>> s[:] = 10           # s[:] is a view of s. Note the difference between s=10 and s[:]=10
>>> a
array([[   0,   10,   10,    3],
[1234,   10,   10,    7],
[   8,   10,   10,   11]])```

### Deep replication

The copy method can copy the array and data completely, which makes the two variables have different array targets and the data is not shared.

```>>> d = a.copy()                          # a new array object with new data is created
>>> d is a
False
>>> d.base is a                           # d doesn t share anything with a
False
>>> d[0,0] = 9999
>>> a
array([[   0,   10,   10,    3],
[1234,   10,   10,    7],
[   8,   10,   10,   11]])```

Tags: Python

Posted on Thu, 04 Jun 2020 07:43:47 -0700 by wildmanmatt