Created
March 26, 2019 01:24
-
-
Save pb111/f08def0ca01c8d5972088bc1fd9354cc to your computer and use it in GitHub Desktop.
Data Analysis with NumPy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Data Analysis with NumPy\n", | |
"\n", | |
"\n", | |
"NumPy is the fundamental package of Python which is required for scientific computing. In this project, I explore NumPy and various data analysis tools of NumPy." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Table of contents\n", | |
"\n", | |
"\n", | |
"\n", | |
"Table of contents for this project is as follows:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"1.\tIntroduction to NumPy\n", | |
"\n", | |
"2.\tKey features of NumPy\n", | |
"\n", | |
"3.\tAdvantages of NumPy\n", | |
"\n", | |
"4.\tImporting NumPy\n", | |
"\n", | |
"5.\tImport data\n", | |
"\n", | |
"6.\tDataset description\n", | |
"\n", | |
"7.\tNumPy ndarray object\n", | |
"\n", | |
"8.\tNumPy data types\n", | |
"\n", | |
"9.\tNumPy array attributes\n", | |
"\n", | |
"10.\tNumPy array creation \n", | |
"\n", | |
"11.\tNumPy array from existing data\n", | |
"\n", | |
"12.\tNumPy array from numerical ranges\n", | |
"\n", | |
"13.\tNumPy array manipulation\n", | |
"\n", | |
"14.\tNumPy indexing and slicing\n", | |
"\n", | |
"15.\tNumPy Broadcasting\n", | |
"\n", | |
"16.\tNumPy binary operators\n", | |
"\n", | |
"17.\tNumPy string functions\n", | |
"\n", | |
"18.\tNumPy arithmetic operations\n", | |
"\n", | |
"19.\tNumPy statistical functions\n", | |
"\n", | |
"20.\tNumPy sorting\n", | |
"\n", | |
"21.\tNumPy searching\n", | |
"\n", | |
"22.\tNumPy copies and views\n", | |
"\n", | |
"23.\tInput output with NumPy\n", | |
"\n", | |
"24.\tRandom sampling with NumPy\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1. Introduction to NumPy\n", | |
"\n", | |
"\n", | |
"**NumPy** is a Python package. It stands for **Numerical Python**. It is the fundamental package for scientific computing in Python. It is the Python library that provides multidimensional array objects, various derived objects and a collection of routines for processing of array. NumPy is used to perform mathematical, logical, shape manipulation, sorting, selecting, input output, linear algebra, statistical operations and random simulation on arrays.\n", | |
"\n", | |
"\n", | |
"The ancestor of NumPy was Numeric. It was originally created by Jim Hugunin with contributions from several other developers. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. Since, then the NumPy community has grown extensively. So, NumPy is an open source software and has many contributors.\n", | |
"\n", | |
"\n", | |
"NumPy is the subject matter of this project. I will discuss NumPy and various data analysis tools associated with NumPy.\n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 2. Key features of NumPy\n", | |
"\n", | |
"\n", | |
"The key features of NumPy are as follows:-\n", | |
"\n", | |
"\n", | |
"1.\tNumPy is designed for scientific computation with Python.\n", | |
"\n", | |
"\n", | |
"2.\tNumPy provides tools for array oriented computing.\n", | |
"\n", | |
"\n", | |
"3.\tIt efficiently implemented multi-dimensional arrays.\n", | |
"\n", | |
"\n", | |
"4.\tNumPy arrays have a fixed size at creation. We can change the size of the array. It will create a new array and delete \n", | |
" the original. \n", | |
" \n", | |
" \n", | |
"5.\tAt the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous data \n", | |
" types.\n", | |
" \n", | |
" \n", | |
"6.\tThe elements in a NumPy array are all required to be of the same data type.\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 3. Advantages of NumPy\n", | |
"\n", | |
"\n", | |
"Advantages of NumPy are as follows:-\n", | |
"\n", | |
"\n", | |
"1.\tNumPy enable us to perform mathematical and logical operations on arrays.\n", | |
"\n", | |
"\n", | |
"2.\tIt provides tools to perform Fourier transformation and routines for shape manipulation.\n", | |
"\n", | |
"\n", | |
"3.\tNumPy can perform operations related to linear algebra. NumPy has in-built functions for linear algebra and random \n", | |
" number generation.\n", | |
" \n", | |
" \n", | |
"4.\tNumPy arrays facilitate advanced mathematical and other types of operations on large datasets. \n", | |
"\n", | |
"\n", | |
"5.\tA large number of Python packages uses NumPy arrays. These support Python-sequence inputs. They convert such input to NumPy arrays prior to processing, and they often output NumPy arrays.\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 4. Importing NumPy\n", | |
"\n", | |
"\n", | |
"In order to use NumPy in our work, we need to import the NumPy library first. We can import the NumPy library \n", | |
"with the following command:-\n", | |
"\n", | |
"\n", | |
"`import numpy`\n", | |
"\n", | |
"\n", | |
"Usually, we import the NumPy library by appending the alias `as np`. It makes things easier because now instead of writing\n", | |
"`numpy.command` we need to write `np.command`. \n", | |
"\n", | |
"\n", | |
"So, I will import the numpy library with the following command:-\n", | |
"\n", | |
"\n", | |
"`import numpy as np`\n", | |
"\n", | |
"\n", | |
"Also, I will import Pandas as well which is the open source library for data analysis in Python. I will import Pandas \n", | |
"with the following command:-\n", | |
"\n", | |
"\n", | |
"`import pandas as pd`\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# import required libraries\n", | |
"\n", | |
"import numpy as np\n", | |
"\n", | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 5. Import data\n", | |
"\n", | |
"\n", | |
"In this project, I work with the **Forest Fires Data Set** which is a comma-separated values (CSV) file type. \n", | |
"\n", | |
"\n", | |
"In a CSV file type, the data is stored as a comma-separated values where each row is separated by a new line, and each \n", | |
"column by a comma (,).\n", | |
"\n", | |
"\n", | |
"I use the **pandas read_csv()** function to import the data set as follows:-\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data = 'C:/datasets/forestfires.csv'\n", | |
"\n", | |
"\n", | |
"df = pd.read_csv(data)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>X</th>\n", | |
" <th>Y</th>\n", | |
" <th>month</th>\n", | |
" <th>day</th>\n", | |
" <th>FFMC</th>\n", | |
" <th>DMC</th>\n", | |
" <th>DC</th>\n", | |
" <th>ISI</th>\n", | |
" <th>temp</th>\n", | |
" <th>RH</th>\n", | |
" <th>wind</th>\n", | |
" <th>rain</th>\n", | |
" <th>area</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>7</td>\n", | |
" <td>5</td>\n", | |
" <td>mar</td>\n", | |
" <td>fri</td>\n", | |
" <td>86.2</td>\n", | |
" <td>26.2</td>\n", | |
" <td>94.3</td>\n", | |
" <td>5.1</td>\n", | |
" <td>8.2</td>\n", | |
" <td>51</td>\n", | |
" <td>6.7</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>7</td>\n", | |
" <td>4</td>\n", | |
" <td>oct</td>\n", | |
" <td>tue</td>\n", | |
" <td>90.6</td>\n", | |
" <td>35.4</td>\n", | |
" <td>669.1</td>\n", | |
" <td>6.7</td>\n", | |
" <td>18.0</td>\n", | |
" <td>33</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>7</td>\n", | |
" <td>4</td>\n", | |
" <td>oct</td>\n", | |
" <td>sat</td>\n", | |
" <td>90.6</td>\n", | |
" <td>43.7</td>\n", | |
" <td>686.9</td>\n", | |
" <td>6.7</td>\n", | |
" <td>14.6</td>\n", | |
" <td>33</td>\n", | |
" <td>1.3</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>8</td>\n", | |
" <td>6</td>\n", | |
" <td>mar</td>\n", | |
" <td>fri</td>\n", | |
" <td>91.7</td>\n", | |
" <td>33.3</td>\n", | |
" <td>77.5</td>\n", | |
" <td>9.0</td>\n", | |
" <td>8.3</td>\n", | |
" <td>97</td>\n", | |
" <td>4.0</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>8</td>\n", | |
" <td>6</td>\n", | |
" <td>mar</td>\n", | |
" <td>sun</td>\n", | |
" <td>89.3</td>\n", | |
" <td>51.3</td>\n", | |
" <td>102.2</td>\n", | |
" <td>9.6</td>\n", | |
" <td>11.4</td>\n", | |
" <td>99</td>\n", | |
" <td>1.8</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" X Y month day FFMC DMC DC ISI temp RH wind rain area\n", | |
"0 7 5 mar fri 86.2 26.2 94.3 5.1 8.2 51 6.7 0.0 0.0\n", | |
"1 7 4 oct tue 90.6 35.4 669.1 6.7 18.0 33 0.9 0.0 0.0\n", | |
"2 7 4 oct sat 90.6 43.7 686.9 6.7 14.6 33 1.3 0.0 0.0\n", | |
"3 8 6 mar fri 91.7 33.3 77.5 9.0 8.3 97 4.0 0.2 0.0\n", | |
"4 8 6 mar sun 89.3 51.3 102.2 9.6 11.4 99 1.8 0.0 0.0" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# view the first five rows of dataset\n", | |
"\n", | |
"df.head()\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 6. Dataset description\n", | |
"\n", | |
"\n", | |
"I have used the **Forest Fires** dataset for this project. The dataset represents the burned area of forest fires using \n", | |
"meteorological data.\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Attribute Information -\n", | |
"\n", | |
"\n", | |
"The dataset contains 517 instances and 13 attributes. The attribute information is as follows:-\n", | |
"\n", | |
" \n", | |
"- 1. X - x-axis spatial coordinate within the Montesinho park map: 1 to 9 \n", | |
"\n", | |
"\n", | |
"- 2. Y - y-axis spatial coordinate within the Montesinho park map: 2 to 9 \n", | |
"\n", | |
"\n", | |
"- 3. month - month of the year: 'jan' to 'dec' \n", | |
"\n", | |
"\n", | |
"- 4. day - day of the week: 'mon' to 'sun' \n", | |
"\n", | |
"\n", | |
"- 5. FFMC - FFMC index from the FWI system: 18.7 to 96.20 \n", | |
"\n", | |
"\n", | |
"- 6. DMC - DMC index from the FWI system: 1.1 to 291.3 \n", | |
"\n", | |
"\n", | |
"- 7. DC - DC index from the FWI system: 7.9 to 860.6 \n", | |
"\n", | |
"\n", | |
"- 8. ISI - ISI index from the FWI system: 0.0 to 56.10 \n", | |
"\n", | |
"\n", | |
"- 9. temp - temperature in Celsius degrees: 2.2 to 33.30 \n", | |
"\n", | |
"\n", | |
"- 10. RH - relative humidity in %: 15.0 to 100 \n", | |
"\n", | |
"\n", | |
"- 11. wind - wind speed in km/h: 0.40 to 9.40 \n", | |
"\n", | |
"\n", | |
"- 12. rain - outside rain in mm/m2 : 0.0 to 6.4 \n", | |
"\n", | |
"\n", | |
"- 13. area - the burned area of the forest (in ha): 0.00 to 1090.84 \n", | |
"\n", | |
"\n", | |
"(area variable is very skewed towards 0.0, thus it may make sense to model with the logarithm transform).\n", | |
"\n", | |
"\n", | |
"I have downloaded this dataset from the UCI machine learning repository from the following url:-\n", | |
"\n", | |
"\n", | |
"https://archive.ics.uci.edu/ml/datasets/Forest+Fires\n", | |
"\n", | |
"\n", | |
"In this project, I will analyze this dataset using NumPy, which is a commonly used Python data analysis package.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"I will extract the last 5 variables - `temp`(temperature), `RH`(relative humidity), `wind`(wind speed), `rain`(rain) and \n", | |
"\n", | |
"`area`(area) into a new dataset `fires`. I will also extract the first five rows of dataset as follws:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" temp RH wind rain area\n", | |
"0 8.2 51 6.7 0.0 0.0\n", | |
"1 18.0 33 0.9 0.0 0.0\n", | |
"2 14.6 33 1.3 0.0 0.0\n", | |
"3 8.3 97 4.0 0.2 0.0\n", | |
"4 11.4 99 1.8 0.0 0.0\n" | |
] | |
} | |
], | |
"source": [ | |
"fires = df.iloc[:5,8:]\n", | |
"\n", | |
"print(fires)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"I have assigned the first five rows and the last five variables of `df` dataset to the `fires` dataset. I will work with `fires` dataset in this project." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 7. NumPy ndarray object\n", | |
"\n", | |
"\n", | |
"\n", | |
"The most important object defined in NumPy is an N-dimensional array called **ndarray**. It describes the collection of \n", | |
"items of the same type. Items in the collection can be accessed using a zero-based index. Each element in ndarray is an object of data-type object (called dtype).\n", | |
"\n", | |
"\n", | |
"\n", | |
"An instance of ndarray class can be constructed by different array creation routines. The basic ndarray is created using an array function in NumPy with the following code snippet as follows:−\n", | |
"\n", | |
"\n", | |
"\n", | |
"`numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"The above constructor takes the following parameters:−\n", | |
"\n", | |
"\n", | |
"- object - Any object exposing the array interface method returns an array, or any (nested) sequence.\n", | |
"\n", | |
"\n", | |
"- dtype - Desired data type of array, optional\n", | |
"\n", | |
"\n", | |
"- copy - (Optional) - By default (true), the object is copied\n", | |
"\n", | |
"\n", | |
"- order - C (row major) or F (column major) or A (any) (default)\n", | |
"\n", | |
"\n", | |
"- subok - By default, returned array forced to be a base class array. If true, sub-classes passed through\n", | |
"\n", | |
"\n", | |
"- ndmin - Specifies minimum dimensions of resultant array\n", | |
"\n", | |
"\n", | |
"Now, I will illustrate the concept better with the following examples:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"# one dimensional array\n", | |
"\n", | |
"a = np.array([1,2,3])\n", | |
"\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 8.2 51. 6.7 0. 0. ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# continuing with our fires dataset, we can extract first row of fires and \n", | |
"# transform into a 1-D array as follows:-\n", | |
"\n", | |
"n1 = np.array(fires[:1])\n", | |
"\n", | |
"print(n1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# two dimensional array\n", | |
"\n", | |
"b = np.array([[1, 2], [3, 4]])\n", | |
"\n", | |
"print(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# a two dimensional array of the fires dataset \n", | |
"# with integer data types can be created as follows\n", | |
"\n", | |
"n2 = np.array(fires[:2], dtype=int)\n", | |
"\n", | |
"print(n2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]\n", | |
" [5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# three dimensional array\n", | |
"\n", | |
"c = np.array([[1,2],[3,4],[5,6]])\n", | |
"\n", | |
"print(c)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# following along with the fires dataset, a three dimensional array of the \n", | |
"# fires dataset with integer data types can be created as follows\n", | |
"\n", | |
"n3 = np.array(fires[:3], dtype=int)\n", | |
"\n", | |
"print(n3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2 3 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# minimum dimensions \n", | |
"\n", | |
"d = np.array([1, 2, 3, 4, 5], ndmin = 2)\n", | |
"\n", | |
"print(d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1.+0.j 2.+0.j 3.+0.j]\n" | |
] | |
} | |
], | |
"source": [ | |
"# dtype parameter \n", | |
"\n", | |
"e = np.array([1, 2, 3], dtype = complex) \n", | |
"\n", | |
"print(e)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 8. NumPy data types\n", | |
"\n", | |
"\n", | |
"\n", | |
"NumPy supports a much greater variety of numerical data types than Python. The following table shows most common data types defined in NumPy.\n", | |
"\n", | |
"\n", | |
"\n", | |
"- bool_ - Boolean (True or False) stored as a byte\n", | |
"\n", | |
"\n", | |
"- int_ - Default integer type (same as C long; normally either int64 or int32)\n", | |
"\n", | |
"\n", | |
"- intc - Identical to C int (normally int32 or int64)\n", | |
"\n", | |
"\n", | |
"- intp - Integer used for indexing (same as C ssize_t; normally either int32 or int64)\n", | |
"\n", | |
"\n", | |
"- int8 - Byte (-128 to 127)\n", | |
"\n", | |
"\n", | |
"- int16 - Integer (-32768 to 32767)\n", | |
"\n", | |
"\n", | |
"- int32 - Integer (-2147483648 to 2147483647)\n", | |
"\n", | |
"\t\n", | |
"- int64 - Integer (-9223372036854775808 to 9223372036854775807)\n", | |
"\n", | |
"\n", | |
"- float_ - Shorthand for float64\n", | |
"\n", | |
"\t\n", | |
"- float16 - Half precision float: sign bit, 5 bits exponent, 10 bits mantissa\n", | |
"\n", | |
"\t\n", | |
"- float32 - Single precision float: sign bit, 8 bits exponent, 23 bits mantissa\n", | |
"\n", | |
"\t\n", | |
"- float64 - Double precision float: sign bit, 11 bits exponent, 52 bits mantissa\n", | |
"\n", | |
"\n", | |
"\n", | |
"NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.\n", | |
"\n", | |
"\n", | |
"\n", | |
"A dtype object is constructed using the following syntax:-\n", | |
"\n", | |
"\n", | |
"`numpy.dtype(object, align, copy)`\n", | |
"\n", | |
"\n", | |
"The parameters are:−\n", | |
"\n", | |
"\n", | |
"`Object` − To be converted to data type object.\n", | |
"\n", | |
"\n", | |
"`Align` − If true, adds padding to the field to make it similar to C-struct.\n", | |
"\n", | |
"\n", | |
"`Copy` − Makes a new copy of dtype object. If false, the result is reference to builtin data type object.\n", | |
"\n", | |
"\n", | |
"\n", | |
"Each built-in data type has a character code that uniquely identifies it.\n", | |
"\n", | |
"\n", | |
"'b' − boolean\n", | |
"\n", | |
"'i' − (signed) integer\n", | |
"\n", | |
"'u' − unsigned integer\n", | |
"\n", | |
"'f' − floating-point\n", | |
"\n", | |
"'c' − complex-floating point\n", | |
"\n", | |
"'m' − timedelta\n", | |
"\n", | |
"'M' − datetime\n", | |
"\n", | |
"'O' − (Python) objects\n", | |
"\n", | |
"'S', 'a' − (byte-)string\n", | |
"\n", | |
"'U' − Unicode\n", | |
"\n", | |
"'V' − raw data (void)\n", | |
"\n", | |
"\n", | |
"The following example demonstrate the creation of NumPy data type objects:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int32\n" | |
] | |
} | |
], | |
"source": [ | |
"# using array-scalar type \n", | |
" \n", | |
"dt = np.dtype(np.int32)\n", | |
"\n", | |
"print(dt)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The following examples show the use of structured data type. The field name and the corresponding scalar data type is to be declared." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[('age', 'i1')]\n" | |
] | |
} | |
], | |
"source": [ | |
"# first create structured data type \n", | |
" \n", | |
"dt = np.dtype([('age',np.int8)])\n", | |
"\n", | |
"print(dt) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(10,) (20,) (30,)]\n" | |
] | |
} | |
], | |
"source": [ | |
"# now apply it to ndarray object \n", | |
"\n", | |
"dt = np.dtype([('age',np.int8)]) \n", | |
"\n", | |
"a1 = np.array([(10,),(20,),(30,)], dtype = dt)\n", | |
"\n", | |
"print(a1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10 20 30]\n" | |
] | |
} | |
], | |
"source": [ | |
"# file name can be used to access content of age column \n", | |
"\n", | |
"dt = np.dtype([('age',np.int8)]) \n", | |
"\n", | |
"a1 = np.array([(10,),(20,),(30,)], dtype = dt) \n", | |
"\n", | |
"print(a1['age'])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The following example define a structured data type called student with a string field `name`, an integer field `age` and a float field `marks`. This dtype is applied to ndarray object as follows:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]\n" | |
] | |
} | |
], | |
"source": [ | |
"student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) \n", | |
"\n", | |
"print(student)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(b'abc', 21, 50.) (b'xyz', 18, 75.)]\n" | |
] | |
} | |
], | |
"source": [ | |
"a2 = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) \n", | |
"\n", | |
"print(a2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 9. NumPy array attributes\n", | |
"\n", | |
"\n", | |
"In this section, I will discuss the various NumPy array attributes. The attributes are **ndarray.shape**, **ndarray.size**, **ndarray.ndim**, **ndarray.itemsize**, **ndarray.dtype** and **ndarray.data**." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ndarray.shape\n", | |
"\n", | |
"\n", | |
"This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(2, 3)\n" | |
] | |
} | |
], | |
"source": [ | |
"x1 = np.array([[1,2,3],[4,5,6]]) \n", | |
"\n", | |
"print(x1.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 5)\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the shape of the n1 array\n", | |
"\n", | |
"print(n1.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(2, 5)\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the shape of the n2 array\n", | |
"\n", | |
"print(n2.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(3, 5)\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the shape of the n3 array\n", | |
"\n", | |
"print(n3.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]\n", | |
" [5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# resizes the ndarray \n", | |
"\n", | |
"x2 = np.array([[1,2,3],[4,5,6]])\n", | |
"\n", | |
"x2.shape = (3,2) \n", | |
"\n", | |
"print(x2) " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ndarray.size\n", | |
"\n", | |
"\n", | |
"It returns the total number of elements of the array. This is equal to the product of the elements of shape." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"6\n" | |
] | |
} | |
], | |
"source": [ | |
"# print the size of the x1 array\n", | |
"\n", | |
"print(x1.size)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the size of the n1 array\n", | |
"\n", | |
"print(n1.size)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the size of the n2 array\n", | |
"\n", | |
"print(n2.size)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"15\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the size of the n3 array\n", | |
"\n", | |
"print(n3.size)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### reshape function\n", | |
"\n", | |
"\n", | |
"NumPy also provides a reshape function to resize an array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]\n", | |
" [5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"x1 = np.array([[1,2,3],[4,5,6]])\n", | |
"\n", | |
"x1_reshaped = x1.reshape(3,2)\n", | |
"\n", | |
"print(x1_reshaped)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 8 51 6]\n", | |
" [ 0 0 18]\n", | |
" [33 0 0]\n", | |
" [ 0 14 33]\n", | |
" [ 1 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# reshape the n3 array\n", | |
"\n", | |
"n3 = n3.reshape(5,3)\n", | |
"\n", | |
"print(n3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can see that the size of the n3 array has changed and accordingly n3 array has also changed." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ndarray.ndim\n", | |
"\n", | |
"\n", | |
"This array attribute returns the number of axes (dimensions) of the array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create an array of evenly spaced numbers \n", | |
"\n", | |
"x3 = np.arange(24) \n", | |
"\n", | |
"print(x3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"# confirm that the above array is a one dimensional array \n", | |
" \n", | |
"x3 = np.arange(24) \n", | |
"\n", | |
"print(x3.ndim) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[[ 0 1 2]\n", | |
" [ 3 4 5]\n", | |
" [ 6 7 8]\n", | |
" [ 9 10 11]]\n", | |
"\n", | |
" [[12 13 14]\n", | |
" [15 16 17]\n", | |
" [18 19 20]\n", | |
" [21 22 23]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# reshape the above array\n", | |
"\n", | |
"x4 = x3.reshape(2,4,3) \n", | |
"\n", | |
"print(x4)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"x4 have three dimensions." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the dimension of n1 array\n", | |
"\n", | |
"print(n1.ndim)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can see that the dimension of the n1 array is 2." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ndarray.itemsize\n", | |
"\n", | |
"\n", | |
"This array attribute returns the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"# dtype of array is int8 (1 byte) \n", | |
" \n", | |
"x5 = np.array([1,2,3,4,5], dtype = np.int8)\n", | |
"\n", | |
"print(x5.itemsize)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4\n" | |
] | |
} | |
], | |
"source": [ | |
"# dtype of array is now float32 (4 bytes) \n", | |
"\n", | |
"x6 = np.array([1,2,3,4,5], dtype = np.float32) \n", | |
"\n", | |
"print(x6.itemsize)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ndarray.dtype\n", | |
"\n", | |
"\n", | |
"It represents an object describing the type of the elements in the array. We can create or specify dtype’s using standard Python types. Additionally NumPy provides its own dtypes as follows:-\n", | |
"\n", | |
"\n", | |
"`numpy.int32`, `numpy.int16` and `numpy.float64`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int8\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x5.dtype.name)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"float32\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x6.dtype.name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"float64\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the dtype of the n1 array\n", | |
"\n", | |
"print(n1.dtype.name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int32\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the dtype of the n2 array\n", | |
"\n", | |
"print(n2.dtype.name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int32\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the dtype of the n3 array\n", | |
"\n", | |
"print(n3.dtype.name)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### type of array\n", | |
"\n", | |
"\n", | |
"We can check the type of the array with the `type()` function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"numpy.ndarray" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(x5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"numpy.ndarray" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(x6)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"numpy.ndarray" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# check the type of n1 array\n", | |
"\n", | |
"type(n1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"numpy.ndarray" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# check type of n2 array\n", | |
"\n", | |
"type(n2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"numpy.ndarray" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# check type of n3 array\n", | |
"\n", | |
"type(n3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 10. NumPy array creation \n", | |
"\n", | |
"\n", | |
"A new ndarray object can be constructed by any of the following array creation routines. There are several ways to create arrays. These are listed below:-\n", | |
"\n", | |
"\n", | |
"- array function\n", | |
"\n", | |
"\n", | |
"- empty function\n", | |
"\n", | |
"\n", | |
"- zeros function\n", | |
"\n", | |
"\n", | |
"- ones function\n", | |
"\n", | |
"\n", | |
"- arange function\n", | |
"\n", | |
"\n", | |
"- linspace function" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### array function\n", | |
"\n", | |
"We can create an array from a regular Python list or tuple using the **array** function. The type of the resulting array is deduced from the type of the elements in the sequences." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([2, 3, 4])" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# create an array\n", | |
"\n", | |
"x7 = np.array([2,3,4])\n", | |
"\n", | |
"x7" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dtype('int32')" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# check its dtype\n", | |
"\n", | |
"x7.dtype" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dtype('float64')" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x8 = np.array([1.2, 3.5, 5.1])\n", | |
"\n", | |
"x8.dtype" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### empty function\n", | |
"\n", | |
"\n", | |
"The function **empty** creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.\n", | |
"\n", | |
"\n", | |
"The following code shows an example of an empty array:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[4 0 8]\n", | |
" [0 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"x9 = np.empty([2,3], dtype = int) \n", | |
"\n", | |
"print(x9)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The elements in an array show random values as they are not initialized." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### zeros function\n", | |
"\n", | |
"\n", | |
"The function **zeros** creates an array full of zeros. By default, the dtype of the created array is float.\n", | |
"\n", | |
"The following examples demonstrate the use of the zeros function:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0. 0. 0. 0. 0.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# array of five zeros\n", | |
"\n", | |
"x10 = np.zeros(5) \n", | |
"\n", | |
"print(x10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 0 0 0 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"x11 = np.zeros((5,), dtype = np.int)\n", | |
"\n", | |
"print(x11)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ones function\n", | |
"\n", | |
"\n", | |
"The function **ones** creates an array full of ones. The default dtype is float." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1. 1. 1. 1. 1.]\n" | |
] | |
} | |
], | |
"source": [ | |
"x12 = np.ones(5)\n", | |
"\n", | |
"print(x12)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 1]\n", | |
" [1 1]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# dtype can also be specified\n", | |
"\n", | |
"x13 = np.ones([2,2], dtype=int)\n", | |
"\n", | |
"print(x13)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### arange function\n", | |
"\n", | |
"\n", | |
"The **arange** function creates sequences of numbers. It is analogous to range that returns arrays instead of lists." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10 12 14 16 18]\n" | |
] | |
} | |
], | |
"source": [ | |
"x14 = np.arange(10, 20, 2)\n", | |
"\n", | |
"print(x14)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10. 10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. 15.5 16. 16.5\n", | |
" 17. 17.5 18. 18.5 19. 19.5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# arange function with float arguments\n", | |
"\n", | |
"x15 = np.arange(10, 20, 0.5)\n", | |
"\n", | |
"print(x15)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"When **arange** function is used with floating point arguments, it is not possible to predict the number of elements obtained, due to the finite floating point precision. So, it is better to use the **linspace** function that accepts the number of elements argument that we want, instead of the step." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### linspace function\n", | |
"\n", | |
"\n", | |
"It returns evenly spaced numbers over a specified interval. The endpoint of the interval can optionally be excluded :-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2. 2.5 3. 3.5 4. ]\n" | |
] | |
} | |
], | |
"source": [ | |
"x16 = np.linspace(2.0, 4.0, num=5)\n", | |
"\n", | |
"print(x16)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2. 2.4 2.8 3.2 3.6]\n" | |
] | |
} | |
], | |
"source": [ | |
"x17 = np.linspace(2.0, 4.0, num=5, endpoint=False)\n", | |
"\n", | |
"print(x17)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 11. NumPy array from existing data\n", | |
"\n", | |
"\n", | |
"\n", | |
"In this section, I will discuss how to create an array from existing data." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.asarray\n", | |
"\n", | |
"\n", | |
"This function is similar to numpy.array except for the fact that it has fewer parameters. This routine is useful for converting Python sequence into ndarray.\n", | |
"\n", | |
"\n", | |
"The syntax of this function is as follows:-\n", | |
"\n", | |
"\n", | |
"`numpy.asarray(a, dtype = None, order = None)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"The constructor takes the following parameters.\n", | |
"\n", | |
"\n", | |
"- a - Input data in any form such as list, list of tuples, tuples, tuple of tuples or tuple of lists.\n", | |
"\n", | |
"\t\n", | |
"\n", | |
"- dtype - By default, the data type of input data is applied to the resultant ndarray.\n", | |
"\n", | |
"\n", | |
"\t\n", | |
"- order - C (row major) or F (column major). C is default.\n", | |
"\n", | |
"\n", | |
"\n", | |
"The following examples show the use of the asarray function:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"# convert list to ndarray \n", | |
"\n", | |
"u1 = [1,2,3] \n", | |
"\n", | |
"v1 = np.asarray(u1)\n", | |
"\n", | |
"print(v1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1. 2. 3.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# dtype is set \n", | |
"\n", | |
"u1 = [1,2,3] \n", | |
"\n", | |
"v1 = np.asarray(u1, dtype=float)\n", | |
"\n", | |
"print(v1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"# ndarray from tuple\n", | |
"\n", | |
"u2 = (1,2,3) \n", | |
"\n", | |
"v2 = np.asarray(u2)\n", | |
"\n", | |
"print(v2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(1, 2, 3) (4, 5)]\n" | |
] | |
} | |
], | |
"source": [ | |
"# ndarray from list of tuples \n", | |
"\n", | |
"u3 = [(1,2,3),(4,5)]\n", | |
"\n", | |
"v3 = np.asarray(u3) \n", | |
"\n", | |
"print(v3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.frombuffer\n", | |
"\n", | |
"\n", | |
"This function interprets a buffer as one-dimensional array. Any object that exposes the buffer interface is used as parameter to return an ndarray. Its syntax is as follows:-\n", | |
"\n", | |
"\n", | |
"`numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"The constructor takes the following parameters.\n", | |
"\n", | |
"\n", | |
"\t\n", | |
"- buffer - Any object that exposes buffer interface\n", | |
"\n", | |
"\t\n", | |
" \n", | |
"- dtype - Data type of returned ndarray. Defaults to float\n", | |
"\n", | |
"\n", | |
"\n", | |
"- count - The number of items to read, default -1 means all data\n", | |
"\n", | |
"\n", | |
"\n", | |
"- offset - The starting position to read from. Default is 0\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### numpy.fromiter\n", | |
"\n", | |
"\n", | |
"\n", | |
"This function builds an ndarray object from any iterable object. A new one-dimensional array is returned by this function.\n", | |
"\n", | |
"\n", | |
"Its syntax is as follows:-\n", | |
"\n", | |
"\n", | |
"`numpy.fromiter(iterable, dtype, count = -1)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"The constructor takes the following parameters.\n", | |
"\n", | |
"\n", | |
"\t\n", | |
"- iterable - Any iterable object\n", | |
"\n", | |
"\t\n", | |
"- dtype - Data type of resultant array\n", | |
"\n", | |
"\t\n", | |
"- count - The number of items to be read from iterator. Default is -1 which means all data to be read." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 12. Numpy array from numerical ranges\n", | |
"\n", | |
"\n", | |
"\n", | |
"In this section, I will discuss how to create an array from numerical ranges.\n", | |
"\n", | |
"\n", | |
"\n", | |
"### numpy.arange\n", | |
"\n", | |
"\n", | |
"This function returns an ndarray object containing evenly spaced values within a given range. The syntax of the function is as follows −\n", | |
"\n", | |
"\n", | |
"\n", | |
"`numpy.arange(start, stop, step, dtype)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"The description of the parameters is as follows:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"- start - The start of an interval. If omitted, defaults to 0\n", | |
"\n", | |
"\n", | |
"\t\n", | |
"- stop - The end of an interval (not including this number)\n", | |
"\n", | |
"\n", | |
"\n", | |
"- step - Spacing between values, default is 1\n", | |
"\n", | |
"\n", | |
"\n", | |
"- dtype - Data type of resulting ndarray. If not given, data type of input is used.\n", | |
"\n", | |
"\n", | |
"\n", | |
"The following examples demonstrate the use of this function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 1 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create an array\n", | |
"\n", | |
"y1 = np.arange(5) \n", | |
"\n", | |
"print(y1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0. 1. 2. 3. 4.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# dtype set\n", | |
"\n", | |
"y1 = np.arange(5, dtype = float)\n", | |
"\n", | |
"print(y1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10 12 14 16 18]\n" | |
] | |
} | |
], | |
"source": [ | |
"# start and stop parameters set\n", | |
"\n", | |
"y1 = np.arange(10,20,2) \n", | |
"\n", | |
"print(y1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.linspace\n", | |
"\n", | |
"\n", | |
"\n", | |
"This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified. The syntax of this function is as follows:−\n", | |
"\n", | |
"\n", | |
"\n", | |
"`numpy.linspace(start, stop, num, endpoint, retstep, dtype)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"The description of the parameters is as follows:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"- start - The starting value of the sequence\n", | |
"\n", | |
"\t\n", | |
"- stop - The end value of the sequence, included in the sequence if endpoint set to true\n", | |
"\n", | |
"\t\n", | |
"- num - The number of evenly spaced samples to be generated. Default is 50.\n", | |
"\n", | |
"\n", | |
"- endpoint - True by default, hence the stop value is included in the sequence. If false, it is not included.\n", | |
"\n", | |
"\n", | |
"- retstep - If true, returns samples and step between the consecutive numbers\n", | |
"\n", | |
"\n", | |
"- dtype - Data type of output ndarray\n", | |
"\n", | |
"\n", | |
"\n", | |
"The following examples demonstrate the use of linspace function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10. 12.5 15. 17.5 20. ]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create an array\n", | |
"\n", | |
"y2 = np.linspace(10,20,5) \n", | |
"\n", | |
"print(y2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10. 12. 14. 16. 18.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# endpoint set to false \n", | |
"\n", | |
"y2 = np.linspace(10,20, 5, endpoint = False) \n", | |
"\n", | |
"print(y2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.logspace\n", | |
"\n", | |
"\n", | |
"This function returns an ndarray object that contains the numbers that are evenly spaced on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10. Its syntax as follows:-\n", | |
"\n", | |
"\n", | |
"`numpy.logspace(start, stop, num, endpoint, base, dtype)`\n", | |
"\n", | |
"\n", | |
"The description of the parameters is as follows:-\n", | |
"\n", | |
"\n", | |
"\t\n", | |
"- start - The starting point of the sequence is basestart\n", | |
"\n", | |
"\t\n", | |
"- stop - The final value of sequence is basestop\n", | |
"\n", | |
"\t\n", | |
"- num - The number of values between the range. Default is 50\n", | |
"\n", | |
"\t\n", | |
"- endpoint - If true, stop is the last value in the range\n", | |
"\n", | |
"\t\n", | |
"- base - Base of log space, default is 10\n", | |
"\n", | |
"\t\n", | |
"- dtype - Data type of output array. If not given, it depends upon other input arguments" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 13. NumPy array manipulation\n", | |
"\n", | |
"\n", | |
"\n", | |
"NumPy package provides several routines for manipulation of elements in ndarray object. These routines can be classified into the following types:−\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Changing shape\n", | |
"\n", | |
"\n", | |
"- **reshape** - gives a new shape to an array without changing its data\n", | |
"\n", | |
"\n", | |
"- **flat** - A 1-D iterator over the array\n", | |
"\n", | |
"\n", | |
"- **flatten** - returns a copy of the array collapsed into one dimension\n", | |
"\n", | |
"\n", | |
"- **ravel** - returns a contiguous flattened array\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Transpose operations\n", | |
"\n", | |
"\n", | |
"\n", | |
"- **transpose** - permutes the dimensions of an array\n", | |
"\n", | |
"\n", | |
"- **ndarray.T** - same as self.transpose()\n", | |
"\n", | |
"\n", | |
"- **rollaxis** - rolls the specified axis backwards\n", | |
"\n", | |
"\n", | |
"- **swapaxes** - interchanges the two axes of an array\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Changing dimensions\n", | |
"\n", | |
"\n", | |
"- **broadcast** - produces an object that mimics broadcasting\n", | |
"\n", | |
"\n", | |
"- **broadcast_to** - broadcasts an array to a new shape\n", | |
"\n", | |
"\n", | |
"- **expand_dims** - expands the shape of an array\n", | |
"\n", | |
"\n", | |
"- **squeeze** - removes single-dimensional entries from the shape of an array\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Joining arrays\n", | |
"\n", | |
"\n", | |
"- **concatenate** - joins a sequence of arrays along an existing axis\n", | |
"\n", | |
"\n", | |
"- **stack** - joins a sequence of arrays along a new axis\n", | |
"\n", | |
"\n", | |
"- **hstack** - stacks arrays in sequence horizontally (column wise)\n", | |
"\n", | |
"\n", | |
"- **vstack** - stacks arrays in sequence vertically (row wise)\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Splitting arrays\n", | |
"\n", | |
"\n", | |
"- **split** - splits an array into multiple sub-arrays\n", | |
"\n", | |
"\n", | |
"- **hsplit** - splits an array into multiple sub-arrays horizontally (column-wise)\n", | |
"\n", | |
"\n", | |
"- **vsplit** - splits an array into multiple sub-arrays vertically (row-wise)\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Adding or removing elements\n", | |
"\n", | |
"\n", | |
"\n", | |
"- **resize** - returns a new array with the specified shape\n", | |
"\n", | |
"\n", | |
"- **append** - appends the values to the end of an array\n", | |
"\n", | |
"\n", | |
"- **insert** - inserts the values along the given axis before the given indices\n", | |
"\n", | |
"\n", | |
"- **delete** - returns a new array with sub-arrays along an axis deleted\n", | |
"\n", | |
"\n", | |
"- **unique** - finds the unique elements of an array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# reshape the n3 array back to its original shape\n", | |
"\n", | |
"n3 = n3.reshape(3,5)\n", | |
"\n", | |
"print(n3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 14. NumPy indexing and slicing\n", | |
"\n", | |
"\n", | |
"\n", | |
"In NumPy, the elements of ndarray object can be accessed and modified by indexing or slicing, just like in Python. The items \n", | |
"in ndarray object follows zero-based index. \n", | |
"\n", | |
"\n", | |
"Three types of indexing methods are available − \n", | |
"\n", | |
"\n", | |
"- 1. **field access** \n", | |
"\n", | |
"\n", | |
"- 2. **basic slicing** \n", | |
"\n", | |
"\n", | |
"- 3. **advanced indexing**\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Basic slicing\n", | |
"\n", | |
"\n", | |
"Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of an array.\n", | |
"\n", | |
"\n", | |
"The following examples illustrate the idea:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 1 2 3 4 5 6 7 8 9]\n" | |
] | |
} | |
], | |
"source": [ | |
"x18 = np.arange(10) \n", | |
"\n", | |
"print(x18)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 4 6]\n" | |
] | |
} | |
], | |
"source": [ | |
"s = slice(2,7,2) \n", | |
"\n", | |
"print(x18[s])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In the above example, an ndarray object is created by **arange()** function. Then a slice object is defined with start, stop \n", | |
"and step values 2, 7 and 2 respectively. This slice object is then passed to the ndarray. A part of the ndarray starting with index 2 up to 7 with a step of 2 is sliced." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The same result can also be obtained by giving the slicing parameters separated by a colon : (start:stop:step) directly to the ndarray object as follows:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 4 6]\n" | |
] | |
} | |
], | |
"source": [ | |
"x19 = x18[2:7:2]\n", | |
"\n", | |
"print(x19)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If only one parameter is put, a single item corresponding to the index will be returned." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice single item \n", | |
"\n", | |
"x18 = np.arange(10) \n", | |
"\n", | |
"x20 = x18[5] \n", | |
"\n", | |
"print(x20)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If a : is inserted in front of it, all items from that index onwards will be extracted." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 3 4 5 6 7 8 9]\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice items starting from index \n", | |
"\n", | |
"x18 = np.arange(10)\n", | |
"\n", | |
"print(x18[2:])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If two parameters (with : between them) is used, items between the two indexes (not including the stop index) with default step one are sliced." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice items between indexes \n", | |
"\n", | |
"x18 = np.arange(10) \n", | |
"\n", | |
"print(x18[2:5])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The above description applies to multi-dimensional ndarray too." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2 3]\n", | |
" [3 4 5]\n", | |
" [4 5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"x20 = np.array([[1,2,3],[3,4,5],[4,5,6]])\n", | |
"\n", | |
"print(x20) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[3 4 5]\n", | |
" [4 5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice items starting from index\n", | |
"\n", | |
"print(x20[1:])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 4 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice array of items in the second column \n", | |
" \n", | |
"print(x20[...,1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[3 4 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice all items from the second row \n", | |
"\n", | |
"print(x20[1,...]) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[2 3]\n", | |
" [4 5]\n", | |
" [5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# slice all items from column 1 onwards \n", | |
" \n", | |
"print(x20[...,1:])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 15. NumPy Broadcasting\n", | |
"\n", | |
"\n", | |
"\n", | |
"In NumPy, **broadcasting** refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed.\n", | |
"\n", | |
"\n", | |
"However, if the dimensions of two arrays are dissimilar, element-to-element operations are not possible. Operations on arrays \n", | |
"of different shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is broadcast to the size of the larger array so that they have compatible shapes.\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Rules of broadcasting\n", | |
"\n", | |
"\n", | |
"\n", | |
"Rules of broadcasting are as follows:- \n", | |
"\n", | |
"\n", | |
"\n", | |
"- 1. Array with smaller ndim than the other is prepended with '1' in its shape.\n", | |
"\n", | |
"\n", | |
"\n", | |
"- 2. Size in each dimension of the output shape is maximum of the input sizes in that dimension.\n", | |
"\n", | |
"\n", | |
"- 3. An input can be used in calculation, if its size in a particular dimension matches the output size or its value is exactly 1.\n", | |
"\n", | |
"\n", | |
"- 4. If an input has a dimension size of 1, the first data entry in that dimension is used for all calculations along that dimension.\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"### Broadcast arrays\n", | |
"\n", | |
"\n", | |
"\n", | |
"A set of arrays is said to be broadcastable if the above rules produce a valid result and one of the following is true:−\n", | |
"\n", | |
"\n", | |
"- 1. Arrays have exactly the same shape.\n", | |
"\n", | |
"\n", | |
"- 2. Arrays have the same number of dimensions and the length of each dimension is either a common length or 1.\n", | |
"\n", | |
"\n", | |
"- 3. Array having too few dimensions can have its shape prepended with a dimension of length 1, so that the above stated property is true.\n", | |
"\n", | |
"\n", | |
"\n", | |
"The following program demonstrates an example of broadcasting." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"First array:\n", | |
"[[ 0. 0. 0.]\n", | |
" [10. 10. 10.]\n", | |
" [20. 20. 20.]\n", | |
" [30. 30. 30.]]\n", | |
"\n", | |
"\n", | |
"The dimension of first array is 2\n", | |
"\n", | |
"\n", | |
"Second array:\n", | |
"[1. 2. 3.]\n", | |
"\n", | |
"\n", | |
"The dimension of second array is 1\n", | |
"\n", | |
"\n", | |
"First Array + Second Array\n", | |
"[[ 1. 2. 3.]\n", | |
" [11. 12. 13.]\n", | |
" [21. 22. 23.]\n", | |
" [31. 32. 33.]]\n", | |
"\n", | |
"\n", | |
"The dimension of first array + second array is 2\n" | |
] | |
} | |
], | |
"source": [ | |
"l1 = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])\n", | |
"\n", | |
"l2 = np.array([1.0,2.0,3.0]) \n", | |
" \n", | |
"print('First array:')\n", | |
"print(l1)\n", | |
"print('\\n')\n", | |
"print(\"The dimension of first array is {}\".format(l1.ndim))\n", | |
"print('\\n')\n", | |
" \n", | |
"print('Second array:') \n", | |
"print(l2)\n", | |
"print('\\n')\n", | |
"print(\"The dimension of second array is {}\".format(l2.ndim))\n", | |
"print('\\n') \n", | |
" \n", | |
"print('First Array + Second Array')\n", | |
"print(l1 + l2)\n", | |
"print('\\n')\n", | |
"print(\"The dimension of first array + second array is {}\".format((l1+l2).ndim))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can see that the two arrays have different dimensions. However, due to broadcasting property we can still add the two arrays elementwise." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 5)\n", | |
"[[ 8.2 51. 6.7 0. 0. ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# print n1 array and check its shape\n", | |
"\n", | |
"print(n1.shape)\n", | |
"\n", | |
"print(n1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(2, 5)\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# print n2 array and check its shape\n", | |
"\n", | |
"print(n2.shape)\n", | |
"\n", | |
"print(n2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(2, 5)\n", | |
"[[ 16.2 102. 12.7 0. 0. ]\n", | |
" [ 26.2 84. 6.7 0. 0. ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# print (n1+n2) array and check its shape\n", | |
"\n", | |
"print((n1 + n2).shape)\n", | |
"\n", | |
"print(n1 + n2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can see that the two arrays n1 and n2 have different shapes. But, it is still possible to add these arrays. It is due to the\n", | |
"NumPy broadcasting property." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 16. NumPy - Binary Operators\n", | |
"\n", | |
"\n", | |
"\n", | |
"There are several functions available for bitwise operations in NumPy package. These are as follows:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"- **bitwise_and** - Computes bitwise AND operation of array elements\n", | |
"\n", | |
"\n", | |
"- **bitwise_or** - Computes bitwise OR operation of array elements\n", | |
"\n", | |
"\n", | |
"- **invert** - Computes bitwise NOT\n", | |
"\n", | |
"\n", | |
"- **left_shift** - Shifts bits of a binary representation to the left\n", | |
"\n", | |
"\n", | |
"- **right_shift** - Shifts bits of binary representation to the right" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 17.\tNumPy String Functions\n", | |
"\n", | |
"\n", | |
"\n", | |
"The following functions are used to perform vectorized string operations for arrays of dtype numpy.string_ or numpy.unicode_. They are based on the standard string functions in Python's built-in library.\n", | |
"\n", | |
"\n", | |
"- **add()** - Returns element-wise string concatenation for two arrays of str or Unicode\n", | |
"\n", | |
"\n", | |
"- **multiply()** - Returns the string with multiple concatenation, element-wise\n", | |
"\n", | |
"\n", | |
"- **center()** - Returns a copy of the given string with elements centered in a string of specified length\n", | |
"\n", | |
"\n", | |
"- **capitalize()** - Returns a copy of the string with only the first character capitalized\n", | |
"\n", | |
"\n", | |
"- **title()** - Returns the element-wise title cased version of the string or unicode\n", | |
"\n", | |
"\n", | |
"- **lower()** - Returns an array with the elements converted to lowercase\n", | |
"\n", | |
"\n", | |
"- **upper()** - Returns an array with the elements converted to uppercase\n", | |
"\n", | |
"\n", | |
"- **split()** - Returns a list of the words in the string, using separatordelimiter\n", | |
"\n", | |
"\n", | |
"- **splitlines()** - Returns a list of the lines in the element, breaking at the line boundaries\n", | |
"\n", | |
"\n", | |
"- **strip()** - Returns a copy with the leading and trailing characters removed\n", | |
"\n", | |
"\n", | |
"- **join()** - Returns a string which is the concatenation of the strings in the sequence\n", | |
"\n", | |
"\n", | |
"- **replace()** - Returns a copy of the string with all occurrences of substring replaced by the new string\n", | |
"\n", | |
"\n", | |
"- **decode()** - Calls str.decode element-wise\n", | |
"\n", | |
"\n", | |
"- **encode()** - Calls str.encode element-wise\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 18. NumPy Arithmetic Operations\n", | |
"\n", | |
"\n", | |
"Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.\n", | |
"\n", | |
"\n", | |
"Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of \n", | |
"the same shape or should conform to array broadcasting rules." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10 20 30 40 50]\n", | |
"[0 1 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create two arrays\n", | |
"\n", | |
"z1 = np.array([10,20,30,40,50])\n", | |
"\n", | |
"z2 = np.arange(5)\n", | |
"\n", | |
"print(z1)\n", | |
"\n", | |
"print(z2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10 21 32 43 54]\n" | |
] | |
} | |
], | |
"source": [ | |
"# addition\n", | |
"\n", | |
"z_add = z1 + z2\n", | |
"\n", | |
"print(z_add)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[10 19 28 37 46]\n" | |
] | |
} | |
], | |
"source": [ | |
"# subtraction\n", | |
"\n", | |
"z_sub = z1 - z2\n", | |
"\n", | |
"print(z_sub)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 0 20 60 120 200]\n" | |
] | |
} | |
], | |
"source": [ | |
"# multiplication - elementwise product\n", | |
"\n", | |
"z_mult = z1 * z2\n", | |
"\n", | |
"print(z_mult)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 5. 10. 15. 20. 25.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# division\n", | |
"\n", | |
"z_div = z1/2\n", | |
"\n", | |
"print(z_div)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ True, True, True, False, False])" | |
] | |
}, | |
"execution_count": 88, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# comparision operator\n", | |
"\n", | |
"z1 < 35" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Some of the other important arithmetic functions available in NumPy are as follows:-\n", | |
"\n", | |
"\n", | |
"- **numpy.reciprocal()**\n", | |
"\n", | |
"\n", | |
"This function returns the reciprocal of argument, element-wise. For elements with absolute values larger than 1, the result is always 0 because of the way in which Python handles integer division. For integer 0, an overflow warning is issued.\n", | |
"\n", | |
"\n", | |
"- **numpy.power()**\n", | |
"\n", | |
"\n", | |
"This function treats elements in the first input array as base and returns it raised to the power of the corresponding element in the second input array.\n", | |
"\n", | |
"\n", | |
"- **numpy.mod()**\n", | |
"\n", | |
"\n", | |
"This function returns the remainder of division of the corresponding elements in the input array. The function numpy.remainder() also produces the same result.\n", | |
"\n", | |
"\n", | |
"\n", | |
"The following functions are used to perform operations on array with complex numbers.\n", | |
"\n", | |
"\n", | |
"- **numpy.real()** − returns the real part of the complex data type argument.\n", | |
"\n", | |
"\n", | |
"- **numpy.imag()** − returns the imaginary part of the complex data type argument.\n", | |
"\n", | |
"\n", | |
"- **numpy.conj()** − returns the complex conjugate, which is obtained by changing the sign of the imaginary part.\n", | |
"\n", | |
"\n", | |
"- **numpy.angle()** − returns the angle of the complex argument. The function has degree parameter. If true, the angle in the degree is returned, otherwise the angle is in radians." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 19. NumPy Statistical Functions\n", | |
"\n", | |
"\n", | |
"NumPy has various useful statistical functions for finding minimum, maximum, percentile, standard deviation and variance, \n", | |
"from the given elements in the array. \n", | |
"\n", | |
"\n", | |
"These functions are explained as follows:−\n", | |
"\n", | |
"\n", | |
"**numpy.amin()** and **numpy.amax()**\n", | |
"\n", | |
"\n", | |
"These functions return the minimum and the maximum from the elements in the given array along the specified axis.\n", | |
"\n", | |
"\n", | |
"For rows, axis = 1\n", | |
"\n", | |
"For columns, axis = 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[5 2 3]\n", | |
" [1 5 6]\n", | |
" [3 7 4]]\n" | |
] | |
} | |
], | |
"source": [ | |
"i1 = np.array([[5,2,3],[1,5,6],[3,7,4]]) \n", | |
"\n", | |
"print(i1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying amin() function across rows:\n", | |
"[2 1 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying amin() function across rows:') \n", | |
"\n", | |
"print(np.amin(i1,1)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying amin() function across columns:\n", | |
"[1 2 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying amin() function across columns:') \n", | |
"\n", | |
"print(np.amin(i1,0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying amax() function across rows:\n", | |
"[5 6 7]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying amax() function across rows:') \n", | |
"\n", | |
"print(np.amax(i1,1)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 93, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying amax() function across columns:\n", | |
"[5 7 6]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying amax() function across columns:') \n", | |
"\n", | |
"print(np.amax(i1,0))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now, I will apply the amin() and amax() functions to the n3 array as follows:-" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Applying amin() function across rows:\n", | |
"[0 0 0]\n", | |
"\n", | |
"\n", | |
"Applying amin() function across columns:\n", | |
"[ 8 33 0 0 0]\n", | |
"\n", | |
"\n", | |
"Applying amax() function across rows:\n", | |
"[51 33 33]\n", | |
"\n", | |
"\n", | |
"Applying amax() function across columns:\n", | |
"[18 51 6 0 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply amin() and amax() function to n3 array\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying amin() function across rows:')\n", | |
"\n", | |
"print(np.amin(n3,1)) \n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying amin() function across columns:') \n", | |
"\n", | |
"print(np.amin(n3,0))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying amax() function across rows:') \n", | |
"\n", | |
"print(np.amax(n3,1)) \n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying amax() function across columns:') \n", | |
"\n", | |
"print(np.amax(n3,0))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.ptp()\n", | |
"\n", | |
"\n", | |
"\n", | |
"The **numpy.ptp()** function returns the range (maximum-minimum) of values along an axis." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying ptp() function:\n", | |
"6\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying ptp() function:')\n", | |
"\n", | |
"print(np.ptp(i1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying ptp() function along rows:\n", | |
"[3 5 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying ptp() function along rows:')\n", | |
"\n", | |
"print(np.ptp(i1, axis = 1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying ptp() function along columns:\n", | |
"[4 5 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying ptp() function along columns:')\n", | |
"\n", | |
"print(np.ptp(i1, axis = 0))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.percentile()\n", | |
"\n", | |
"\n", | |
"Percentile is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 20 50 80]\n", | |
" [ 30 60 90]\n", | |
" [ 40 70 100]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create an array\n", | |
"\n", | |
"i2 = np.array([[20,50,80],[30,60,90],[40,70,100]]) \n", | |
"\n", | |
"print(i2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying percentile() function:\n", | |
"60.0\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying percentile() function:')\n", | |
"\n", | |
"print(np.percentile(i2,50))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying percentile() function along rows:\n", | |
"[50. 60. 70.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying percentile() function along rows:')\n", | |
"\n", | |
"print(np.percentile(i2,50,axis = 1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying percentile() function along columns:\n", | |
"[30. 60. 90.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying percentile() function along columns:')\n", | |
"\n", | |
"print(np.percentile(i2,50,axis = 0))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.median()\n", | |
"\n", | |
"\n", | |
"\n", | |
"Median is defined as the value separating the higher half of a data sample from the lower half. The **numpy.median()**\n", | |
"function is used as shown in the following program." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 20 50 80]\n", | |
" [ 30 60 90]\n", | |
" [ 40 70 100]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# print i2 array\n", | |
"\n", | |
"print(i2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying median() function:\n", | |
"60.0\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying median() function:')\n", | |
"\n", | |
"print(np.median(i2)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying median() function across rows:\n", | |
"[50. 60. 70.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying median() function across rows:')\n", | |
"\n", | |
"print(np.median(i2, axis=1)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying median() function across columns:\n", | |
"[30. 60. 90.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying median() function across columns:')\n", | |
"\n", | |
"print(np.median(i2, axis=0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 106, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Applying median() function:\n", | |
"1.0\n", | |
"\n", | |
"\n", | |
"Applying median() function across rows:\n", | |
"[6. 0. 1.]\n", | |
"\n", | |
"\n", | |
"Applying median() function across columns:\n", | |
"[30. 60. 90.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply median function to n3 array\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying median() function:')\n", | |
"\n", | |
"print(np.median(n3))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying median() function across rows:')\n", | |
"\n", | |
"print(np.median(n3, axis=1)) \n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying median() function across columns:')\n", | |
"\n", | |
"print(np.median(i2, axis=0))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.mean()\n", | |
"\n", | |
"\n", | |
"Arithmetic mean is the sum of elements along an axis divided by the number of elements. The **numpy.mean()** function returns the arithmetic mean of elements in the array. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2 3]\n", | |
" [3 4 5]\n", | |
" [4 5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"i3 = np.array([[1,2,3],[3,4,5],[4,5,6]]) \n", | |
"\n", | |
"print(i3) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 108, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying mean() function:\n", | |
"3.6666666666666665\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying mean() function:')\n", | |
"\n", | |
"print(np.mean(i3))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 109, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying mean() function along rows:\n", | |
"[2. 4. 5.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying mean() function along rows:')\n", | |
"\n", | |
"print(np.mean(i3, axis = 1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 110, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying mean() function along columns:\n", | |
"[2.66666667 3.66666667 4.66666667]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying mean() function along columns:')\n", | |
"\n", | |
"print(np.mean(i3, axis = 0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Applying mean() function:\n", | |
"10.933333333333334\n", | |
"\n", | |
"\n", | |
"Applying mean() function across rows:\n", | |
"[13. 10.2 9.6]\n", | |
"\n", | |
"\n", | |
"Applying mean() function across columns:\n", | |
"[13.33333333 39. 2.33333333 0. 0. ]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply mean function to n3 array\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying mean() function:')\n", | |
"\n", | |
"print(np.mean(n3))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying mean() function across rows:')\n", | |
"\n", | |
"print(np.mean(n3, axis=1)) \n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying mean() function across columns:')\n", | |
"\n", | |
"print(np.mean(n3, axis=0))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.average()\n", | |
"\n", | |
"\n", | |
"Weighted average is an average resulting from the multiplication of each component by a factor reflecting its importance. \n", | |
"The **numpy.average()** function computes the weighted average of elements in an array according to their respective weight given in another array. The function can have an axis parameter. If the axis is not specified, the array is flattened.\n", | |
"\n", | |
"\n", | |
"Considering an array [1,2,3,4] and corresponding weights [4,3,2,1], the weighted average is calculated by adding the product of the corresponding elements and dividing the sum by the sum of weights.\n", | |
"\n", | |
"\n", | |
"Weighted average = (1*4+2*3+3*2+4*1)/(4+3+2+1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 112, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"# declare an array\n", | |
"i4 = np.array([1,2,3,4]) \n", | |
"\n", | |
"# print the array \n", | |
"print(i4) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 113, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying average() function:\n", | |
"2.5\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying average() function:')\n", | |
"\n", | |
"print(np.average(i4))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying average() function with weights:\n", | |
"2.0\n" | |
] | |
} | |
], | |
"source": [ | |
"# specify weights\n", | |
"\n", | |
"wts = np.array([4,3,2,1]) \n", | |
"\n", | |
"print('Applying average() function with weights:')\n", | |
"\n", | |
"print(np.average(i4,weights = wts))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 115, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Sum of weights\n", | |
"(2.0, 10.0)\n" | |
] | |
} | |
], | |
"source": [ | |
"# Returns the sum of weights, if the returned parameter is set to True.\n", | |
"\n", | |
"print('Sum of weights')\n", | |
"\n", | |
"print(np.average([1,2,3,4],weights = [4,3,2,1], returned = True))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Variance\n", | |
"\n", | |
"\n", | |
"Variance is the average of squared deviations. It is given by **mean(abs(x - x.mean())**2)**.\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 116, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.25\n" | |
] | |
} | |
], | |
"source": [ | |
"# calculation of variance\n", | |
"\n", | |
"i5 = np.array([1,2,3,4])\n", | |
"\n", | |
"print(np.var(i5))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 117, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"240.46222222222215\n" | |
] | |
} | |
], | |
"source": [ | |
"# calculate variance of n3\n", | |
"\n", | |
"print(np.var(n3))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Standard Deviation\n", | |
"\n", | |
"\n", | |
"Standard deviation is the square root of the variance." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 118, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.118033988749895\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.std(i5))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 119, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"15.506844366995567\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.std(n3))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 20. NumPy Sorting\n", | |
"\n", | |
"\n", | |
"NumPy provides various sorting related functions. These sorting functions implement different sorting algorithms and enable us to sort the array as we want. These sorting functions are as follows:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"### numpy.sort()\n", | |
"\n", | |
"\n", | |
"\n", | |
"The **sort()** function returns a sorted copy of the input array. It has the following parameters:−\n", | |
"\n", | |
"\n", | |
"\n", | |
"`numpy.sort(a, axis, kind, order)`\n", | |
"\n", | |
"\n", | |
"\n", | |
"Description of the parameters is as follows:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"- a - Array to be sorted\n", | |
"\n", | |
"\t\n", | |
"- axis - The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis\n", | |
"\n", | |
"\t\n", | |
"- kind - Default is quicksort\n", | |
"\n", | |
"\t\n", | |
"- order - If the array contains fields, the order of fields to be sorted\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 120, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[[9 4]\n", | |
" [2 7]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# declare an array\n", | |
"s1 = np.array([[9,4],[2,7]]) \n", | |
"\n", | |
"print('Our array is:')\n", | |
"\n", | |
"print(s1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 121, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying sort() function along rows:\n", | |
"[[4 9]\n", | |
" [2 7]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# sorting along rows, axis = 1\n", | |
"\n", | |
"print('Applying sort() function along rows:')\n", | |
"\n", | |
"print(np.sort(s1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying sort() function along columns:\n", | |
"[[2 4]\n", | |
" [9 7]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# sorting along columns, axis = 0\n", | |
"\n", | |
"print('Applying sort() function along columns:')\n", | |
"\n", | |
"print(np.sort(s1, axis = 0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Applying sort() function across rows:\n", | |
"[[ 0 0 6 8 51]\n", | |
" [ 0 0 0 18 33]\n", | |
" [ 0 0 1 14 33]]\n", | |
"\n", | |
"\n", | |
"Applying sort() function across columns:\n", | |
"[[ 8 33 0 0 0]\n", | |
" [14 33 1 0 0]\n", | |
" [18 51 6 0 0]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply sort function to n3 array\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying sort() function across rows:')\n", | |
"\n", | |
"print(np.sort(n3)) \n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying sort() function across columns:')\n", | |
"\n", | |
"print(np.sort(n3, axis=0))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.argsort()\n", | |
"\n", | |
"\n", | |
"The **argsort()** function performs an indirect sort on input array, along the given axis and using a specified kind of sort to return the array of indices of data. This indices array is used to construct the sorted array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[5 9 2]\n" | |
] | |
} | |
], | |
"source": [ | |
"s2 = np.array([5,9,2]) \n", | |
"\n", | |
"print('Our array is:')\n", | |
"\n", | |
"print(s2) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 0 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"# applying argsort() to s2\n", | |
"\n", | |
"s3 = np.argsort(s2) \n", | |
"\n", | |
"print(s3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 126, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 5 9]\n" | |
] | |
} | |
], | |
"source": [ | |
"# reconstruct original array in sorted order\n", | |
"\n", | |
"print(s2[s3]) " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.lexsort()\n", | |
"\n", | |
"\n", | |
"The **lexsort()** function performs an indirect sort using a sequence of keys. The keys can be seen as a column in a spreadsheet. The function returns an array of indices, using which the sorted data can be obtained. The last key happens \n", | |
"to be the primary key of sort." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 21. NumPy Searching\n", | |
"\n", | |
"\n", | |
"NumPy package has a number of functions for searching inside an array. These functions enable us to find the maximum, the minimum as well as the elements satisfying a given condition.\n", | |
"\n", | |
"\n", | |
"\n", | |
"### numpy.argmax() and numpy.argmin()\n", | |
"\n", | |
"\n", | |
"These two functions return the indices of maximum and minimum elements respectively along the given axis." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 127, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[[30 40 70]\n", | |
" [80 20 10]\n", | |
" [50 90 60]]\n" | |
] | |
} | |
], | |
"source": [ | |
"d1 = np.array([[30,40,70],[80,20,10],[50,90,60]]) \n", | |
"\n", | |
"print('Our array is:')\n", | |
"\n", | |
"print(d1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 128, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"7\n" | |
] | |
} | |
], | |
"source": [ | |
"# applying argmax() function to d1\n", | |
"\n", | |
"print(np.argmax(d1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 129, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[30 40 70 80 20 10 50 90 60]\n" | |
] | |
} | |
], | |
"source": [ | |
"# index of maximum number in flattened array\n", | |
"\n", | |
"print(d1.flatten())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 130, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"# array containing indices of maximum along axis 0\n", | |
"\n", | |
"maxindex = np.argmax(d1, axis = 0)\n", | |
"\n", | |
"print(maxindex)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 131, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2 0 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"# array containing indices of maximum along axis 1\n", | |
"\n", | |
"maxindex = np.argmax(d1, axis = 1) \n", | |
"\n", | |
"print(maxindex) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 132, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"# applying argmin() function to d1\n", | |
"\n", | |
"minindex = np.argmin(d1)\n", | |
"\n", | |
"print(minindex) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 133, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"# flattened array\n", | |
"\n", | |
"print(d1.flatten()[minindex]) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 134, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 1 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"# flattened array along axis 0\n", | |
"\n", | |
"minindex = np.argmin(d1, axis = 0) \n", | |
"\n", | |
"print(minindex)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 135, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 2 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"# flattened array along axis 1\n", | |
"\n", | |
"minindex = np.argmin(d1, axis = 1)\n", | |
"\n", | |
"print(minindex)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 136, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Applying argmax() function to n3:\n", | |
"1\n", | |
"\n", | |
"\n", | |
"Print array containing indices of maximum along axis 0:\n", | |
"[1 0 0 0 0]\n", | |
"\n", | |
"\n", | |
"Print array containing indices of maximum along axis 1:\n", | |
"[1 1 1]\n", | |
"\n", | |
"\n", | |
"Applying argmin() function to n3:\n", | |
"3\n", | |
"\n", | |
"\n", | |
"Print array containing indices of minimum along axis 0:\n", | |
"[0 1 1 0 0]\n", | |
"\n", | |
"\n", | |
"Print array containing indices of minimum along axis 1:\n", | |
"[3 2 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply argmax and argmin function to n3 array\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying argmax() function to n3:')\n", | |
"\n", | |
"print(np.argmax(n3))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Print array containing indices of maximum along axis 0:')\n", | |
"\n", | |
"print(np.argmax(n3, axis = 0))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Print array containing indices of maximum along axis 1:')\n", | |
"\n", | |
"print(np.argmax(n3, axis = 1))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying argmin() function to n3:')\n", | |
"\n", | |
"print(np.argmin(n3))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Print array containing indices of minimum along axis 0:')\n", | |
"\n", | |
"print(np.argmin(n3, axis = 0))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Print array containing indices of minimum along axis 1:')\n", | |
"\n", | |
"print(np.argmin(n3, axis = 1))\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.nonzero()\n", | |
"\n", | |
"\n", | |
"\n", | |
"The **nonzero()** function returns the indices of non-zero elements in the input array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 137, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[[30 40 70]\n", | |
" [80 20 10]\n", | |
" [50 90 60]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Our array is:')\n", | |
"\n", | |
"print(d1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 138, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(array([0, 0, 0, 1, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 2, 0, 1, 2], dtype=int64))\n" | |
] | |
} | |
], | |
"source": [ | |
"# applying nonzero() function\n", | |
"\n", | |
"print(np.nonzero(d1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 139, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(array([0, 0, 0, 1, 1, 1, 2, 2, 2], dtype=int64),\n", | |
" array([0, 1, 2, 0, 1, 2, 0, 1, 2], dtype=int64))" | |
] | |
}, | |
"execution_count": 139, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# get the indices of non-zero elements \n", | |
"\n", | |
"d1.nonzero()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 140, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([30, 40, 70, 80, 20, 10, 50, 90, 60])" | |
] | |
}, | |
"execution_count": 140, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# get the elements based on above indices\n", | |
"\n", | |
"d1[d1.nonzero()]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 141, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Applying nonzero() function to n3 array:\n", | |
"(array([0, 0, 0, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 0, 1, 2], dtype=int64))\n", | |
"\n", | |
"\n", | |
"The indices of non-zero elements are:\n", | |
"(array([0, 0, 0, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 0, 1, 2], dtype=int64))\n", | |
"\n", | |
"\n", | |
"The elements based on above indices are:\n", | |
"[ 8 51 6 18 33 14 33 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply nonzero function to n3 array\n", | |
"\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Applying nonzero() function to n3 array:')\n", | |
"\n", | |
"print(np.nonzero(n3))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('The indices of non-zero elements are:')\n", | |
"\n", | |
"print(n3.nonzero())\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('The elements based on above indices are:')\n", | |
"\n", | |
"print(n3[n3.nonzero()])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.where()\n", | |
"\n", | |
"The **where()** function returns the indices of elements in an input array where the given condition is satisfied.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 142, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[[0. 1. 2.]\n", | |
" [3. 4. 5.]\n", | |
" [6. 7. 8.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"d2 = np.arange(9.).reshape(3, 3) \n", | |
"\n", | |
"print('Our array is:')\n", | |
"\n", | |
"print(d2) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 143, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Indices of elements > 3\n", | |
"(array([1, 1, 2, 2, 2], dtype=int64), array([1, 2, 0, 1, 2], dtype=int64))\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Indices of elements > 3')\n", | |
"\n", | |
"d3 = np.where(d2 > 3) \n", | |
"\n", | |
"print(d3) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 144, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Use these indices to get elements satisfying the condition\n", | |
"[4. 5. 6. 7. 8.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Use these indices to get elements satisfying the condition')\n", | |
"\n", | |
"\n", | |
"print(d2[d3])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 145, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Indices of elements > 10\n", | |
"(array([0, 1, 1, 2, 2], dtype=int64), array([1, 0, 1, 0, 1], dtype=int64))\n", | |
"\n", | |
"\n", | |
"Use these indices to get elements satisfying the condition\n", | |
"[51 18 33 14 33]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply where function to n3 array\n", | |
"\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Indices of elements > 10')\n", | |
"\n", | |
"print(np.where(n3 > 10))\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"print('Use these indices to get elements satisfying the condition')\n", | |
"\n", | |
"print(n3[np.where(n3 > 10)])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### numpy.extract()\n", | |
"\n", | |
"\n", | |
"The **extract()** function returns the elements satisfying any condition." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 146, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[[0. 1. 2.]\n", | |
" [3. 4. 5.]\n", | |
" [6. 7. 8.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"e1 = np.arange(9.).reshape(3, 3) \n", | |
"\n", | |
"print('Our array is:')\n", | |
"\n", | |
"print(e1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 147, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# define a condition \n", | |
"\n", | |
"condition = np.mod(e1,2) == 0 " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 148, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Element-wise value of condition\n", | |
"[[ True False True]\n", | |
" [False True False]\n", | |
" [ True False True]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# evaluate the condition\n", | |
"\n", | |
"print('Element-wise value of condition')\n", | |
"\n", | |
"print(condition)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 149, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Extract elements using condition\n", | |
"[0. 2. 4. 6. 8.]\n" | |
] | |
} | |
], | |
"source": [ | |
"# extract corresponding elements\n", | |
"\n", | |
"print('Extract elements using condition')\n", | |
"\n", | |
"print(np.extract(condition, e1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 150, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The array n3 is:\n", | |
"[[ 8 51 6 0 0]\n", | |
" [18 33 0 0 0]\n", | |
" [14 33 1 0 0]]\n", | |
"\n", | |
"\n", | |
"Element-wise value of condition\n", | |
"[[ True False True True True]\n", | |
" [ True False True True True]\n", | |
" [ True False False True True]]\n", | |
"\n", | |
"\n", | |
"Extract elements using condition\n", | |
"[ 8 6 0 0 18 0 0 0 14 0 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"# apply extract function to n3 array\n", | |
"\n", | |
"\n", | |
"print('The array n3 is:')\n", | |
"\n", | |
"print(n3)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"# define a condition \n", | |
"\n", | |
"condition = np.mod(n3,2) == 0 \n", | |
"\n", | |
"# evaluate the condition\n", | |
"\n", | |
"print('Element-wise value of condition')\n", | |
"\n", | |
"print(condition)\n", | |
"\n", | |
"print('\\n')\n", | |
"\n", | |
"# extract corresponding elements\n", | |
"\n", | |
"print('Extract elements using condition')\n", | |
"\n", | |
"print(np.extract(condition, n3))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 22. NumPy Copies and Views\n", | |
"\n", | |
"\n", | |
"\n", | |
"While executing the functions, some of them return a copy of the input array, while some return the view. When the contents are physically stored in another location, it is called **Copy**. If on the other hand, a different view of the same memory content is provided, we call it as **View**.\n", | |
"\n", | |
"\n", | |
"### No Copy\n", | |
"\n", | |
"\n", | |
"Simple assignments do not make the copy of array object. Instead, it uses the same id() of the original array to access it. The **id()** returns a universal identifier of Python object, similar to the pointer in C.\n", | |
"\n", | |
"\n", | |
"Any changes in either gets reflected in the other too. For example, changing the shape of one will change the shape of the other too." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 151, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Our array is:\n", | |
"[0 1 2 3 4 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# declare an array\n", | |
"\n", | |
"a10 = np.arange(6) \n", | |
"\n", | |
"print('Our array is:')\n", | |
"\n", | |
"print(a10) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 152, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applying id() function:\n", | |
"251290520432\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Applying id() function:')\n", | |
"\n", | |
"\n", | |
"print(id(a10)) \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a10 is assigned to b10:\n", | |
"[0 1 2 3 4 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('a10 is assigned to b10:')\n", | |
"\n", | |
"b10 = a10\n", | |
"\n", | |
"print(b10) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b10 has same id():\n", | |
"251290520432\n" | |
] | |
} | |
], | |
"source": [ | |
"print('b10 has same id():')\n", | |
"\n", | |
"\n", | |
"print(id(b10))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 155, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Change shape of b10:\n", | |
"[[0 1]\n", | |
" [2 3]\n", | |
" [4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Change shape of b10:')\n", | |
"\n", | |
"b10.shape = 3,2 \n", | |
"\n", | |
"print(b10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 156, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Shape of a also gets changed:\n", | |
"[[0 1]\n", | |
" [2 3]\n", | |
" [4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Shape of a also gets changed:')\n", | |
"\n", | |
"print(a10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### View or Shallow Copy\n", | |
"\n", | |
"\n", | |
"NumPy has a **view()** method which is a new array object that looks at the same data of the original array. In this case, change in dimensions of the new array doesn't change dimensions of the original." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 157, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0 1]\n", | |
" [2 3]\n", | |
" [4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# declare an array\n", | |
"\n", | |
"x1 = np.arange(6).reshape(3,2) \n", | |
"\n", | |
"print(x1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 158, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0 1]\n", | |
" [2 3]\n", | |
" [4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create view of x1\n", | |
"\n", | |
"y1 = x1.view() \n", | |
"\n", | |
"print(y1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 159, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"id() of x1:\n", | |
"251290525904\n", | |
"\n", | |
"\n", | |
"id() of y1:\n", | |
"251290520272\n", | |
"\n", | |
"\n", | |
"id() for both the arrays are different\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the ids of both arrays\n", | |
"\n", | |
"print('id() of x1:')\n", | |
"print(id(x1))\n", | |
"print('\\n')\n", | |
"print('id() of y1:') \n", | |
"print(id(y1)) \n", | |
"print('\\n')\n", | |
"print('id() for both the arrays are different') " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 160, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Shape of y1:\n", | |
"(3, 2)\n", | |
"\n", | |
"\n", | |
"Shape of x1:\n", | |
"(3, 2)\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the shape of both the arrays\n", | |
"\n", | |
"print('Shape of y1:') \n", | |
"print(y1.shape) \n", | |
"print('\\n')\n", | |
"print('Shape of x1:') \n", | |
"print(x1.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 161, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0 1 2]\n", | |
" [3 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Change the shape of y1\n", | |
"\n", | |
"y1.shape = 2,3 \n", | |
"\n", | |
"print(y1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 162, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0 1]\n", | |
" [2 3]\n", | |
" [4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# check the shape of x1\n", | |
"\n", | |
"x1.shape\n", | |
"\n", | |
"print(x1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can see that changing the shape of y1 does not change the shape of x1." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Deep Copy\n", | |
"\n", | |
"\n", | |
"The **copy()** function creates a deep copy. It is a complete copy of the array and its data, and doesn't share with the original array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 163, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Array z1 is:\n", | |
"[[10 10]\n", | |
" [ 2 3]\n", | |
" [ 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# declare an array\n", | |
"\n", | |
"z1 = np.array([[10,10], [2,3], [4,5]]) \n", | |
"\n", | |
"print('Array z1 is:')\n", | |
"\n", | |
"print(z1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 164, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Array y1 is:\n", | |
"[[10 10]\n", | |
" [ 2 3]\n", | |
" [ 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# create a deep copy of z1\n", | |
"\n", | |
"y1 = z1.copy()\n", | |
"\n", | |
"print('Array y1 is:')\n", | |
"\n", | |
"print(y1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 165, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Can we write y1 is z1\n", | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"# y1 does not share any memory of x1\n", | |
"\n", | |
"print('Can we write y1 is z1')\n", | |
"\n", | |
"print(y1 is z1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 166, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Modified array y1:\n", | |
"[[100 10]\n", | |
" [ 2 3]\n", | |
" [ 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# change the contents of y1\n", | |
"\n", | |
"y1[0,0] = 100 \n", | |
"\n", | |
"print('Modified array y1:')\n", | |
"\n", | |
"print(y1) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 167, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[10 10]\n", | |
" [ 2 3]\n", | |
" [ 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# z1 remains unchanged\n", | |
"\n", | |
"print(z1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 23. Input Output with NumPy\n", | |
"\n", | |
"\n", | |
"\n", | |
"The ndarray objects can be saved to and loaded from the disk files. The input output functions available are as follows:−\n", | |
"\n", | |
"\n", | |
"- **load()** and **save()** functions handle /numPy binary files (with npy extension).\n", | |
"\n", | |
"\n", | |
"- **loadtxt()** and **savetxt()** functions handle normal text files.\n", | |
"\n", | |
"\n", | |
"\n", | |
"NumPy introduces a simple file format for ndarray objects. It is called **.npy file**. This .npy file stores data, shape, dtype and other information required to reconstruct the ndarray in a disk file. The array is correctly retrieved even if the file is on another machine with different architecture.\n", | |
"\n", | |
"\n", | |
"### numpy.save()\n", | |
"\n", | |
"\n", | |
"The **numpy.save()** file stores the input array in a disk file with npy extension." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"To reconstruct array from **outfile.npy**, use **load()** function." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The **save()** and **load()** functions accept an additional Boolean parameter allow_pickles. A pickle in Python is used to serialize and de-serialize objects before saving to or reading from a disk file." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### savetxt()\n", | |
"\n", | |
"\n", | |
"The storage and retrieval of array data in simple text file format is done with **savetxt()** and **loadtxt()** functions." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The **savetxt()** and **loadtxt()** functions accept additional optional parameters such as header, footer, and delimiter." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 24. Random sampling with NumPy\n", | |
"\n", | |
"\n", | |
"NumPy provides `random` module for doing random sampling. This `random` module contains many useful functions for generation of random numbers. \n", | |
"\n", | |
"\n", | |
"There are several functions to generate simple random data. These functions are described below:-\n", | |
"\n", | |
"\n", | |
"\n", | |
"### rand() \n", | |
"\n", | |
"\n", | |
"This function creates an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 168, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.38587771, 0.74120681],\n", | |
" [0.7136804 , 0.30090895],\n", | |
" [0.57519453, 0.3636873 ]])" | |
] | |
}, | |
"execution_count": 168, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.random.rand(3,2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### randn() \n", | |
"\n", | |
"\n", | |
"It returns a sample (or samples) from the \"standard normal\" distribution." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 169, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.34949135496369865" | |
] | |
}, | |
"execution_count": 169, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.random.randn()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### randint()\n", | |
"\n", | |
"\n", | |
"It returns random integers from low (inclusive) to high (exclusive)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 170, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[2, 3, 3, 3],\n", | |
" [1, 2, 3, 2]])" | |
] | |
}, | |
"execution_count": 170, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.random.randint(5, size=(2,4))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### random()\n", | |
"\n", | |
"\n", | |
"It returns random floats in the half-open interval [0.0, 1.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 171, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.6099418509631628" | |
] | |
}, | |
"execution_count": 171, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.random.random_sample()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### choice()\n", | |
"\n", | |
"\n", | |
"It generates a random sample from a given 1-D array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 172, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 0, 0])" | |
] | |
}, | |
"execution_count": 172, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# generates a uniform random sample from np.arange(5) of size 3\n", | |
"\n", | |
"np.random.choice(5, 3)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment