Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
390 changes: 390 additions & 0 deletions My_Numpy_Exercises.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,390 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Numpy_Exercises.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"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/github/Suraj1199/Assignment-2/blob/Suraj1199/My_Numpy_Exercises.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "a_4UupTr9fbX",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# Numpy Exercises\n",
"\n",
"1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions"
]
},
{
"metadata": {
"id": "LIP5u4zi0Nmg",
"colab_type": "code",
"outputId": "2578a466-c03f-4ecc-a1d8-fa2c615e9b2c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 208
}
},
"cell_type": "code",
"source": [
"import numpy as np #import numpy\n",
"a = np.linspace(1.3,2.5,64)\n",
"print (a)"
],
"execution_count": 76,
"outputs": [
{
"output_type": "stream",
"text": [
"[1.3 1.31904762 1.33809524 1.35714286 1.37619048 1.3952381\n",
" 1.41428571 1.43333333 1.45238095 1.47142857 1.49047619 1.50952381\n",
" 1.52857143 1.54761905 1.56666667 1.58571429 1.6047619 1.62380952\n",
" 1.64285714 1.66190476 1.68095238 1.7 1.71904762 1.73809524\n",
" 1.75714286 1.77619048 1.7952381 1.81428571 1.83333333 1.85238095\n",
" 1.87142857 1.89047619 1.90952381 1.92857143 1.94761905 1.96666667\n",
" 1.98571429 2.0047619 2.02380952 2.04285714 2.06190476 2.08095238\n",
" 2.1 2.11904762 2.13809524 2.15714286 2.17619048 2.1952381\n",
" 2.21428571 2.23333333 2.25238095 2.27142857 2.29047619 2.30952381\n",
" 2.32857143 2.34761905 2.36666667 2.38571429 2.4047619 2.42380952\n",
" 2.44285714 2.46190476 2.48095238 2.5 ]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "dBoH_A7M9jjL",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3"
]
},
{
"metadata": {
"id": "4TxT66309n1o",
"colab_type": "code",
"outputId": "9f25b496-8c58-4735-c710-04686c30e58c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"a = np.array([1,2,3])\n",
"\n",
"x = np.resize(a,12)\n",
"print(x)"
],
"execution_count": 77,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 2 3 1 2 3 1 2 3 1 2 3]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "Vh-UKizx9oTp",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"3) Create an array of the first 10 odd integers."
]
},
{
"metadata": {
"id": "ebhEUZq29r32",
"colab_type": "code",
"outputId": "160c78ce-9337-467b-9609-9bab72eda317",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"a = np.arange(20)\n",
"\n",
"i = a%2!=0\n",
"\n",
"b = a[i]\n",
"print (b)"
],
"execution_count": 78,
"outputs": [
{
"output_type": "stream",
"text": [
"[ 1 3 5 7 9 11 13 15 17 19]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "QfJRdMat90f4",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"4) Find intersection of a and b"
]
},
{
"metadata": {
"id": "gOlfuJCo-JwF",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "5daaadcc-4767-4f76-9c53-2bc2cdb504d7"
},
"cell_type": "code",
"source": [
"#expected output array([2, 4])\n",
"a = np.array([1,2,3,2,3,4,3,4,5,6])\n",
"b = np.array([7,2,10,2,7,4,9,4,9,8])\n",
"c = list()\n",
"k=0\n",
"for value in a:\n",
" for values in b:\n",
" if value == values and value not in c:\n",
" c.append(value)\n",
" break\n",
" \n",
"x = np.asarray(a)\n",
"x"
],
"execution_count": 79,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])"
]
},
"metadata": {
"tags": []
},
"execution_count": 79
}
]
},
{
"metadata": {
"id": "RtVCf0UoCeB8",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"5) Reshape 1d array a to 2d array of 2X5"
]
},
{
"metadata": {
"id": "2E8b55_2Cjx5",
"colab_type": "code",
"outputId": "929007aa-bb2d-4967-d57f-b454ef274315",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
}
},
"cell_type": "code",
"source": [
"a = np.arange(10)\n",
"print(a)\n",
"\n",
"print(a.reshape(2,5))"
],
"execution_count": 80,
"outputs": [
{
"output_type": "stream",
"text": [
"[0 1 2 3 4 5 6 7 8 9]\n",
"[[0 1 2 3 4]\n",
" [5 6 7 8 9]]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "dVrSBW1zEjp2",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"6) Create a numpy array to list and vice versa"
]
},
{
"metadata": {
"id": "tcBCyhXPEp9C",
"colab_type": "code",
"outputId": "3d060203-be6f-4813-c1e6-d9c1764f591a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
}
},
"cell_type": "code",
"source": [
"a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
"print(a)\n",
"#converting to list\n",
"b = a.tolist()\n",
"print (b)\n",
"#again converting it to an array\n",
"c =np.asarray(b)\n",
"print (c)"
],
"execution_count": 81,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 2 3 4 5 6 7 8 9]\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"[1 2 3 4 5 6 7 8 9]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "JNqX8wnz9sQJ",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones."
]
},
{
"metadata": {
"id": "4bjP3JAc9vRD",
"colab_type": "code",
"outputId": "054cba0e-5051-4b16-8b59-a3b5170aa2a7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 191
}
},
"cell_type": "code",
"source": [
"a = np.zeros(shape = (10,10))\n",
"for i in range(0,10):\n",
" for j in range(0,10):\n",
" if i == 0 or i == 9 or j == 0 or j == 9:\n",
" a[i,j] = 1\n",
" \n",
"print (a)"
],
"execution_count": 82,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "xaQgf8tT9v-n",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach."
]
},
{
"metadata": {
"id": "No7fx0Xy9zEh",
"colab_type": "code",
"outputId": "57a4dada-8e5a-4b13-8cd1-992ce702c7bd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 156
}
},
"cell_type": "code",
"source": [
"a = np.zeros(shape = (8,8)) \n",
" # fill with 1 the alternate rows and columns \n",
" \n",
"a[1::2, ::2] = 1\n",
"a[::2, 1::2] = 1\n",
" \n",
"print (a)"
],
"execution_count": 83,
"outputs": [
{
"output_type": "stream",
"text": [
"[[0. 1. 0. 1. 0. 1. 0. 1.]\n",
" [1. 0. 1. 0. 1. 0. 1. 0.]\n",
" [0. 1. 0. 1. 0. 1. 0. 1.]\n",
" [1. 0. 1. 0. 1. 0. 1. 0.]\n",
" [0. 1. 0. 1. 0. 1. 0. 1.]\n",
" [1. 0. 1. 0. 1. 0. 1. 0.]\n",
" [0. 1. 0. 1. 0. 1. 0. 1.]\n",
" [1. 0. 1. 0. 1. 0. 1. 0.]]\n"
],
"name": "stdout"
}
]
}
]
}
Loading