Wednesday , January 19 2022
Home / Data Science (page 2)

Data Science

NumPy Set Operations

11 – Set Operations Numpy Set Operations¶ In [1]: import numpy as np select the unique elements from an array¶ In [2]: arr = np.array([1,1,2,2,3,3,4,5,6]) print(np.unique(arr)) [1 2 3 4 5 6] In [3]: # 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) …

Read More »

Combine & Split an Array

10 – Combine & Split an Array In [2]: import numpy as np In [3]: 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 [4]: arr1 Out[4]: array([[1, 2, 3, 4], [1, 2, 3, 4]]) In [5]: arr2 Out[5]: array([[5, 6, 7, 8], [5, 6, 7, 8]]) In [6]: # concat along …

Read More »

Array Manipulation

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

Read More »

NumPy Array Sorting

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

Read More »

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: …

Read More »

Numpy Mathematic Functions

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

Read More »

Numpy Random Array

5 – Random Array NumPy Random Array¶ In [2]: import numpy as np In [3]: # generate a random scalar print(np.random.rand()) 0.2224104911171324 In [4]: # generate a 1-D array print(np.random.rand(3)) [0.69589689 0.9990713 0.77034202] In [5]: # 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 …

Read More »

Inspect an Array

4 – Inspect an Array Inspect NumPy Array¶ In [2]: import numpy as np In [3]: arr = np.array([[1,2,3], [4,5,6]], dtype=np.int64) arr Out[3]: array([[1, 2, 3], [4, 5, 6]], dtype=int64) Inspect general information of an array¶ In [4]: print(np.info(arr)) class: ndarray shape: (2, 3) strides: (24, 8) itemsize: 8 aligned: True contiguous: True …

Read More »

Numpy Datatypes

3 – Numpy Data Types NumPy Data Types¶ In [2]: import numpy as np import pandas as pd Data Types in NumPy¶Numpy has the following data types: int float complex bool string unicode object The numeric data types have various precisions like 32-bit or 64-bit. Numpy data types can be represented …

Read More »

Creating NumPy Array

2 – Create an Array Create a NumPy Array¶The learning objectives of this section are: Understand advantages of vectorised code using NumPy (over standard python ways) Create NumPy arrays Convert lists and tuples to NumPy arrays Create (initialise) arrays Compare computation times in NumPy and standard Python lists NumPy Basics¶NumPy …

Read More »