Posts

Create minimum dimensions array using Numpy. Data science using Python in Anaconda - Jupyter

import  numpy  as  np print (mda) mda = np.array([ 1 , 2 , 3 , 4 , 5 , 6 ]) Output: - [1 2 3 4 5 6]

Perform the following Standard Set Operations using Numpy Array. array1 = np.array([[10, 20, 30], [14, 24, 36]]) array2 = np.array([[20, 40, 50], [24, 34, 46]]). Data science using Python in Anaconda - Jupyter

 Question 1. Find the union of two arrays 2. Find the intersection of two arrays 3. Find the set difference of two arrays Program: - import  numpy  as  np array1 = np.array([[ 10 ,  20 ,  30 ], [ 14 ,  24 ,  36 ]]) array2 = np.array([[ 20 ,  40 ,  50 ], [ 24 ,  34 ,  46 ]]) print ( "union of two arrays :- \n" ,np.union1d(array1,array2)) print ( "intersection of two arrays :- \n" ,np.intersect1d(array1,array2)) print ( "set difference of two arrays :- \n" ,np.setdiff1d(array1,array2)) Output: - union of two arrays :- [10 14 20 24 30 34 36 40 46 50] intersection of two arrays :- [20 24] set difference of two arrays :- [10 14 30 36]

Perform the following Logical Operations using Numpy Array. thearray = np.array([[10, 20, 30], [14, 24, 36]]). Data science using Python in Anaconda - Jupyter

 Question 1. logical_or(Condition array&lt;10, array&gt;15) 2. logical_and(Condition array&lt;10, array&gt;15) 3. logical_not(Condition array&lt;20) Program: - import  numpy  as  np thearray = np.array([[ 10 ,  20 ,  30 ], [ 14 ,  24 ,  36 ]]) print ( "Orignal Array 1:- \n" ,thearray, "\n" ) print ( "Logical OR :- \n" ,np.logical_or(thearray< 10 ,thearray> 15 )) print ( "Logical And :- \n" ,np.logical_and(thearray< 10 ,thearray> 15 )) print ( "Logical Not :- \n" ,np.logical_not(thearray< 20 )) Output: - Orignal Array 1:- [[10 20 30] [14 24 36]] Logical OR :- [[False True True] [False True True]] Logical And :- [[False False False] [False False False]] Logical Not :- [[False True True] [False True True]]

Perform the following Aggregate and Statistical Functions using Numpy Array. array1 = np.array([[10, 20, 30], [40, 50, 60]]). Data science using Python in Anaconda - Jupyter

 Question 1. Mean 2. Standard deviation 3. Variance 4. Sum of array elements 5. Product of array elements Program: - import  numpy  as  np array1 = np.array([[ 10 ,  20 ,  30 ], [ 40 ,  50 ,  60 ]]) print ( "Orignal Array 1:- \n" ,array1, "\n" ) print ( "Mean :-" ,np.mean(array1)) print ( "Standard deviation :-" ,np.std(array1)) print ( "Variance :-" ,np.var(array1)) print ( "Sum of array elements :-" ,np. sum (array1)) print ( "Product of array elements :-" ,np.prod(array1)) Output: - Orignal Array 1:- [[10 20 30] [40 50 60]] Mean :- 35.0 Standard deviation :- 17.07825127659933 Variance :- 291.6666666666667 Sum of array elements :- 210 Product of array elements :- 720000000

Perform the following Element-wise Mathematical Operations using Numpy Array. array1 = np.array([[10, 20, 30], [40, 50, 60]]) array2 = np.array([[2, 3, 4], [4, 6, 8]]) array3 = np.array([[-2, 3.5, -4], [4.05, -6, 8]]). Data science using Python in Anaconda - Jupyter

