Skip to content

Commit

Permalink
hw4
Browse files Browse the repository at this point in the history
  • Loading branch information
betich committed Nov 20, 2022
1 parent d8e114b commit 3e0e6db
Show file tree
Hide file tree
Showing 31 changed files with 3,896 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
241 changes: 241 additions & 0 deletions grader/week11.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_11] 11_NumPy_★_Indexing_and_Slicing"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"# A is a 2-d array\n",
"def get_column_from_bottom_to_top( A, c ):\n",
" return A[::-1,c]\n",
"\n",
"def get_odd_rows( A ):\n",
" return A[1::2,:]\n",
"\n",
"def get_even_column_last_row( A ):\n",
" return A[-1,::2]\n",
"\n",
"def get_diagonal1( A ):# A is a square matrix\n",
" # from top-left corner down to bottom-right corner\n",
" return np.diag(A)\n",
"\n",
"def get_diagonal2( A ): # A is a square matrix\n",
" # from top-right corner down to bottom-left corner\n",
" return np.diag(np.fliplr(A))\n",
"\n",
"exec(input().strip()) #must have this line when submitting to grader"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_12] 11_NumPy_★_Scalar_and_Array"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"he\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"def toCelsius(f):\n",
" return (f-32)*5/9\n",
"\n",
"def BMI(wh):\n",
" return wh[:,0]/(wh[:,1]/100)**2 \n",
"\n",
"def distanceTo(p, Points):\n",
" return np.sqrt((p[0]-Points[:,0])**2+(p[1]-Points[:,1])**2)\n",
"\n",
"exec(input().strip())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_13] 11_NumPy_★_Logistic_Regression"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def p(x):\n",
" return 1 / (1 + np.e**(-(-3.98 + 0.1 * x[:,0] + 0.5 * x[:,1])))\n",
"\n",
"exec(input().strip())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_21] 11_NumPy_★★_Slicing and Element-wise Op."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def sum_2_rows(M):\n",
" return M[::2,:]+M[1::2,:]\n",
"\n",
"def sum_left_right(M):\n",
" return M[:,:M.shape[1]//2]+M[:,M.shape[1]//2:]\n",
"\n",
"def sum_upper_lower(M):\n",
" return M[:M.shape[0]//2,:]+M[M.shape[0]//2:,:]\n",
"\n",
"def sum_4_quadrants(M):\n",
" return sum_left_right(sum_upper_lower(M))\n",
"\n",
"def sum_4_cells(M):\n",
" return M[::2,::2]+M[::2,1::2]+M[1::2,::2]+M[1::2,1::2]\n",
"\n",
"def count_leap_years(years):\n",
" return np.sum((years%4==0)&((years%100!=0)|(years%400==0)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_22] 11_NumPy_★★_Outer_Product"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def mult_table(nrows, ncols):\n",
" return np.outer(np.arange(1,nrows+1),np.arange(1,ncols+1))\n",
"\n",
"exec(input().strip())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_23] 11_NumPy_★★_Lower_than_Mean"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"wtf\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"read_data=lambda:(np.array([float(x)for x in input().split()]),np.array([[int(r)for r in input().split()]for i in'0'*int(input())]))\n",
"\n",
"def report_lower_than_mean(weight, data):\n",
" mean = np.mean(data[:,1:] * weight)\n",
" result = data[np.mean(weight * data[:,1:], axis=1) < mean][:,0]\n",
" print(\", \".join([str(id) for id in result]) if result.shape[0] > 0 else \"None\")\n",
"\n",
"exec(input().strip())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [10_NumPy_24] 11_NumPy_★★_Peak_Indexes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"def peak_indexes(x):\n",
" # x is an array containing values\n",
" # return an array listing indexes of “peaks”\n",
" pass\n",
"\n",
"def main():\n",
" d = np.array([float(e) for e in input().split()])\n",
" pos = peak_indexes(np.array(d))\n",
" if len(pos) > 0:\n",
" print(\", \".join([str(e) for e in pos]))\n",
" else:\n",
" print(\"No peaks\")\n",
"\n",
"exec(input().strip()) # Don't remove this line\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.10 64-bit",
"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.9.10"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 3e0e6db

Please sign in to comment.