Skip to content

Instantly share code, notes, and snippets.

@yogesh-aggarwal
Created July 8, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogesh-aggarwal/95b1a14c0366ae390ef2e4b5e52d8519 to your computer and use it in GitHub Desktop.
Save yogesh-aggarwal/95b1a14c0366ae390ef2e4b5e52d8519 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ✔ CREATING NUMPY ARRAYS "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### FROM PYTHON OBJECT\n",
"**From lists, tuples, dictionaries etc.**"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([['Yogesh', 'Sakshi', 'Pankaj'],\n",
" ['Saksham', 'Sahil', 'Sandesh']], dtype='<U7')"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]], np.str)\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## FROM INBUILT FUNCTIONS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### np.zeros()\n",
"**➡ Returns a null matrix of the provided order.**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0.],\n",
" [0., 0., 0., 0.]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.zeros([2, 4])\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### np.arange()\n",
"**➡ Returns a 1-D array(matrix) with elements as numbers till the argument provided.**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.arange(15)\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### np.empty()\n",
"**➡ Returns a null matrix of the provided order.**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[8.78864560e-312, 3.16202013e-322, 0.00000000e+000],\n",
" [0.00000000e+000, 1.11260619e-306, 2.27202219e+184],\n",
" [1.48643702e-076, 2.37817686e+184, 4.04644567e-057],\n",
" [1.00538902e-070, 7.86209190e-067, 2.21568078e+160]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.empty([4, 3])\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### np.identity()\n",
"**➡ Returns a identity matrix of the provided order.**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0.],\n",
" [0., 1., 0., 0.],\n",
" [0., 0., 1., 0.],\n",
" [0., 0., 0., 1.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.identity(4)\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ✔ ATTIBUTES OF A NUMPY ARRAY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### shape\n",
"**➡ Returns the shape of the matrix.**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### T\n",
"**➡ Returns the transpose of matrix.**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([['Yogesh', 'Saksham'],\n",
" ['Sakshi', 'Sahil'],\n",
" ['Pankaj', 'Sandesh']], dtype='<U7')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ndim"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### size\n",
"**➡ Returns the no. of elements of array.**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### nbytes\n",
"**➡ Returns the size (in bytes) that the array occupy in the memory.**"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"168"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.nbytes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ✔ METHODS FOR A NUMPY ARRAY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .ravel()\n",
"**➡ Converts the array to 1-D array.**"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['Yogesh', 'Sakshi', 'Pankaj', 'Saksham', 'Sahil', 'Sandesh'],\n",
" dtype='<U7')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.ravel()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .transpose()\n",
"**➡ Returns the transpose of matrix.**"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([['Yogesh', 'Saksham'],\n",
" ['Sakshi', 'Sahil'],\n",
" ['Pankaj', 'Sandesh']], dtype='<U7')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"arr.transpose()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .reshape()\n",
"**➡ Changes the order of the matrix to provided one.**"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original (2 X 3) array:\n",
"[['Yogesh' 'Sakshi' 'Pankaj']\n",
" ['Saksham' 'Sahil' 'Sandesh']]\n",
"\n",
"Reshaped (3 X 2) array:\n",
"[['Yogesh' 'Sakshi']\n",
" ['Pankaj' 'Saksham']\n",
" ['Sahil' 'Sandesh']]\n"
]
}
],
"source": [
"arr = np.array([[\"Yogesh\", \"Sakshi\", \"Pankaj\"],\n",
" [\"Saksham\", \"Sahil\", \"Sandesh\"]])\n",
"print(f\"Original (2 X 3) array:\\n{arr}\\n\")\n",
"\n",
"arrReshaped = arr.reshape(3, 2)\n",
"print(f\"Reshaped (3 X 2) array:\\n{arrReshaped}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .sum()\n",
"**➡ Returns the sum of the element of the axis provided. By default it sums all the elements of the array.**"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum on axis=0: [5 7 9] | (Y-axis)\n",
"Sum on axis=1: [ 6 15] | (X-axis)\n"
]
}
],
"source": [
"arr = np.array([[1, 2, 3],\n",
" [4, 5, 6]])\n",
"print(f\"Sum on axis=0: {arr.sum(axis=0)} | (Y-axis)\")\n",
"print(f\"Sum on axis=1: {arr.sum(axis=1)} | (X-axis)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .argmin()\n",
"**➡ Returns the index at which the min value of array is located.**"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Minimum value index: 0\n",
"\n",
"Minimum value index on axis=0: [0 1 1]\n",
"Minimum value index on axis=1: [0 0]\n"
]
}
],
"source": [
"arr = np.array([[1, 5, 6],\n",
" [3, 4, 3]])\n",
"print(f\"Minimum value index: {arr.argmin()}\\n\")\n",
"\n",
"print(f\"Minimum value index on axis=0: {arr.argmin(axis=0)}\")\n",
"print(f\"Minimum value index on axis=1: {arr.argmin(axis=1)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .argmax()\n",
"**➡ Returns the index at which the max value of array is located.**"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximum value index: 5\n",
"\n",
"Maximum value index on axis=0: [1 0 1]\n",
"Maximum value index on axis=1: [2 2]\n"
]
}
],
"source": [
"arr = np.array([[1, 5, 6],\n",
" [3, 4, 7]])\n",
"print(f\"Maximum value index: {arr.argmax()}\\n\")\n",
"\n",
"print(f\"Maximum value index on axis=0: {arr.argmax(axis=0)}\")\n",
"print(f\"Maximum value index on axis=1: {arr.argmax(axis=1)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .argsort()\n",
"**➡ Returns an array which contains indeces, when array is arranged according to them, it will sort the array.**"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"argsort:\n",
"[[0 1 2]\n",
" [0 1 2]]\n",
"\n",
"argsort on axis=0:\n",
"[[0 1 0]\n",
" [1 0 1]]\n",
"argsort on axis=1:\n",
"[[0 1 2]\n",
" [0 1 2]]\n"
]
}
],
"source": [
"arr = np.array([[1, 5, 6],\n",
" [3, 4, 7]])\n",
"print(f\"argsort:\\n{arr.argsort()}\\n\")\n",
"\n",
"print(f\"argsort on axis=0:\\n{arr.argsort(axis=0)}\")\n",
"print(f\"argsort on axis=1:\\n{arr.argsort(axis=1)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .min()\n",
"**➡ Returns the minimum value of the array.**"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Minimum value: 1\n",
"\n",
"Minimum value on axis=0: [1 4 3]\n",
"Minimum value on axis=1: [1 3]\n"
]
}
],
"source": [
"arr = np.array([[1, 5, 6],\n",
" [3, 4, 3]])\n",
"print(f\"Minimum value: {arr.min()}\\n\")\n",
"\n",
"print(f\"Minimum value on axis=0: {arr.min(axis=0)}\")\n",
"print(f\"Minimum value on axis=1: {arr.min(axis=1)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### .max()\n",
"**➡ Returns the maximum value of the array.**"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximum value: 6\n",
"\n",
"Maximum value on axis=0: [3 5 6]\n",
"Maximum value on axis=1: [6 4]\n"
]
}
],
"source": [
"arr = np.array([[1, 5, 6],\n",
" [3, 4, 3]])\n",
"print(f\"Maximum value: {arr.max()}\\n\")\n",
"\n",
"print(f\"Maximum value on axis=0: {arr.max(axis=0)}\")\n",
"print(f\"Maximum value on axis=1: {arr.max(axis=1)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FOR MORE VISIT DOCUMENTATION."
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment