## Numpy Basic, Reshaping and Slicing

If you are a python user and work as data scientist, you definitely use Numpy. But if you are aspiring to be a data scientist, you need to learn Numpy. This is a well established numerical computational tool that is available for free. Learning should be easy because Numpy has well- structured documentation and also communities are very active in stack overflow.

Now let’s see why Numpy is so popular. You may think python list should be enough. All the operations that are available in Numpy, are possible with python list as well. But still people use Numpy because it handles data more efficiently as it works faster and uses much less memory space. Your code will be lot more concise because you won’t have to write the functions. Instead just import Numpy and use their functions.

Here are some basic Numpy operations. First an array was created using a range of numbers. Then the array elements are summed up using regular python sum function and then using Numpy. I used ‘%timeit’ to show the time difference between two ways.

Then two arrays are created. You can see adding, multiplying, dividing and even making exponential operations in Numpy. ‘Ndim’ method gives the dimension and ‘shape’ method provides you with the shape of the arrays. Remember, shape is always a tuple. First element of the tuple is the number of rows and second element is the number of columns. Also ‘size’ method gives the number of elements in the arrays. The next portion is very important. It shows slicing of the arrays. There are so many different ways to slice and extract the data from arrays which is very significant in data analysis.

```import numpy as np

#create an array
g = list(range(1000000))```
In :
`%timeit sum(g)`
```32.6 ms ± 1.85 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
```
In :
```#make a numpy array out of g
g_array = np.array(g)
```
In :
`%timeit np.sum(g_array)`
```496 µs ± 11 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
```
In :
```#Now create two arrays a and b

a = np.array([1,2,3,4])
b = np.array([10,11,12,13])

a+b
```
Out:
`array([11, 13, 15, 17])`
In :
```a * b
```
Out:
`array([10, 22, 36, 52])`
In :
```a / b
```
Out:
`array([0.1       , 0.18181818, 0.25      , 0.30769231])`
In :
```a ** b
```
Out:
`array([       1,     2048,   531441, 67108864], dtype=int32)`
In :
```a
```
Out:
`array([1, 2, 3, 4])`
In :
```a.ndim #get the dimension
```
Out:
`1`
In :
```a.shape #returns a tuple that says the number of elements in each dimension
```
Out:
`(4,)`
In :
```a * 10
```
Out:
`array([10, 20, 30, 40])`
In :
```np.sin(a)
```
Out:
`array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])`
In :
```np.exp(a)
```
Out:
`array([ 2.71828183,  7.3890561 , 20.08553692, 54.59815003])`
In :
```np.log(a)
```
Out:
`array([0.        , 0.69314718, 1.09861229, 1.38629436])`
In :
```a.dtype
```
Out:
`dtype('int32')`
In :
```a = np.array([1,2,3,4.0])
a.dtype
```
Out:
`dtype('float64')`
In :
```a = np.array([1,2,3,4.0+1j])
a.dtype
```
Out:
`dtype('complex128')`
In :
```c = np.array([[10,11,12], [20,21,22]])
c
```
Out:
```array([[10, 11, 12],
[20, 21, 22]])```
In :
```c.ndim
```
Out:
`2`
In :
```c.shape
```
Out:
`(2, 3)`
In :
```c.size
```
Out:
`6`
In :
```c.T
```
Out:
```array([[10, 20],
[11, 21],
[12, 22]])```
In :
```c.nbytes
```
Out:
`24`
In :
```c[0,0] #highly recommended in numpy
```
Out:
`10`
In :
```c
```
Out:
`10`
In :
```b[-2:] #selects the last two elements of the array
```
Out:
`array([12, 13])`
In :
```a = np.arange(25).reshape(5,5)
```
In :
```a
```
Out:
```array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])```
In :
```c = np.arange(40).reshape(5, 8)
c
```
Out:
```array([[ 0,  1,  2,  3,  4,  5,  6,  7],
[ 8,  9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31],
[32, 33, 34, 35, 36, 37, 38, 39]])```
In :
```c[:, 1] # Selects the second column
```
Out:
`array([ 1,  9, 17, 25, 33])`
In :
```c[:, 4] #Selects fifth column
```
Out:
`array([ 4, 12, 20, 28, 36])`
In :
```c[4, :] #Selects 5th row
```
Out:
`array([32, 33, 34, 35, 36, 37, 38, 39])`
In :
```c[1:4, 2:5]
```
Out:
```array([[10, 11, 12],
[18, 19, 20],
[26, 27, 28]])```
In :
```c[:, 1::3]
```
Out:
```array([[ 1,  4,  7],
[ 9, 12, 15],
[17, 20, 23],
[25, 28, 31],
[33, 36, 39]])```
In :
```c[:, 1:-1:3]
```
Out:
```array([[ 1,  4],
[ 9, 12],
[17, 20],
[25, 28],
[33, 36]])```
In :
```c[1::2, :-5:2]
```
Out:
```array([[ 8, 10],
[24, 26]])```