Numpy Crash Course — Building Powerful n-Dimensional Arrays with NumPy

Numpy is a python library for performing large scale numerical computations. It is extremely useful, especially in machine learning. Let's look at what Numpy has to offer.

Manish Shivanandhan
Sep 22 · 7 min read
Image for post
Image for post

Introduction

Installation

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 parameter
OUTPUT:[0 1 2 3 4 5 6 7 8 9]
print(np.arange(0,10,2)) # with distance parameter
OUTPUT: [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

Image for post
Image for post
Normal and Uniform Distribution
print(np.random.rand(10)) # array
OUTPUT: [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 matrix
OUTPUT:[[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[:] = 99
print(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[0]) # print a single row
OUTPUT: [21 1 20]
print(my_matrix[0][0]) # print a single value or row 0, column 0
OUTPUT: 21
print(my_matrix[0,0]) #alternate way to print value from row0,col0
OUTPUT: 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: 300
print(arr2d.sum(axis=0)) # sum of columns
OUTPUT: [50 55 60 65 70]
print(arr2d.sum(axis=1)) #sum of rows
OUTPUT: [ 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

Manish Shivanandhan’s Blog

Making tech easier for people, one article at a time.

Manish Shivanandhan

Written by

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

Manish Shivanandhan’s Blog

Making tech easier for people, one article at a time.

Manish Shivanandhan

Written by

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

Manish Shivanandhan’s Blog

Making tech easier for people, one article at a time.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store