Tutorial Numpy (DF Data Analysis)

Data Science, TutorialNo Comments

You Are Here:, TutorialTutorial Numpy (DF Data Analysis)

These quick tutorial are designed to quickly get started with data analysis. In this tutorial we’ll be looking at Numpy.

Numpy

It’s the fundamental package for scientific computing used to create & manipulate array with python.

Creating arrays

>>> import numpy as np

#importing numpy as np so we don’t have to type numpy again & aagain.

>>> df_list1 = [1, 2, 3, 4, 5]

#creating a list which is one of the ways to create an array

>>> df_array1 = np.array(df_list1)

#now we can create an array df_array1

>>> df_array1

array([1, 2, 3, 4, 5])

#checking df_array1

>>> df_list2 = [6, 7, 8, 9, 10]

#creating another list df_list2

>>> df_lists = [df_list1, df_list2]

#combined df_list1 and df_list2

>>> df_array2 = np.array(df_lists)

#creating a multidimensional array out of df_lists

>>> df_array2

array([[ 1, 2, 3, 4, 5],

[ 6, 7, 8, 9, 10]])

#checking df_array2

>>> df_array2.shape

(2, 5)

#checking shape of the array

>>> df_array2.dtype

dtype(‘int64’)

#checking the data-type of array

#now special case arrays

>>> np.zeros(5)

array([0., 0., 0., 0., 0.])

#we created an array of zeros of shape 1:5

#also “0.” represents floating type

>>> df_zeros_array = np.zeros(5)

#converting to array to check data-type

>>> df_zeros_array.dtype

dtype(‘float64’)

#checking data-type

>>> np.ones([5,5])

array([[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.]])

#another special type of array of 1s with shape of 5:5

>>> np.empty(5)

array([0., 0., 0., 0., 0.])

#empty array

>>> np.eye(5)

array([[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.]])

#identity matrix

another way to make array is by using “arange”

with structure as (start, stop, step)

>>> np.arange(5)

array([0, 1, 2, 3, 4])

#array with value 5

>>> np.arange(5,50,2)

array([ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37,

39, 41, 43, 45, 47, 49])

#so now we start at 5, stop at 50 and take steps of 2

Importing array and using scalars

>>> import numpy as np

>>> 5.0/2

2.5

#here its a float value so we get decimal

>>> arr1 = np.array([[2,5,6,7],[1,4,8,9]])

>>> arr1

array([[2, 5, 6, 7],

[1, 4, 8, 9]])

# now we created a multidimensional array

>>> arr1*arr1

array([[ 4, 25, 36, 49],

[ 1, 16, 64, 81]]

# we multiplied every array

>>> arr1-arr1

array([[0, 0, 0, 0],

[0, 0, 0, 0]])

#here we subtracted array1 by itself

>>> 1/arr1

array([[0.5 , 0.2 , 0.16666667, 0.14285714],

[1. , 0.25 , 0.125 , 0.11111111]])

#now we performed arithmetic operations with scalar on array

>>> 1/arr1

array([[0.5 , 0.2 , 0.16666667, 0.14285714],

[1. , 0.25 , 0.125 , 0.11111111]])

# exponential operation is done with array

>>> arr = np.arange

INDEXING ARRAY

>>> arr = np.arange(0,9)

>>> arr

array([0, 1, 2, 3, 4, 5, 6, 7, 8])

# we get 9 points here

>>> arr[7]

7

#we get value at particular index

>>> arr[1:7]

array([1, 2, 3, 4, 5, 6])

#values at particular range are displayed

>>> arr[0:4] =100

>>> arr

array([100, 100, 100, 100, 4, 5, 6, 7, 8])

# here we set value of index

>>> arr = np.arange(0,9)

>>> arr

array([0, 1, 2, 3, 4, 5, 6, 7, 8])

# we shall reset the array

>>> slice_of_arr = arr[0:5]

>>> slice_of_arr

array([0, 1, 2, 3, 4])

#here we get slices of array

>>> slice_of_arr[:]=99

>>> slice_of_arr

array([99, 99, 99, 99, 99])

# here we change value of elements of array

>>> arr

array([99, 99, 99, 99, 99, 5, 6, 7, 8])

# we can see that original array has also changed due to slicing

>>> arr_copy = arr.copy()

>>> arr_copy

array([99, 99, 99, 99, 99, 5, 6, 7, 8])

# we have copy of original array after slicing

>>> arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))

>>> arr_2d

array([[ 5, 10, 15],

[20, 25, 30],

[35, 40, 45]])

# this in indexing in 2D array in matrix form.

>>> arr_2d[1]

array([20, 25, 30])

#here we observe a single row index 1 of 2D array.

>>> arr_2d[0]

array([ 5, 10, 15])

#we observe zeroth index row of 2D array .

>>> arr_2d[1][0]

20

here we get specified value .

>>> arr_2d[:2,1:]

array([[10, 15],

[25, 30]])

#here we get slicing of original 2D array

>>> arr2d = np.zeros((10,10))

>>> arr2d

array([[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., 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.],

[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., 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.]])

#this is fancy indexing done by making all zeros array

>>> arr_length = arr2d.shape[1]

>>> arr_length

10

#this shows length of array

Congratulations on making this far, tutorial ahead are in the process and will be published soon here! Till then Happy Coding! 🙂

About the author:

Leave a Reply

Your email address will not be published. Required fields are marked *

Top