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
364 changes: 364 additions & 0 deletions Assignment1 Numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,364 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make a python list => \\[1,2,3,4,5\\]\n",
"\n",
"Convert it into numpy array and print it"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4, 5]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy\n",
"list=[1,2,3,4,5]\n",
"a=np.array([list])\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make a python matrix (3 x 3) => \\[[1,2,3],[4,5,6],[7,8,9]\\]\n",
"\n",
"Convert it into numpy array and print it"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix1=[[1,2,3],[4,5,6],[7,8,9]]\n",
"np.array(matrix1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make a matrix (3 x 3) using built-in methods (like arange(), reshape() etc.):\n",
"\n",
"\\[ [1,3,5],\n",
"\n",
" [7,9,11],\n",
" \n",
" [13,15,17] \\]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 3, 5],\n",
" [ 7, 9, 11],\n",
" [13, 15, 17]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=np.arange(1,18,2)\n",
"a.reshape(3,3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a numpy array with 10 random numbers from 0 to 10 (there should be few numbers greater than 1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 9, 3, 4, 2, 8, 3, 2, 0, 6])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randint(0,10,10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create numpy array => \\[1,2,3,4,5\\] and convert it to 2D array with 5 rows"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1],\n",
" [2],\n",
" [3],\n",
" [4],\n",
" [5]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"a=np.array([1,2,3,4,5])\n",
"\n",
"a.reshape(5,1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the shape of the above created array"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a numpy array with 10 elements in it. Access and print its 3rd, 4th and 9th element."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 2 3 4 5 6 7 8 9 10]\n",
"3\n",
"4\n",
"9\n"
]
}
],
"source": [
"a1=np.arange(1,11)\n",
"print(a1)\n",
"print(a1[2])\n",
"print(a1[3])\n",
"print(a1[8])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print alternate elements of that array"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 3 5 7 9]\n"
]
}
],
"source": [
"print(a1[0:10:2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Change last 3 elements into 100 using broadcasting and print"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 2, 3, 4, 5, 6, 7, 100, 100, 100])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1[7:10]=100\n",
"a1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a 5 x 5 matrix (fill it with any element you like), print it.\n",
"\n",
"Then print the middle (3 x 3) matrix."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 2 3 4 5]\n",
" [ 6 7 8 9 10]\n",
" [11 12 13 14 15]\n",
" [16 17 18 19 20]\n",
" [21 22 23 24 25]]\n"
]
},
{
"data": {
"text/plain": [
"array([[ 7, 8, 9],\n",
" [12, 13, 14],\n",
" [17, 18, 19]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1=np.arange(1,26)\n",
"a2=a1.reshape(5,5)\n",
"print(a2)\n",
"a2[1:4,1:4]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading