## Creating Arrays using NumPy

NumPy is used to work with arrays. The array object in NumPy is called `ndarray`

.

We can create a NumPy `ndarray`

object by using the `array()`

function

arr = np.array([1, 2, 3, 4, 5])

An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by using the `ndmin`

argument.

arr = np.array([1, 2, 3, 4], ndmin=5)

## NumPy Arrays Indexing

Array indexing is the same as accessing an array element.

You can access an array element by referring to its index number.

The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

arr = np.array([1, 2, 3, 4])

print(arr[0])

print(arr[3])

Accessing 2-D array

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print(‘3th element on 2nd dim: ‘, arr[1, 2])

## NumPy Arrays Slicing

Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: `[`

.*start*:*end*]

We can also define the step, like this: `[`

.*start*:*end*:*step*]

If we don’t pass start its considered 0

If we don’t pass end its considered length of array in that dimension

If we don’t pass step its considered 1

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])

*Negative Slicing*

Use the minus operator to refer to an index from the end:

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])

*Step*

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5:2])

**Sorting and Reshaping**

`array.sort()`

can be used to sort your NumPy array — you can pass different arguments inside the brackets to define what you want to sort on (by using the argument ‘order=string/list of strings’, for example. See more examples in the documentation). `array.sort(axis=0)`

will sort specific axis of the array — rows or columns. `two_d_arr.flatten()`

will flatten a 2 dimensional array to a 1 dimensional array. `array.T`

will transpose an array — meaning columns will become rows and vice versa. `array.reshape(x,y)`

would reshape your array to the size you set with x and y. `array.resize((x,y))`

will change the array shape to x and y and fill new values with zeros.

## Combining and Splitting

You can use `np.concatenate((array1,array2),axis=0)`

to combine two NumPy arrays — this will add array 2 as rows to the end of array 1 while `np.concatenate((array1,array2),axis=1)`

will add array 2 as columns to the end of array 1. `np.split(array,2)`

will spilt the array into two sub-arrays and `np.hsplit(array,5)`

will split the array horizontally on the *5th* index.

## Adding and Removing Elements

There are, of course, commands to add and remove elements from NumPy arrays:

`np.append(array,values)`

will append values to end of array.`np.insert(array, 3, values)`

will insert values into array before index 3`np.delete(array, 4, axis=0)`

will delete row on index 4 of array`np.delete(array, 5, axis=1)`

will delete column on index 5 of array

## Descriptive Statistics

You can use NumPy methods to get descriptive statistics on NumPy arrays:

`np.mean(array,axis=0)`

will return mean along specific axis (0 or 1)`array.sum()`

will return the sum of the array`array.min()`

will return the minimum value of the array`array.max(axis=0)`

will return the maximum value of specific axis`np.var(array)`

will return the variance of the array`np.std(array,axis=1)`

will return the standard deviation of specific axis`array.corrcoef()`

will return the correlation coefficient of the array`numpy.median(array)`

will return the median of the array elements

## Doing Math with NumPy

Any tutorial to NumPy would not be complete without the numerical and mathematical operations you can do with NumPy! Let’s go over them:

`np.add(array ,1)`

will add 1 to each element in the array and `np.add(array1,array2)`

will add array 2 to array 1. The same is true to `np.subtract(), np.multiply(), np.divide() and np.power()`

— all these commands would work in exactly the same way as described above.

You can also get NumPy to return different values from the array, like:

`np.sqrt(array)`

will return the square root of each element in the array`np.sin(array)`

will return the sine of each element in the array`np.log(array)`

will return the natural log of each element in the array`np.abs(arr)`

will return the absolute value of each element in the array`np.array_equal(arr1,arr2)`

will return`True`

if the arrays have the same elements and shape

It is possible to round different values in array: `np.ceil(array)`

will round up to the nearest integer, `np.floor(array)`

will round down to the nearest integer and `np.round(array)`

will round to the nearest integer.