Question 1. Addition of array1 and array2 2. Multiplication of array1 and array2 3. Power of array1 and array2 Program: - import  numpy  as  np array2 = np.array([[ 2 ,  3 ,  4 ], [ 4 ,  6 ,  8 ]]) array3 = np.array([[ -2 ,  3.5 ,  -4 ], [ 4.05 ,  -6 ,  8 ]]) array1 = np.array([[ 10 ,  20 ,  30 ], [ 40 ,  50 ,  60 ]]) print ( "Orignal Array 1:- \n" ,array1, "\n" ) print ( "Orignal Array 2:- \n" ,array2, "\n" ) print ( "Orignal Array 2:- \n" ,array3, "\n" ) add=array1+array2+array3 mul=array1*array2*array3 powr=array1**array2**array3 print ( "Add :- \n" ,add) print ( "Multiplication :- \n" ,mul) print ( "Power :- \n" ,powr) Output: - Orignal Array 1:- [[10 20 30] [40 50 60]] Orignal Array 2:- [[2 3 4] [4 6 8]] Orignal Array 2:- [[-2. 3.5 -4. ] [ 4.05 -6. 8. ]] Add :- [[1...

Perform the following Elementary Mathematical Functions using Numpy Array. array1 = np.array([[10, 20, 30], [40, 50, 60]]). Data science using Python in Anaconda - Jupyter

 Question 1. sin(array1) 2. cos(array1) 3. tan(array1) 4. sqrt(array1) 5. exp(array1) 6. log10(array1) Program: - import  numpy  as  np array1 = np.array([[ 10 ,  20 ,  30 ], [ 40 ,  50 ,  60 ]]) print ( "Orignal Array 1:- \n" ,array1, "\n" ) print ( "Sin :- \n" ,np.sin(array1)) print ( "Cos :- \n" ,np.cos(array1)) print ( "Tan :- \n" ,np.tan(array1)) print ( "Sqrt :- \n" ,np.sqrt(array1)) print ( "Exp :- \n" ,np.exp(array1)) print ( "log :- \n" ,np.log10(array1)) Output: - Orignal Array 1:- [[10 20 30] [40 50 60]] Sin :- [[-0.54402111 0.91294525 -0.98803162] [ 0.74511316 -0.26237485 -0.30481062]] Cos :- [[-0.83907153 0.40808206 0.15425145] [-0.66693806 0.96496603 -0.95241298]] Tan :- [[ 0.64836083 2.23716094 -6.4053312 ] [-1.11721493 -0.27190061 0.32004039]] Sqrt :- [[3.16227766 4.47213595 5.47722558] [6.32455532 7.07106781 7.74596669]...

Perform the following Scalar Arithmetic Operations using Numpy Array. array1 = np.array([[10, 20, 30], [40, 50, 60]]). Data science using Python in Anaconda - Jupyter

 Question 1. array1 + 2 2. array1 – 5 3. array1 * 2 4. array1 / 5 5. array1 ** 2 Program: - import  numpy  as  np array1 = np.array([[ 10 ,  20 ,  30 ], [ 40 ,  50 ,  60 ]]) print ( "Orignal Array 1:- \n" ,array1, "\n" ) print ( "Adding by 2 :- \n" ,array1+ 2 ) print ( "Subtracting by 5 :- \n" ,array1 -5 ) print ( "Multiply  by 2:- \n" ,array1* 2 ) print ( "Dividing by 5 :- \n" ,array1/ 5 ) print ( "Power by 2 :- \n" ,array1** 2 ) Output: - Orignal Array 1:- [[10 20 30] [40 50 60]] Adding by 2 :- [[12 22 32] [42 52 62]] Subtracting by 5 :- [[ 5 15 25] [35 45 55]] Multiply by 2:- [[ 20 40 60] [ 80 100 120]] Dividing by 5 :- [[ 2. 4. 6.] [ 8. 10. 12.]] Power by 2 :- [[ 100 400 900] [1600 2500 3600]]

Perform the following Arithmetic Operations using Numpy Array. array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[7, 8, 9], [10, 11, 12]]). Data science using Python in Anaconda - Jupyter

 Question 1. array1 + array2 2. array1 - array2 3. array1 * array2 4. array2 / array1 5. array1 ** array2 Program: - import  numpy  as  np array1 = np.array([[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ]]) array2 = np.array([[ 7 ,  8 ,  9 ], [ 10 ,  11 ,  12 ]]) print ( "Orignal Array 1:- \n" ,array1, "\n" ) print ( "Orignal Array 2:- \n" ,array2, "\n" ) print ( "Add :- \n" ,np.add(array1,array2)) print ( "Subtract :- \n" ,np.subtract(array1,array2)) print ( "Divison :- \n" ,np.divide(array1,array2)) print ( "Multiply :- \n" ,np.multiply(array1,array2)) print ( "Power :- \n" ,np.power(array1,array2)) Output: - Orignal Array 1:- [[1 2 3] [4 5 6]] Orignal Array 2:- [[ 7 8 9] [10 11 12]] Add :- [[ 8 10 12] [14 16 18]] Subtract :- [[-6 -6 -6] [-6 -6 -6]] Divison :- [[0.14285714 0.25 0.33333333] [0.4 0.4...

Perform the following operations to Manipulating the Dimensions and the Shape of Arrays(Joining and Stacking). Data science using Python in Anaconda - Jupyter

 Question  array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[7, 8, 9], [10, 11, 12]]) 1. Stack arrays in sequence horizontally (column wise). 2. Stack arrays in sequence vertically (row wise) 3. Stack arrays in sequence depth wise (along third axis) 4. Appending arrays after each other, along a given axis 5. Append values to the end of an array Program: - import  numpy  as  np array1 = np.array([[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ]]) array2 = np.array([[ 7 ,  8 ,  9 ], [ 10 ,  11 ,  12 ]]) print ( "Orignal Array 1:- \n" ,array1, "\n" ) print ( "Orignal Array 2:- \n" ,array2, "\n" ) print ( "Stack arrays in sequence horizontally (column wise). :- \n" ,np.hstack((array1,array2))) print ( "Stack arrays in sequence vertically (row wise) :- \n" ,np.vstack((array1,array2))) print ( "...

Perform the following operations to Manipulating the Dimensions and the Shape of Arrays(Flips the order of the Axes) array2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]). Data science using Python in Anaconda - Jupyter

 Question 1. Permute the dimensions of an array 2. Flip array in the left/right direction 3. Flip array in the up/down direction 4. Rotate an array by 90 degrees in the plane specified by axes Program: - import  numpy  as  np array2d = np.array([[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ], [ 7 ,  8 ,  9 ]]) print ( "Orignal Array:- \n" ,array2d, "\n" ) print ( "Permute the dimensions of an array :-" ,np.transpose(array2d))  #Unable to find print ( "Flip array in the left/right direction :- \n" ,np.fliplr(array2d)) print ( "Flip array in the up/down direction :- \n" ,np.flipud(array2d)) print ( "Rotate an array by 90 degrees in the plane specified by axes :- \n" ,np.rot90(array2d)) Output: - Orignal Array:- [[1 2 3] [4 5 6] [7 8 9]] Perm...

