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
358 changes: 358 additions & 0 deletions Numpy_Exercises.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Numpy_Exercises.ipynb",
"version": "0.3.2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/github/Sayan46/Assignment-2/blob/Sayan46/Numpy_Exercises.ipynb)"
]
},
{
"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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
},
"outputId": "34d9f53c-d444-4c2b-8d79-72fffde0b0c5"
},
"cell_type": "code",
"source": [
"import numpy as np \n",
"s = np.random.uniform(-1.3,2.5,64)\n",
"print(s)\n"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"[ 1.29714042 0.05382672 2.11603915 0.46257789 0.39152661 -0.14922545\n",
" 1.6112405 1.97758278 -1.23275156 -0.43644515 -0.83081985 -0.20409018\n",
" 0.9461046 -1.21474827 1.0501915 -0.65469646 0.21974205 2.0357349\n",
" -0.0210625 0.2303843 0.03695057 -0.22360614 1.57105111 0.44567277\n",
" -0.31814993 -0.33186138 1.83457452 0.64501397 -0.72027239 0.31311365\n",
" 2.22288395 -0.43783351 -0.5531905 -0.11004932 0.04434198 1.6141596\n",
" 1.29484714 -0.88055521 0.90784743 2.24186256 0.6197379 1.82008514\n",
" 2.14463852 2.4116079 0.80386049 -0.43050777 1.02151095 0.89904801\n",
" 1.44200865 0.00406608 1.20474749 1.03238049 -0.79745927 0.13132264\n",
" -0.04273136 -0.11766145 -0.8821126 1.15447771 2.12783845 0.59703821\n",
" 0.49830412 0.71234306 1.27832384 1.6935996 ]\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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "a6353d5f-0b60-4b22-ba90-b6de1e10f3e7"
},
"cell_type": "code",
"source": [
"n=3\n",
"x = np.array([1,2,3])\n",
"y = np.tile(x,n)\n",
"print(y)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"[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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "70fa21ec-cae3-44b7-8e78-188cfeab4d26"
},
"cell_type": "code",
"source": [
"a=np.array([1,3,5,7,9,11,13,15,17,19])\n",
"print(a)"
],
"execution_count": 11,
"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": "cdcdcdd1-c262-4b6b-dc33-1c3122db770d"
},
"cell_type": "code",
"source": [
"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",
"print(np.intersect1d(a,b))"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"[2 4]\n"
],
"name": "stdout"
}
]
},
{
"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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
},
"outputId": "24cfe21c-9147-4e8c-f483-a8072e2c8639"
},
"cell_type": "code",
"source": [
"new = np.reshape(a,(2,5))\n",
"print(new)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1 2 3 2 3]\n",
" [4 3 4 5 6]]\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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
},
"outputId": "2e74837a-614f-48f2-c4d3-2bfe25987616"
},
"cell_type": "code",
"source": [
"a=np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
"b=(a.tolist())\n",
"print(b)\n",
"a1=np.asarray(b)\n",
"print(a1)"
],
"execution_count": 12,
"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"
],
"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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
},
"outputId": "b0ea4aff-611c-4b15-82ff-6e66ae445d50"
},
"cell_type": "code",
"source": [
"d =np.ones(shape=(11,11))\n",
"d[1:-1,1:-1] = 0\n",
"print(d)"
],
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
" [1. 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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 151
},
"outputId": "7bc64148-58d9-43fd-d1ec-f04166c37979"
},
"cell_type": "code",
"source": [
"x = np.ones((3,3))\n",
"x = np.zeros((8,8),dtype=int)\n",
"x[1::2,::2] = 1\n",
"x[::2,1::2] = 1\n",
"print(x)\n"
],
"execution_count": 3,
"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