Skip to content

Sweekruti 2nd assignment #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
357 changes: 357 additions & 0 deletions Assignment1 Numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5]\n"
]
}
],
"source": [
"l1=[1,2,3,4,5]\n",
"a=np.array(l1)\n",
"print(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": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"matrix1=[[1,2,3],[4,5,6],[7,8,9]]\n",
"a=np.array(matrix1)\n",
"print(a)"
]
},
{
"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": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 3, 5],\n",
" [ 7, 9, 11],\n",
" [13, 15, 17]])"
]
},
"execution_count": 6,
"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": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.51178337, 0.68754766, -1.56580793, 0.38061104, 0.80845824,\n",
" 0.8847466 , -0.26930143, 0.83438683, -1.66926503, -0.87104349])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn(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": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1],\n",
" [2],\n",
" [3],\n",
" [4],\n",
" [5]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=np.array([1,2,3,4,5])\n",
"a.reshape(5,1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the shape of the above created array"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 9,
"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": 10,
"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": [
"a=np.arange(1,11)\n",
"print(a)\n",
"print(a[2])\n",
"print(a[3])\n",
"print(a[8])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print alternate elements of that array"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 3 5 7 9]\n"
]
}
],
"source": [
"print(a[0:10:2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Change last 3 elements into 100 using broadcasting and print"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 2, 3, 4, 5, 6, 7, 100, 100, 100])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[7:10]=100\n",
"a"
]
},
{
"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": 13,
"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": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=np.arange(1,26)\n",
"a1=a.reshape(5,5)\n",
"print(a1)\n",
"a1[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