Perform the following Multidimensional Dimensional Slicing Operations using Numpy array. array2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]). Data science using Python in Anaconda - Jupyter

 Question 1. 2nd and 3rd col 2. 2nd and 3rd row 3. Reverse an array Program: - import  numpy  as  np array2d = np.array([[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ], [ 7 ,  8 ,  9 ]]) print ( "Orignal Array:- \n" ,array2d, "\n" ) print ( "2nd and 3rd col :- \n" ,array2d[:, 1 :]) print ( "2nd and 3rd row :- \n" ,array2d[ 1 :,:]) print ( "Reverse an array :- \n" ,array2d[:: -1 ,:: -1 ]) Output: - Orignal Array:- [[1 2 3] [4 5 6] [7 8 9]] 2nd and 3rd col :- [[2 3] [5 6] [8 9]] 2nd and 3rd row :- [[4 5 6] [7 8 9]] Reverse an array :- [[9 8 7] [6 5 4] [3 2 1]]

Perform the following Single Dimensional Slicing Operations using Numpy array. array1d = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]). Data science using Python in Anaconda - Jupyter

Question 1. from index 4 to last index 2. From index 0 to 4 index 3. From index 4(included) up to index 7(excluded) 4. Excluded last element 5. Up to second last index ( negative index) 6. From last to first in reverse order ( negative step) 7. All odd numbers in reversed order 8. All even numbers in reversed order 9. All elements array1d = np.array([ 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ]) print ( "Orignal Array: " ,array1d, "\n" ) print ( "1. from index 4 to last index: " ,array1d[ 3 :]) print ( "2. From index 0 to 4 index: " ,array1d[ 0 : 4 ]) pint ( "3. From index 4(included) up to index 7(excluded): " ,array1d[ 4 :: 2 ]) print ( "4. Excluded last element: " ,array1d[ 0 : -2 ]) print ( "5. Up to second last index (negative index): " ,array1d[ 0 : -2 ]) print ( "6. From...

Perform the following Indexing Operations using Numpy array. array3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]). Data science using Python in Anaconda - Jupyter

