Monday, August 27, 2018

Basic Numpy Functions & Slicing | Numpy Tutorial Part 2 | Codeing School

This is the part 2 of Numpy Tutorial and Jupyter Notebook Tutorial. Numpy is a python array function, it helps for Data Science and Data Analysis, and it is used for scientific computing with Python. This is an introduction for beginners with examples. Jupyter Notebook is best for Data Science and Data Analysis, that's why we used Jupyter Notebook.   
Numpy Tutorial Part 2 | Jupyter Notebook Tutorial Part 2 | Coding School
Numpy Tutorial Part 2 | Jupyter Notebook Tutorial Part 2

Today's Topic is: Basic NumpyFunctions and Slicing 



Basic Numpy Function:


1. Make a Numpy Array :


import numpy as np

array1 = np.array( [ [1,2,3,4,5],[6,7,8,9,10] ] )

(array1)


Output:
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
Simple Numpy Array


Logic: Here I take a simple numpy array, by using this syntax --> np.array(). This is a array          funtion in numpy. array1 is a variable, where i store the array. And then print the array1, and I got the value of array1. This is a 2D array.


2. Find the shape of the array:

array1.shape

Output:
( 2 , 5 )
Find the shape of the array
Logic: We can find the shape of the array by using this syntax --> array1.shape. This array has 2 rows and 5 columns, so it is a [2x5] array. 


3. Find the data type of the array:

array1.dtype

Output:
dtype( 'int32' )

Find the data type of the array

Logic: We take the array [1,2,3,4,5],[6,7,8,9,10]  . In this array of the elements is the integer type. That's why the data type of the array is ' int32'.  if you take any float value on this array like --> [1,2,3.5,4,5],[6,7,8,9,10], then the data type of the array will change to ' float64 '.               


4. The Zeros array:

np.zeros( (2,3) )

Output:
array([[0., 0., 0.],
[0., 0., 0.]])
The Zeros array
Logic: With the help of the Numpy function we can create a zeros array.

5. The One's array:

np.ones( (2,3) )

Output:
array([[0., 0., 0.],
[0., 0., 0.]])
The One's array
Logic: With the help of the Numpy function we can create a One's array.


6. The Transpose() function:

array1.transpose()

Output:
array([[ 1,  6],
           [ 2,  7],
           [ 3,  8],
           [ 4,  9],
           [ 5, 10]])
Transpose Function
Logic: With the help of the Numpy function we can do transpose of the given array. 

Note 1: Explore more Numpy attrebutes read this Numpy Official Documentation:        https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.html      

Note 2: Empty array and zero array are not equal. Because in zero array all the elements are 0, which is a value. But in the empty array all the elements are not a proper value,  some are there garbage value. 



7. Add Tow array:

array1 + array1

output:
array([[ 2,  4,  6,  8, 10],
           [12, 14, 16, 18, 20]])
Add tow array
Logic: In Python, we can directly add the same array. In this add method the addition not Working like "Array Addition". In this function, add by the element wise.

Slicing 

Slicing is the one of the best feature of Python. Slicing can not to be used in list, it also  used in array and tuple. 
Slicing
Logic: In the above program, I take a array, in the array2 variable. And In the in arr avriable I stored the 1th position and 2th position of the element in the arrayThe syntax is --> arr = array2[1:3], 1:3    besause 1th index : 2th index + 1; then we get the 1th location value and 2th location value 2 & 3.
If I assign 9 in the (arr[0] = 9) 0th location of arr, then the main array wiil change. See: Line 19. That means, I change the value of the 1th position of the main array. 
To prevent this problem, see the code below:
In the 17th line, I copy the main array, that's why data will not change.


If you have any quarry, please comment below.

1 comment: