# Working with NumPy

## Importing NumPy

`import numpy as np`

## Converting Arrays to NumPy Arrays

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

## NumPy Arange Function

`print(np.arange(0,10)) # without distance parameterOUTPUT:[0 1 2 3 4 5 6 7 8 9]print(np.arange(0,10,2)) # with distance parameterOUTPUT: [0 2 4 6 8]`

## Zeroes and Ones

`print(np.zeros(3))OUTPUT: [0. 0. 0.]print(np.ones(3))OUTPUT: [1. 1. 1.]`
`print(np.zeros((4,5)))OUTPUT:[ [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]]print(np.ones((4,5)))OUTPUT:[ [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]`

## Identity Matrix

`np.eye(5)OUTPUT:[[1., 0., 0., 0., 0.][0., 1., 0., 0., 0.][0., 0., 1., 0., 0.][0., 0., 0., 1., 0.][0., 0., 0., 0., 1.]]`

## NumPy Linspace Function

`print(np.linspace(0,10,3))OUTPUT:[ 0.  5. 10.]`
`print(np.linspace(0,10,20))OUTPUT:[ 0. 0.52631579  1.05263158  1.57894737  2.10526316  2.63157895   3.15789474  3.68421053  4.21052632  4.73684211  5.26315789  5.78947368   6.31578947  6.84210526  7.36842105  7.89473684  8.42105263  8.94736842   9.47368421 10.]`

## Random Number Generation

`print(np.random.rand(10)) # arrayOUTPUT: [0.46015141 0.89326339 0.22589334 0.29874476 0.5664353  0.39257603  0.77672998 0.35768031 0.95087408 0.34418542]print(np.random.rand(3,4)) # 3x4 matrixOUTPUT:[[0.63775985 0.91746663 0.41667645 0.28272243]  [0.14919547 0.72895922 0.87147748 0.94037953]  [0.5545835  0.30870297 0.49341904 0.27852723]]`
`print(np.random.randn(10))OUTPUT:[-1.02087155 -0.75207769 -0.22696798  0.86739858  0.07367362 -0.41932541   0.86303979  0.13739312  0.13214285  1.23089936]print(np.random.randn(3,4))OUTPUT: [[ 1.61013773  1.37400445  0.55494053  0.23133522]  [ 0.31290971 -0.30866402  0.33093618  0.34868954]  [-0.11659865 -1.22311073  0.36676476  0.40819545]]`
`print(np.random.randint(1,100,10))OUTPUT:[64 37 62 27  4 33 23 52 70  7]print(np.random.randint(1,100,(2,3)))OUTPUT:[[92 42 38]  [87 69 38]]`
`np.random.seed(42)print(np.random.rand(4))OUTPUT:[0.37454012, 0.95071431, 0.73199394, 0.59865848]`

## Reshaping Arrays

`arr = np.random.rand(2,2)print(arr)print(arr.shape)OUTPUT:[[0.19890857 0.00806693][0.48199837 0.55373954]](2, 2)`
`print(arr.reshape(1,4))OUTPUT: [[0.19890857 0.00806693 0.48199837 0.55373954]]print(arr.reshape(4,1))OUTPUT:[[0.19890857][0.00806693][0.48199837][0.55373954]]`

## Slicing Data

`myarr = np.arange(0,11)print(myarr)OUTPUT:[ 0  1  2  3  4  5  6  7  8  9 10]sliced = myarr[0:5]print(sliced)OUTPUT: [0 1 2 3 4]sliced[:] = 99print(sliced)OUTPUT: [99 99 99 99 99]print(myarr)OUTPUT:[99 99 99 99 99  5  6  7  8  9 10]`
`sliced = myarr.copy()[0:5]`
`my_matrix = np.random.randint(1,30,(3,3))print(my_matrix)OUTPUT: [[21  1 20][22 16 27][24 14 22]]print(my_matrix) # print a single rowOUTPUT: [21  1 20]print(my_matrix) # print a single value or row 0, column 0OUTPUT: 21print(my_matrix[0,0]) #alternate way to print value from row0,col0OUTPUT: 21`

## Array Computations

`new_arr = np.arange(1,11)print(new_arr)OUTPUT: [ 1  2  3  4  5  6  7  8  9 10]`
`print(new_arr + 5)OUTPUT: [ 6  7  8  9 10 11 12 13 14 15]`
`print(new_arr - 5)OUTPUT: [-4 -3 -2 -1  0  1  2  3  4  5]`
`print(new_arr + new_arr)OUTPUT: [ 2  4  6  8 10 12 14 16 18 20]`
`print(new_arr / new_arr)OUTPUT:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]`
`arr2d = np.arange(25).reshape(5,5)print(arr2d)OUTPUT: [[ 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]]print(arr2d.sum())OUTPUT: 300print(arr2d.sum(axis=0))  # sum of columnsOUTPUT: [50 55 60 65 70]print(arr2d.sum(axis=1)) #sum of rowsOUTPUT: [ 10  35  60  85 110]`

## Conditional Operations

`arr = np.arange(0,10)OUTPUT: [0,2,3,4,5,6,7,8,9]print(arr > 4)OUTPUT: [False False False False False  True  True  True  True  True]print(arr[arr > 4])OUTPUT: [5 6 7 8 9]`

# Summary

Written by

Written by

## Manish Shivanandhan

#### Product Manager with a strong tech background and a flair for Marketing. Guest writer for FreeCodeCamp and The Startup. Learn more at www.manishmshiva.com 