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
179 changes: 179 additions & 0 deletions Pragalbhv_MM19B012/Pragalbhv_assignment1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question1\n",
"Create two vectors 𝑦 and 𝑦̂ having the same dimensions, where 𝑦̂ should consist of random numbers between [0,1) and 𝑦 should contain 0𝑠 and1𝑠, for example, 𝑦=[0,1,1,0,1,0,0,1,...,1]. Compute the Cross-Entropy loss function\n",
"\n",
"n = 100, is the total number of elementsin y and 𝑦̂\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"\n",
"import numpy as np\n",
"class q1():\n",
" def __init__(self,n=100):\n",
" self.n=n\n",
" self.y_hat=np.random.random(self.n)\n",
" self.y=np.random.randint(0,2,self.n)\n",
" \n",
" def check(self,a,b,c):# to test testcases\n",
" self.n=a\n",
" self.y_hat=b\n",
" self.y=c\n",
" \n",
" def crossentropy(self):\n",
" O=self.y*np.log2(self.y_hat)+(1-self.y)*np.log2(1-self.y_hat)\n",
" O=-sum(O)/self.n\n",
" return O\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"# b=q1(5)\n",
"# b.check(5,np.array([0.32, 0.56, 0.71, 0.14, 0.57]),np.array([1, 0, 1, 1, 0]))\n",
"# b.check(4,np.array([0.23, 0.54, 0.61, 0.89]),np.array([0, 0, 1, 1]))\n",
"# b.crossentropy()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y: [0 0 0 1 1 1 1 1 1 1 0 1 0 0 1 1 0 1 1 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 1\n",
" 0 0 0 1 0 0 1 1 0 1 1 1 1 1 1 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 1 0 0 0\n",
" 1 1 1 1 1 0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1]\n",
"y^: [0.51021649 0.72948491 0.77934699 0.16864048 0.9810205 0.52897531\n",
" 0.42077713 0.24993429 0.29245122 0.47707456 0.68231908 0.24288666\n",
" 0.12652212 0.28854146 0.44658798 0.76661013 0.9325786 0.85773805\n",
" 0.94331178 0.06502094 0.42803983 0.98628899 0.62867782 0.82255184\n",
" 0.9620715 0.05291848 0.75515617 0.56735066 0.24574422 0.7224453\n",
" 0.6380315 0.01306936 0.77109653 0.48600201 0.36626264 0.29992089\n",
" 0.92651081 0.69393579 0.29673897 0.5465854 0.47831487 0.28070568\n",
" 0.14715873 0.81778697 0.66073728 0.46008507 0.9584546 0.32148232\n",
" 0.82442716 0.75120344 0.371478 0.09903866 0.63822627 0.98822728\n",
" 0.83935984 0.41268517 0.65951624 0.46678992 0.86718493 0.26555129\n",
" 0.22640279 0.40830547 0.01748741 0.9900541 0.35860553 0.82219612\n",
" 0.53973776 0.28845018 0.91035037 0.93150135 0.30976138 0.03925348\n",
" 0.35523396 0.18286391 0.65574514 0.90833396 0.26349271 0.53125752\n",
" 0.58091839 0.87783523 0.02759129 0.97593767 0.57664664 0.74691748\n",
" 0.49616255 0.71016896 0.58393564 0.13386478 0.51870505 0.74418914\n",
" 0.32108295 0.42511076 0.07943298 0.24763562 0.32567092 0.83955447\n",
" 0.04961893 0.28661403 0.01968282 0.99951812]\n",
"O: 1.307509498109142\n"
]
}
],
"source": [
"answer1=q1()\n",
"print('y: ',answer1.y)\n",
"print('y^: ',answer1.y_hat)\n",
"print('O: ',answer1.crossentropy())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question 2\n",
"Write a Python class to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"class findpair():\n",
" def __init__(self,arr,target):\n",
" self.dicta=dict()\n",
" self.arr=arr\n",
" self.target=target\n",
" \n",
" def find(self):\n",
" N=len(self.arr)\n",
" count=1\n",
" for i in range(N):\n",
" for j in range(N):\n",
" if self.arr[i]+self.arr[j]==self.target:\n",
" self.dicta[count]=[i,j]\n",
" count+=1\n",
" return self.dicta\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 20 10 40 50 60 70\n",
"50\n"
]
},
{
"data": {
"text/plain": [
"{1: [0, 3], 2: [2, 3], 3: [3, 0], 4: [3, 2]}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"array=input() #assumed to be 1 23 45 66 and such...\n",
"target=int(input())\n",
"arr=list(map(int,array.strip(' ').split()))\n",
"#___\n",
"answer2=findpair(arr,target)\n",
"answer2.find()"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
12,539 changes: 4,050 additions & 8,489 deletions Python_and_GIT_Basics_Summer_School.ipynb

Large diffs are not rendered by default.