Skip to content

Instantly share code, notes, and snippets.

@HarshCasper
Created May 12, 2020 09:50
Show Gist options
  • Save HarshCasper/086c817e3e534f1263fa52c22c6977f1 to your computer and use it in GitHub Desktop.
Save HarshCasper/086c817e3e534f1263fa52c22c6977f1 to your computer and use it in GitHub Desktop.
Different Types of Arrays.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Different Types of Arrays.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyNmvF1RaAtWTr1+7SgILyc0",
"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/086c817e3e534f1263fa52c22c6977f1/different-types-of-arrays.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "r-ckxe_Nc7bp",
"colab_type": "text"
},
"source": [
"# Different Types of Arrays in Numpy"
]
},
{
"cell_type": "code",
"metadata": {
"id": "3T09u6i1dGcR",
"colab_type": "code",
"outputId": "cb5432ff-aec8-48da-df93-fd2cc93331fe",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"import numpy as np\n",
"\n",
"# This Code creates a 2X3 Matrix with Elements being Zero\n",
"arr=np.zeros((2,3))\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[0. 0. 0.]\n",
" [0. 0. 0.]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Us456Q40dNPI",
"colab_type": "code",
"outputId": "4f183e0b-8f3d-433d-cef7-044afe2a9fb6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 67
}
},
"source": [
"# This Code creates a 3X3 Matrix with Element being One\n",
"arr=np.ones((3,3))\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1. 1. 1.]\n",
" [1. 1. 1.]\n",
" [1. 1. 1.]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Q-FmLURidgoT",
"colab_type": "code",
"outputId": "edb0e3d4-80ab-4b69-e3b7-a1888a0e74b6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 101
}
},
"source": [
"# This Code creates a 5X5 Matrix with an Element chosen by the User\n",
"arr=np.full((5,5),67)\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[67 67 67 67 67]\n",
" [67 67 67 67 67]\n",
" [67 67 67 67 67]\n",
" [67 67 67 67 67]\n",
" [67 67 67 67 67]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "83FtU4treCyj",
"colab_type": "code",
"outputId": "278e305c-c56f-4df9-a9ef-1b3e343a8b56",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 84
}
},
"source": [
"# This Code will create a 4X3 Matrix with a Random Element\n",
"arr=np.random.rand(4,3)\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[0.8706085 0.66905906 0.18073705]\n",
" [0.90856186 0.7654724 0.90236333]\n",
" [0.49745524 0.54378354 0.16426315]\n",
" [0.75826984 0.58330864 0.42502968]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "QP6KnZGgeW9G",
"colab_type": "code",
"outputId": "40de55ae-fe01-4003-dc8e-6bdcec95a003",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 101
}
},
"source": [
"# This Code will generate a 5X5 Matrix in a specified range\n",
"arr=np.random.randint(5,13,size=(5,5))\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[ 6 12 8 6 10]\n",
" [11 7 8 11 8]\n",
" [11 5 11 7 7]\n",
" [10 8 12 6 7]\n",
" [ 6 11 5 5 10]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "g_lqqQy0h6y4",
"colab_type": "code",
"outputId": "889e9100-809c-4008-9c96-8b78e1593706",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 101
}
},
"source": [
"# This Code Cell will generate an Identity Matrix\n",
"arr=np.identity(5)\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1. 0. 0. 0. 0.]\n",
" [0. 1. 0. 0. 0.]\n",
" [0. 0. 1. 0. 0.]\n",
" [0. 0. 0. 1. 0.]\n",
" [0. 0. 0. 0. 1.]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "NBtJ9nf7iHl5",
"colab_type": "code",
"outputId": "54d4b2ac-84e6-49b9-a26c-a22c108e98b5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 185
}
},
"source": [
"# One Thing that you need to be careful is to use copy() function to Copy an Array\n",
"arr1=arr.copy()\n",
"arr1[0]=-100\n",
"print(arr1)\n",
"print(arr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[-100. -100. -100. -100. -100.]\n",
" [ 0. 1. 0. 0. 0.]\n",
" [ 0. 0. 1. 0. 0.]\n",
" [ 0. 0. 0. 1. 0.]\n",
" [ 0. 0. 0. 0. 1.]]\n",
"[[1. 0. 0. 0. 0.]\n",
" [0. 1. 0. 0. 0.]\n",
" [0. 0. 1. 0. 0.]\n",
" [0. 0. 0. 1. 0.]\n",
" [0. 0. 0. 0. 1.]]\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment