Skip to content

Commit

Permalink
updated assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
simrunsharma committed Oct 13, 2023
1 parent 4d0ea23 commit 5ce7bed
Show file tree
Hide file tree
Showing 23 changed files with 32,395 additions and 0 deletions.
Binary file added Code-Drawing Template.pdf
Binary file not shown.
391 changes: 391 additions & 0 deletions NumpyExercises/Individual_Numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,391 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 1: make a common array"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 80842, 333008, 202553, 140037, 81969],\n",
" [ 63857, 42105, 261540, 481981, 176739],\n",
" [489984, 326386, 110795, 394863, 25024],\n",
" [ 38317, 49982, 408830, 485118, 16119],\n",
" [407675, 231729, 265455, 109413, 103399],\n",
" [174677, 343356, 301717, 224120, 401101],\n",
" [140473, 254634, 112262, 25063, 108262],\n",
" [375059, 406983, 208947, 115641, 296685],\n",
" [444899, 129585, 171318, 313094, 425041],\n",
" [188411, 335140, 141681, 59641, 211420],\n",
" [287650, 8973, 477425, 382803, 465168],\n",
" [ 3975, 32213, 160603, 275485, 388234],\n",
" [246225, 56174, 244097, 9350, 496966],\n",
" [225516, 273338, 73335, 283013, 212813],\n",
" [ 38175, 282399, 318413, 337639, 379802],\n",
" [198049, 101115, 419547, 260219, 325793],\n",
" [148593, 425024, 348570, 117968, 107007],\n",
" [ 52547, 180346, 178760, 305186, 262153],\n",
" [ 11835, 449971, 494184, 472031, 353049],\n",
" [476442, 35455, 191553, 384154, 29917]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"# Seed insures results are stable.\n",
"np.random.seed(21)\n",
"random_integers = np.random.randint(1, high=500000, size=(20, 5))\n",
"random_integers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 2:What is the average value of the second column (to one decimal place)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"214895.8"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The average value of the second column\n",
"random_integers[:, 1].mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 3: What is the average value of the first 5 rows of the third and fourth columns (to one decimal place)?"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"286058.5"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The average of the first 5 rows of 3rd and 4th columns\n",
"subset = random_integers[:5, 2:4]\n",
"np.mean(subset)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 4: Result of matrix 1 plus matrix 2\n",
"\\begin{bmatrix}\n",
"2 & 4 & 6\\\\\n",
"5 & 7 & 9\n",
"\\end{bmatrix}"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2 4 6]\n",
" [5 7 9]]\n"
]
}
],
"source": [
"# Exercise 4 Python:\n",
"first_matrix = np.array([[1, 2, 3], [4, 5, 6]])\n",
"second_matrix = np.array([1, 2, 3])\n",
"print(first_matrix + second_matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 5: Result of my_vector[selection]:\n",
"\\begin{bmatrix}\n",
"2 & 4 & 6\\\\\n",
"\\end{bmatrix}"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 4 6]\n"
]
}
],
"source": [
"# Exercise 5 python:\n",
"my_vector = np.array([1, 2, 3, 4, 5, 6])\n",
"selection = my_vector % 2 == 0\n",
"print(my_vector[selection])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For exercise 6: I didn't make any errors but I learned how to do matrix notation on markdown"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 7 slicing:\n",
"\\begin{bmatrix}\n",
"2&3\\\\\n",
"5&6\n",
"\\end{bmatrix}"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 4 6]\n",
" [10 12]]\n"
]
}
],
"source": [
"# Exercise 8\n",
"my_array = np.array([[1, 2, 3], [4, 5, 6]])\n",
"my_slice = my_array[:, 1:3]\n",
"my_array[:, :] = my_array * 2\n",
"print(my_slice)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 8 slicing and view\n",
"\\begin{bmatrix}\n",
"4&6\\\\\n",
"10&12\n",
"\\end{bmatrix}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 9 what does the slice look like?\n",
"\\begin{bmatrix}\n",
"2&3\\\\\n",
"5&6\n",
"\\end{bmatrix}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 10: my prediction was correct. I knew that slice would not change because my_slice creates its own subsetted array that is different from the original my array"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2 3]\n",
" [5 6]]\n"
]
}
],
"source": [
"# exercise 10\n",
"my_array = np.array([[1, 2, 3], [4, 5, 6]])\n",
"my_slice = my_array[:, 1:3]\n",
"my_array = my_array * 2\n",
"print(my_slice)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 11: \n",
"\\begin{bmatrix}\n",
"2&3\\\\\n",
"5&6\n",
"\\end{bmatrix}"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2 3]\n",
" [5 6]]\n"
]
}
],
"source": [
"my_array = np.array([[1, 2, 3], [4, 5, 6]])\n",
"my_slice = my_array[:, 1:3].copy()\n",
"my_array[:, :] = my_array * 2\n",
"print(my_slice)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 12 prediction:\n",
"y would be [\"a change\", 2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 13 prediction: if we printed x it would be [1,2,3]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a change', 2]\n",
"[1, 2, 3]\n"
]
}
],
"source": [
"# Exercise 12 and 13:\n",
"x = [1, 2, 3]\n",
"y = x[0:2]\n",
"y[0] = \"a change\"\n",
"print(y)\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 14: I was correct and this is because we sliced y and then made a change on a specific index number for y and also x doesn't change at all."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 3]\n",
"[3]\n",
"[ 2 -1]\n",
"[-1]\n"
]
}
],
"source": [
"my_array = np.array([1, 2, 3])\n",
"my_array = my_array[1:4]\n",
"print(my_array)\n",
"my_slice = my_array[1:3]\n",
"print(my_slice)\n",
"my_slice[0] = -1\n",
"print(my_array)\n",
"print(my_slice)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 5ce7bed

Please sign in to comment.