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
371 changes: 371 additions & 0 deletions Copy_of_Numpy_Exercises.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,371 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Copy of 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/dnc2k/Assignment-2/blob/dnc2k/Copy_of_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": 311
},
"outputId": "23b1a117-1e5b-4256-d322-3b95cec3d1c5"
},
"cell_type": "code",
"source": [
"import numpy as np #import numpy\n",
"a=np.linspace(-1.3,2.5,64).reshape(8,8)\n",
"print (a)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"[[-1.3 -1.23968254 -1.17936508 -1.11904762 -1.05873016 -0.9984127\n",
" -0.93809524 -0.87777778]\n",
" [-0.81746032 -0.75714286 -0.6968254 -0.63650794 -0.57619048 -0.51587302\n",
" -0.45555556 -0.3952381 ]\n",
" [-0.33492063 -0.27460317 -0.21428571 -0.15396825 -0.09365079 -0.03333333\n",
" 0.02698413 0.08730159]\n",
" [ 0.14761905 0.20793651 0.26825397 0.32857143 0.38888889 0.44920635\n",
" 0.50952381 0.56984127]\n",
" [ 0.63015873 0.69047619 0.75079365 0.81111111 0.87142857 0.93174603\n",
" 0.99206349 1.05238095]\n",
" [ 1.11269841 1.17301587 1.23333333 1.29365079 1.35396825 1.41428571\n",
" 1.47460317 1.53492063]\n",
" [ 1.5952381 1.65555556 1.71587302 1.77619048 1.83650794 1.8968254\n",
" 1.95714286 2.01746032]\n",
" [ 2.07777778 2.13809524 2.1984127 2.25873016 2.31904762 2.37936508\n",
" 2.43968254 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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "13ce8763-8b0e-4de1-ad88-7f870e808022"
},
"cell_type": "code",
"source": [
"a1=np.array([1,2,3])\n",
"a2=np.resize(a1,3)\n",
"print (a2)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"[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": 35
},
"outputId": "0efbd9b4-7bc8-4978-e6f6-0fe847326a45"
},
"cell_type": "code",
"source": [
"a2=np.arange(1,20,2)\n",
"print (a2)"
],
"execution_count": 0,
"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": 35
},
"outputId": "766a60e1-d170-41e5-8526-8e13d55546a3"
},
"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",
"I = np.intersect1d(a,b)\n",
"print ('I',I)\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"I [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": 72
},
"outputId": "0321e16b-580e-4f68-e128-ef07b70086f1"
},
"cell_type": "code",
"source": [
"a = np.arange(10)\n",
"a1= a.reshape(2,5)\n",
"print ('After reshape \\n',a1)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"After reshape \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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 90
},
"outputId": "b11709dd-14c1-441f-ea05-42815cc546d8"
},
"cell_type": "code",
"source": [
"a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"a1=np.array(a)\n",
"print (\"1-D array:\\n\" , a1)\n",
"l1=a1.tolist()\n",
"print (\"Array to List:\\n\", l1)"
],
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": [
"1-D array:\n",
" [1 2 3 4 5 6 7 8 9]\n",
"Array to List:\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": 201
},
"outputId": "2618d44e-9f7a-423c-f4f4-ba5b2cc4b2b9"
},
"cell_type": "code",
"source": [
"a=np.zeros(shape=(10,10),dtype=int)\n",
"a[0,:]=1\n",
"a[:,0]=1\n",
"a[9,:]=1\n",
"a[:,9]=1\n",
"print (a)"
],
"execution_count": 4,
"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",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 182
},
"outputId": "0e8bcd3c-2881-467b-ffdb-6c57153a46c0"
},
"cell_type": "code",
"source": [
"import numpy as np\n",
"x=np.zeros(shape=(8,8),dtype=int)\n",
"x[1::2,::2]=1\n",
"x[::2,1::2]=1\n",
"print (\"Checkerboard Pattern:\\n\",x)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Checkerboard Pattern:\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",
" [0 1 0 1 0 1 0 1]\n",
" [1 0 1 0 1 0 1 0]]\n"
],
"name": "stdout"
}
]
}
]
}
Loading