Breaking News

# Numpy

## Suicide Case Study – NumPy

13 Suicide Case Study – Numpy Import Libraries¶ In : import numpy as np Read CSV¶ In : data = np.genfromtxt('Suicidesindia2001-2012.csv',delimiter=',',dtype=str) data Out: array([['State', 'Year', 'Type_code', ..., 'Gender', 'Age_group', 'Total'], ['A & N Islands', '2001', 'Causes', ..., 'Female', '0-14', '0'], ['A & N Islands', '2001', 'Causes', ..., 'Female', '0-14', '0'], ..., ['West …

## NumPy Set Operations

11 – Set Operations Numpy Set Operations¶ In : import numpy as np select the unique elements from an array¶ In : arr = np.array([1,1,2,2,3,3,4,5,6]) print(np.unique(arr)) [1 2 3 4 5 6] In : # return the number of times each unique item appears arr = np.array([1,1,2,2,3,3,4,5,6]) uniques, counts = np.unique(arr, return_counts=True) print(uniques) …

## Combine & Split an Array

10 – Combine & Split an Array In : import numpy as np In : arr1 = np.array([[1,2,3,4], [1,2,3,4]]) arr2 = np.array([[5,6,7,8], [5,6,7,8]]) np.concatenate((a, b), axis=0)¶ In : arr1 Out: array([[1, 2, 3, 4], [1, 2, 3, 4]]) In : arr2 Out: array([[5, 6, 7, 8], [5, 6, 7, 8]]) In : # concat along …

## Array Manipulation

9 – Manipulate an Array Numpy Array Manipulation¶ In : import numpy as np In : arr = np.random.randint(1,10,[3,3]) arr Out: array([[7, 4, 7], [5, 3, 6], [2, 1, 4]]) Transpose an array¶ In : print(arr.T) [[7 5 2] [4 3 1] [7 6 4]] or¶ In : print(np.transpose(arr)) [[7 5 2] [4 3 …

## NumPy Array Sorting

8 – Sort an Array Sorting of an Array¶ In : import numpy as np In : import numpy as np arr = np.array([[30,17,15],[19,90,16],[69,53,21]]) arr Out: array([[30, 17, 15], [19, 90, 16], [69, 53, 21]]) sort an array along a specified axis¶ In : # sort along the row and return a copy …

## Numpy Slicing & Indexing

7 – Slicing & Indexing Subset, Slice, Index and Iterate through Arrays¶For one-dimensional arrays, indexing, slicing etc. is similar to python lists – indexing starts at 0. Slicing arrays¶Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: …

## Numpy Mathematic Functions

6 – Math Functions NumPy Math Functions¶ In : import numpy as np In : arr = np.random.randint(10,99,[3,3]) arr Out: array([[45, 53, 78], [81, 88, 25], [71, 55, 59]]) Element-wise addition, subtraction, multiplication and division¶ In : print(arr + 10) print(arr - 10) print(arr * 10) print(arr / 10) [[55 63 88] [91 …

## Numpy Random Array

5 – Random Array NumPy Random Array¶ In : import numpy as np In : # generate a random scalar print(np.random.rand()) 0.2224104911171324 In : # generate a 1-D array print(np.random.rand(3)) [0.69589689 0.9990713 0.77034202] In : # generate a 2-D array print(np.random.rand(3,3)) [[0.2151302 0.64925559 0.95982155] [0.02673682 0.33937101 0.78181161] [0.39285799 0.7581885 0.15635241]] Generate a sample from …