# Manipulation des Tableaux

NumPy supports the creation and manipulation of multidimensional arrays. NumPy arrays are fast, easy to understand, they occupy little memory and its indexing of arrays is very fast. NumPy also facilitates calculations on its arrays and many other things... it is an essential library to master for a data scientist.

we will walk through some array manipulation features through the following methods:

- Concatenate

-hstack

- vstack

- Reshape

- Squeeze

-Ravel

First, let's start by importing our library:

` import numpy as np`

then let's create tables, it is done this way

`B = np.ones((4,3)) `

np.ones((4,3)) means that we want to create an array containing 1s of dimension passed as parameters(4,3)

display:

`print(B)`

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

let's use shape to display the dimension of each die.

`print(B.shape)`

`(4, 3)`

`A = np.zeros((4,3)) `

np.zeros((4,3)) means that we want to create an array containing 0s of dimension passed as parameters(4,3)

display:

`print(A)`

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

let's use shape to display the dimension of each die.

`print(A.shape)`

`(4, 3)`

it's already done, we start with the Concatenate() function

concatenate takes input two arrays and returns you an array

`e = np.concatenate((A,B) , axis = 0)`

display:

`print(e)`

```
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
```

`e.shape`

`(8, 3)`

so if you want to assemble two arrays it is ideal, it also lets you choose the assembly in the axis you want example axis = 0 to assemble along the vertical and axis = 1 to assemble along the horizontal donations if you have an array of dimension 3 or 4 you can make a concatenation according to your chosen axis.

`e = np.concatenate((A,B) , axis = 1)`

display:

`print(e)`

```
[[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]]
```

`print(e.shape)`

`(4, 6)`

you have seen how concatenate works now let's move on to the

hstack() and vstack() functions they are very useful for data analysis

hstack (): allows the assembly of two tables along the horizontal axis, yes h for horizontal

example:

` c = np.hstack((A,B))`

display:

`print(c)`

```
[[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]]
```

vtack (): allows assembly along the vertical axis, v for vertical

example:

`d = np.vstack((A,B))`

display:

`print(d)`

```
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
```

reshape

it allows to remanipulate the form of an array to give it a new form, extremely important, be careful however this method only works if the number of elements present in the initial form is equal to the number of elements in the final form

example:

`print(e.shape)`

`(4, 6)`

let's check the number of elements present:

`print(f.size)`

`24`

apply :

resize f of dimension (4, 6)to a new one of dimension (6,4)

`ff = f.reshape( 6,4)`

`print(ff.shape)`

`(6, 4)`

Squeeze

it has the effect of making disappear the dimensions in which we have only 1

example :

` d = np.ones((3,1))`

take a matrix of dimension (3,1) apply this function on it we obtain as result

`d.squeeze()`

we have :

`array([1., 1., 1.])`

`d.squeeze().shape`

`(3,)`

Ravel

it flattens an array into a single dimension

example:

`print(f)`

display:

```
[[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]]
```

`f.ravel()`

we obtain:

```
array([0., 0., 0., 1., 1., 1., 0., 0., 0., 1., 1., 1., 0., 0., 0., 1., 1.,
1., 0., 0., 0., 1., 1., 1.])
```

here you have the notebook