import  numpy  as  np   array3d = np.array([[[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ]], [[ 7 ,  8 ,  9 ], [ 10 ,  11 ,  12 ]]]) print (array3d) print ( "first row first col: " ,array3d[ 0 ][ 0 ][ 0 ]) print ( "first row second col: " ,array3d[ 0 ][ 0 ][ 1 ]) print ( "second row first col: " ,array3d[ 0 ][ 1 ][ 0 ]) print ( "second row second col: " ,array3d[ 0 ][ 1 ][ 1 ]) Output: - [[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]] first row first col: 1 first row second col: 2 second row first col: 4 second row second col: 5

Perform the following Indexing Operations using Numpy array. array2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) . Data science using Python in Anaconda - Jupyter

Question 1. Get first row first col 2. Get first row second col 3. Get first row second col 4. Get second row second col Program: - import  numpy  as  np   array2d = np.array([[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ], [ 7 ,  8 ,  9 ]]) print (array2d) print ( "first row first col :- " ,array2d[ 0 : 1 ,  0 : 1 ]) print ( "first row second col :- " ,array2d[ 0 : 1 , 1 : 2 ]) print ( "second row first col :- " ,array2d[ 1 : 2 , 0 : 1 ]) print ( "second row second col :- " ,array2d[ 1 : 2 , 1 : 2 ]) Output: - [[1 2 3] [4 5 6] [7 8 9]] first row first col :- [[1]] first row second col :- [[2]] second row first col :- [[4]] second row second col :- [[5]]

Perform the following Indexing Operations using Numpy array array1d = np.array([1, 2, 3, 4, 5, 6]). Data science using Python in Anaconda - Jupyter

import  numpy  as  np array1d = np.array([ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ]) print ( "first value :- " ,array1d[ 0 ]) print ( "last value :- " ,array1d[ -1 ]) print ( "4th value from first :- " ,array1d[ 3 ]) print ( "5th value from last :- " ,array1d[ -5 ]) print ( "multiple values :- " ,array1d) Output: - first value :- 1 last value :- 6 4th value from first :- 4 5th value from last :- 2 multiple values :- [1 2 3 4 5 6]

Create the Program to Transform List or Tuple into NumPy array. Data science using Python in Anaconda - Jupyter

import  numpy  as  np list1 = [ 1 , 2 , 3 , 4 ] tuple1 = ( 5 , 6 , 7 , 8 ) print ( "List :- " ,list1, " DataType:- " , type (list1)) print ( "Tuple :- " ,tuple1, " DataType:- " , type (tuple1)) tupleArray = np.asarray(tuple1)  #first way listArray = np.array(list1)  # Second Way print ( "List to Array :- " , type (tupleArray)) print ( "Tuple to Array :- " , type (listArray)) Output: - List :- [1, 2, 3, 4] DataType:- <class 'list'> Tuple :- (5, 6, 7, 8) DataType:- <class 'tuple'> List to Array :- <class 'numpy.ndarray'> Tuple to Array :- <class 'numpy.ndarray'>

Use the resize and reshape method on Numpy array. Data science using Python in Anaconda - Jupyter

import  numpy  as  np ar1 = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) ar2 = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]) #It change orignal array ar2.resize( 2 , 4 )  #It dose not change orignal array ar1.reshape( 2 , 4 ) print ( "ar1 :-\n" , ar1) print ( "ar2: -\n" , ar2) Output: - ar1 :- [1 2 3 4 5 6 7 8] ar2: - [[1 2 3 4] [5 6 7 8]]

What is the minimum loan amount disbursed in the agriculture sector? Data science using Python in Anaconda - Jupyter

import pandas as pd data = pd.read_csv('lendingdata.csv') data.groupby('sector')['loan_amount'].max()['Agriculture'] Output: - 11025.0

What is the percentage split of the different categories in the column “repayment_interval” after dropping the missing values? Data science using Python in Anaconda - Jupyter

Image
import pandas as pd data = pd.read_csv('lendingdata.csv') round(data.repayment_interval.value_counts()*100/data.shape[0]) Output: -

What is the third quartile value of the variable “loan_amount”. Data science using Python in Anaconda - Jupyter

Image
import pandas as pd data = pd.read_csv('lendingdata.csv') data['loan_amount'].describe() Output: -