Hey welcome, let's start with numpy

Numpy is one of the most important libraries for Data Science projects in Python.

If you're learning python next thing you will need is to learn is Numpy.

there are other important libraries that are built on top of NumPy like pandas SciKit-Learn, so basically Numpy makes execution easy, fast, and uses less memory.

Below are the most important Numpy methods which will be enough to get started.

👇🏻

Numpy (Numeric Python)

a basic Python package that provides an alternative to a regular Python list, a Numpy n-dimensional homogeneous array.

Installing Numpy

It's one of the easy peasy tasks...using cmd

pip install numpy

it will start installing the Numpy library...if you want to install using Jupiter notebook

!pip install numpy

Just add "!" It will do the rest.

All set, let's use NumPy now.

importing numpy as np (by convention)

import numpy as np

importing numpy as np is the alias used to write code which is useful in a way we don't need to type the whole word 'numpy' just 'np' will do the work.

Hold on, But why use numpy when you already have lists in Python?

The short answer: Speed, numpy lists use lesser memory and are much faster to run than vanilla Python lists.

Let's see how we can use Numpy.

np.array() : It can be used to define any kind of array of any dimension and is the most common method. dtype argument can be added while defining the array for the output to be of our preferred data type.

import numpy as np
# Array with elements of floating integers
a = np.array([1,2,3,4,5,6,7,8], dtype = float)
print(a)

np.zeros() : Using np.zeros() would produce an array of zeroes only. You just need to define the dimension of the zero array.

import numpy as np
b = np.zeros(5)
print(b)

np.ones() : Using np.ones() would produce an array of one’s only. Again, you just need to define the dimension of the array.

import numpy as np
c = np.ones(5)
print(c)

while '5' is the parameter that gives 5 column matrix

np.arange() : When you want an array with values at a regular interval within a given range, you can use np.arange() to achieve so. It takes in three values, the first two for the range, and the third for the skip value. Note that the second value of the range is non-inclusive.

import numpy as np
#with 2 parameters It prints all the values in given range
d = np.arange(4,12)
print(d)

#with third parameter it prints values in steps
e = np.arange(4,12,3)
print(e)

some more methods to try:

1 np.size : It returns the number of elements in the array, no matter its dimension.

import numpy as np                         

a = np.arange(12)
print(a)
print(a.size)

2 np.shape : It returns the number of rows and the columns of the array in the form of (row, columns).

a.shape

3 np.reshape() : It lets you change the dimension of the given array to the dimension of your choice.

print(a.reshape(3,4))

4 np.resize() : It is the same as a shape in its operation, the only difference being that using resize() would change the original array.

print(a.resize(3,4))
# #it would print None, but the change has taken place on the original array

easy way to convert lists into np arrays:

import numpy as np
a = [1,2,3,4,5,6]
b = np.array(a)
# python list is converted to numpy array.

identity matrix

import numpy as np
f = np.eyes(5)
print(f)

Linspace

List with evenly spaced elements in between, the 3 here tells us that we need a list with 3 elements from 0-10, evenly spaced (both included in this case).

import numpy as np
#array with evenly spaced elements
g = np.linespace(0,10,3)

list operations

import numpy as np

a = np.arrange(1,3)
#[0,1,2]

a + 5 
# [5,6,7]

a + a
#[0,2,4]

list operations

import numpy as np
a = np.arrange(1,3)
# [0,1,2]

a.sum()
#sum of all elements

a.sum(axis=0)
#sum of all columns

a.sum(axis=1)
# sum of all rows

Numpy statistics

import numpy as np
arr1 = np.arange(1,10)
arr1.reshape(3,3)

Mean

import numpy as np
np.arr1.mean()

#OR

np.mean(np_arr)

Median

import numpy as np
np.median(arr1)

Variance

import numpy as np
np.var(arr1)

Standard Deviation

import numpy as np
np.std(np_arr)

There are many other methods like sum(), sort(), corcoeff(), etc., that comes in handy while doing an in-depth data analysis.

Alright, now you know a bunch of tools which are the foundations to numpy, this is it for now.

I hope you had fun reading this article, and you found everything easy to understand. If you need further information on any topic, let me know in the response.

Thanks for reading small introductory stuff on numpy, if you are interested in more stuff you will be added to the mailing list once you subscribe to the newsletters..

happy coding!

Did you find this article valuable?

Support Shreyas Kulkarni by becoming a sponsor. Any amount is appreciated!