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
375 changes: 375 additions & 0 deletions Copy_of_Excercise_numpy_.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,375 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Copy of Excercise numpy .ipynb",
"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": [
"<a href=\"https://colab.research.google.com/github/MSoumen/Numpy-Assignment-/blob/soumen_mahata/Copy_of_Excercise_numpy_.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a_4UupTr9fbX",
"colab_type": "text"
},
"source": [
"# Numpy Exercises\n",
"\n",
"1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions"
]
},
{
"cell_type": "code",
"metadata": {
"id": "LIP5u4zi0Nmg",
"colab_type": "code",
"outputId": "e52b5c12-ebfd-48ac-a9a1-a97503a1a143",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"import numpy as np #import numpy\n",
"arr1=np.linspace(-1.3,2.5,64)\n",
"print(arr1)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"[-1.3 -1.23968254 -1.17936508 -1.11904762 -1.05873016 -0.9984127\n",
" -0.93809524 -0.87777778 -0.81746032 -0.75714286 -0.6968254 -0.63650794\n",
" -0.57619048 -0.51587302 -0.45555556 -0.3952381 -0.33492063 -0.27460317\n",
" -0.21428571 -0.15396825 -0.09365079 -0.03333333 0.02698413 0.08730159\n",
" 0.14761905 0.20793651 0.26825397 0.32857143 0.38888889 0.44920635\n",
" 0.50952381 0.56984127 0.63015873 0.69047619 0.75079365 0.81111111\n",
" 0.87142857 0.93174603 0.99206349 1.05238095 1.11269841 1.17301587\n",
" 1.23333333 1.29365079 1.35396825 1.41428571 1.47460317 1.53492063\n",
" 1.5952381 1.65555556 1.71587302 1.77619048 1.83650794 1.8968254\n",
" 1.95714286 2.01746032 2.07777778 2.13809524 2.1984127 2.25873016\n",
" 2.31904762 2.37936508 2.43968254 2.5 ]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dBoH_A7M9jjL",
"colab_type": "text"
},
"source": [
"2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4TxT66309n1o",
"colab_type": "code",
"outputId": "ee2c0dcf-ed07-49d0-a4f6-9701d986cf81",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"arr2=np.arange(1,4)\n",
"print(arr2)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 2 3]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vh-UKizx9oTp",
"colab_type": "text"
},
"source": [
"3) Create an array of the first 10 odd integers."
]
},
{
"cell_type": "code",
"metadata": {
"id": "ebhEUZq29r32",
"colab_type": "code",
"outputId": "ab79b833-9ae8-457b-89b5-81f88f718864",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"arr3=np.arange(20)\n",
"oddArr=arr3[arr3%2!=0]\n",
"print(oddArr)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[ 1 3 5 7 9 11 13 15 17 19]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QfJRdMat90f4",
"colab_type": "text"
},
"source": [
"4) Find intersection of a and b"
]
},
{
"cell_type": "code",
"metadata": {
"id": "gOlfuJCo-JwF",
"colab_type": "code",
"outputId": "ed551b1a-7f2e-4b29-bd91-334b0404d156",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"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",
"intersect=set(a) & set(b)\n",
"print(intersect)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"{2, 4}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RtVCf0UoCeB8",
"colab_type": "text"
},
"source": [
"5) Reshape 1d array a to 2d array of 2X5"
]
},
{
"cell_type": "code",
"metadata": {
"id": "2E8b55_2Cjx5",
"colab_type": "code",
"outputId": "cc7d88b8-7b4b-46c6-ecd1-412da9951e8e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"a = np.arange(10).reshape(2,5)\n",
"print(a)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[0 1 2 3 4]\n",
" [5 6 7 8 9]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dVrSBW1zEjp2",
"colab_type": "text"
},
"source": [
"6) Create a numpy array to list and vice versa"
]
},
{
"cell_type": "code",
"metadata": {
"id": "tcBCyhXPEp9C",
"colab_type": "code",
"outputId": "960843ba-c7ed-4d65-dde7-7b7cc19dfdbc",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
}
},
"source": [
"a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"a=np.array(a)\n",
"print(a,type(a))\n",
"a=list(a)\n",
"print(a,type(a))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JNqX8wnz9sQJ",
"colab_type": "text"
},
"source": [
"7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones."
]
},
{
"cell_type": "code",
"metadata": {
"id": "4bjP3JAc9vRD",
"colab_type": "code",
"outputId": "0c988f22-eb38-4aae-acad-1def7b27752c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 185
}
},
"source": [
"zeroArr=np.zeros((10,10),dtype=int)\n",
"zeroArr[:,[0,-1]]=1\n",
"zeroArr[[0,-1],:]=1\n",
"print(zeroArr)"
],
"execution_count": 0,
"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"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xaQgf8tT9v-n",
"colab_type": "text"
},
"source": [
"8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach."
]
},
{
"cell_type": "code",
"metadata": {
"id": "No7fx0Xy9zEh",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 151
},
"outputId": "8d45c65f-97bb-46ef-9900-aed59453a2c9"
},
"source": [
"chess=np.zeros((8,8),dtype=int)\n",
"chess[1::2,1::2]=1\n",
"chess[::2,::2]=1\n",
"chess\n"
],
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[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",
" [0, 1, 0, 1, 0, 1, 0, 1]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 2
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "tlOWop0MlZIp",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Loading