Skip to content

Instantly share code, notes, and snippets.

@HarshCasper
Created May 12, 2020 09:49
Show Gist options
  • Save HarshCasper/66dee2af3617ea1a018c60d6ba23100e to your computer and use it in GitHub Desktop.
Save HarshCasper/66dee2af3617ea1a018c60d6ba23100e to your computer and use it in GitHub Desktop.
Numpy Array.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Numpy Array.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyNdWCMydxw84Kkcxw8WeJzR",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/HarshCasper/66dee2af3617ea1a018c60d6ba23100e/numpy-array.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IbLS5Leigty-",
"colab_type": "text"
},
"source": [
"# Numpy Tutorials\n",
"\n",
"NumPy is an open source library available in Python that aids in mathematical, scientific, engineering, and data science programming. NumPy is an incredible library to perform mathematical and statistical operations. It works perfectly well for multi-dimensional arrays and matrices multiplication\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gi1YpnR_g4TO",
"colab_type": "text"
},
"source": [
"### Working with Numpy"
]
},
{
"cell_type": "code",
"metadata": {
"id": "JE9RYgXrg-xd",
"colab_type": "code",
"outputId": "59fe1cf5-471a-4e62-cc3f-1e38a0bec8be",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"import numpy as np\n",
"\n",
"# Convert a List to an Array using Numpy\n",
"\n",
"np_list=[1,2,3,4,5]\n",
"np_arr=np.array(np_list)\n",
"print(np_arr)\n",
"print(type(np_arr))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 2 3 4 5]\n",
"<class 'numpy.ndarray'>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "EYI1aJHbhQnR",
"colab_type": "code",
"outputId": "28fd7bfb-45f4-4737-bbbb-3cfb9212ea02",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"# Calculate the Dimension of the Array\n",
"\n",
"print(\"The Number of Rows and Columns in your Array is %r\" %(np_arr.shape))\n",
"print(\"The Dimension of your Array is %r\" %(np_arr.ndim))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"The Number of Rows and Columns in your Array is 5\n",
"The Dimension of your Array is 1\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "sXBBavLiiHIA",
"colab_type": "code",
"outputId": "1ac92d52-9b53-46ba-b449-e96571ac6788",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"# Let's use some other functions and check Numpy's Functionality\n",
"\n",
"print(\"The Number of Items in the Array are %r\" %(np_arr.itemsize)) # Returns length of one array element in bytes\n",
"print(\"The Total Size of the Array is %r\" %(np_arr.nbytes)) # Returns the Total Memory Size of the Array"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"The Number of Items in the Array are 8\n",
"The Total Size of the Array is 40\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gRArEwxQjojT",
"colab_type": "text"
},
"source": [
"### Working with Multidimensional Array"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RI7ebgSMjsPj",
"colab_type": "code",
"outputId": "cf3d65e4-ea0a-470c-c061-4468d50411d8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 67
}
},
"source": [
"list1=[1,2,3,4,5]\n",
"list2=[6,7,8,9,10]\n",
"list3=[11,12,13,14,15]\n",
"\n",
"multi_arr=np.array([list1,list2,list3],dtype=\"int32\")\n",
"print(multi_arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[ 1 2 3 4 5]\n",
" [ 6 7 8 9 10]\n",
" [11 12 13 14 15]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Wl1FkMXakNpl",
"colab_type": "code",
"outputId": "b3062f39-a644-4113-b149-4f467fba14be",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"print(multi_arr.shape)\n",
"print(multi_arr.ndim)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"(3, 5)\n",
"2\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "6k-fVdtekZ76",
"colab_type": "code",
"outputId": "660c4fd2-8838-467b-dfda-ed874a315795",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# Let's reshape our Array \n",
"\n",
"new_arr=multi_arr.reshape(1,15)\n",
"print(new_arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "04RdPrdLo24E",
"colab_type": "code",
"outputId": "4a8e4fe3-aeec-47de-f9ee-7711d6ebc14b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 101
}
},
"source": [
"new_arr1=new_arr.reshape(5,3)\n",
"print(new_arr1)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 9]\n",
" [10 11 12]\n",
" [13 14 15]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "RLcMbq3fpBYT",
"colab_type": "code",
"outputId": "3b02fdcb-73fd-47ed-90df-6c21cf712081",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 67
}
},
"source": [
"# We will reshape a One-Dimensional Array into a Three Dimensional Array\n",
"\n",
"arr=np.array([1,2,3,4,5,6,7,8,9])\n",
"newarr = arr.reshape(1, 3, 3)\n",
"print(newarr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]]\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment