Sunday , September 25 2022
Home / Data Science (page 4)

Data Science

Array Manipulation

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

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

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

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

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

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

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

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 »

Introduction to NumPy

Introduction to NumPy

1 – Introduction to Numpy Introduction to NumPy¶NumPy (numerical Python) is a library that consists of multidimensional array objects and a set of routines for processing them. NumPy allows you to perform mathematical and logical operations on arrays. This tutorial walks you through the fundamentals of NumPy, including its design …

Read More »