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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

558 changes: 558 additions & 0 deletions python-assn/assn-01/Vinayak_Assignment1.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions python-assn/assn-01/myfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hey sjdfj foasidu namename oslakj iosdfupo lajhio soadfoin hofsu asdoyuf hasdof asoasd asdhklf asdfhl ioasfu
Empty file added python-assn/assn-01/namename
Empty file.
353 changes: 353 additions & 0 deletions python-assn/assn-02/.ipynb_checkpoints/Assignment2-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy exercise\n",
"===\n",
"\n",
"This exercise is designed to get you familiarised with numpy and shows the most common operations that you will perform in the future.\n",
"\n",
"You will need the Iris.csv file from the GitHub repositry to perform this exercise.\n",
"\n",
"Write your code in the following way:\n",
"#################################<br>\n",
"\n",
"Your code here\n",
"#################################"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]\n",
" [12 13 14 15]]\n"
]
}
],
"source": [
"A=np.arange(16) #This creates a vector with values from 0 to 15.\n",
"\n",
"#Reshape A into a 4x4 numpy array and print the array\n",
"#################################\n",
"A.resize(4,4)\n",
"print(A)\n",
"#################################"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Expected output:<br>\n",
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]\n",
" [12 13 14 15]]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 4 9]\n",
" [ 16 25 36 49]\n",
" [ 64 81 100 121]\n",
" [144 169 196 225]]\n"
]
}
],
"source": [
"#Create another array B which is made of square of each elements of B and print B.\n",
"#################################\n",
"B=A*A\n",
"print(B)\n",
"#################################"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Expected output:<br>\n",
"[[ 0 1 4 9]\n",
" [ 16 25 36 49]\n",
" [ 64 81 100 121]\n",
" [144 169 196 225]]\n"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"#Create a numpy array of shape (28,28) whose values are chosen randomly from 0-255\n",
"import random\n",
"#################################\n",
"C=np.random.randint(0,256,28*28)\n",
"C.resize(28,28)\n",
"#print(C)\n",
"#################################\n",
"\n",
"#Convert x into a vector (Make it of single dimension)\n",
"#################################\n",
"x=np.array([3,2,5,7,5,4,6,8,9])\n",
"#################################\n",
"\n",
"#Add the number 1 at the start of x\n",
"# e.g.: If x is [3,2,5,7,5,...], new x should be [1,3,2,5,7,5,...]\n",
"#################################\n",
"x=np.append([1],x)\n",
"#print(x)\n",
"#################################\n",
"\n",
"#Create vector W of shape (785,10) whose values follow a normal distribution with mean=0 and standard dev=0.01\n",
"#################################\n",
"W= np.random.normal(0, 0.01, (785, 10))\n",
"#print(W)\n",
"#################################\n",
"\n",
"#Multiply the 2 matrices W and x. (Keep in mind the shape of the two)\n",
"#################################\n",
"abc=np.matmul(W,x)\n",
"#print(abc)\n",
"#################################"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#Load the Iris.csv file into numpy array 'data' and shuffle the data.(The data array should contain rows in random order)\n",
"#################################\n",
"data=np.genfromtxt('Iris.csv',delimiter=',')\n",
"np.random.shuffle(data)\n",
"#print(data)\n",
"#################################\n",
"\n",
"\n",
"#Create a numpy array X that contains the firts 4 columns of data. Keep the last column of data in numpy array y.\n",
"#################################\n",
"X=data[0:,0:4]\n",
"Y=data[0:,4:5]\n",
"#print(X,Y)\n",
"#################################"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"first 10 value:\n",
" [[ 5.9 3. 4.2 1.5 1. ]\n",
" [ 5.9 3.2 4.8 1.8 1. ]\n",
" [ 5.5 3.5 1.3 0.2 0. ]\n",
" [ 6.3 3.4 5.6 2.4 2. ]\n",
" [ 5.5 4.2 1.4 0.2 0. ]\n",
" [ 4.9 2.4 3.3 1. 1. ]\n",
" [ 5.8 2.7 5.1 1.9 2. ]\n",
" [ 6.1 2.6 5.6 1.4 2. ]\n",
" [ 6.5 3. 5.2 2. 2. ]\n",
" [ 7. 3.2 4.7 1.4 1. ]]\n",
"last 10 value:\n",
" [[ 7.7 3. 6.1 2.3 2. ]\n",
" [ 6.3 2.5 4.9 1.5 1. ]\n",
" [ 5.2 4.1 1.5 0.1 0. ]\n",
" [ 5.7 3.8 1.7 0.3 0. ]\n",
" [ 6.5 3.2 5.1 2. 2. ]\n",
" [ 5.8 2.7 4.1 1. 1. ]\n",
" [ 5. 2.3 3.3 1. 1. ]\n",
" [ 5.2 3.5 1.5 0.2 0. ]\n",
" [ 5.4 3.4 1.7 0.2 0. ]\n",
" [ 7.1 3. 5.9 2.1 2. ]]\n",
"All rows of X whose corresponding value in y is 2\n",
"[ 6.3 3.4 5.6 2.4]\n",
"[ 5.8 2.7 5.1 1.9]\n",
"[ 6.1 2.6 5.6 1.4]\n",
"[ 6.5 3. 5.2 2. ]\n",
"[ 6.3 2.7 4.9 1.8]\n",
"[ 6.4 2.8 5.6 2.2]\n",
"[ 7.2 3. 5.8 1.6]\n",
"[ 6.9 3.2 5.7 2.3]\n",
"[ 6.4 3.2 5.3 2.3]\n",
"[ 7.6 3. 6.6 2.1]\n",
"[ 6.3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#################################\n",
"print(\"first 10 value:\\n\",data[0:10])\n",
"print(\"last 10 value:\\n\",data[-10::1])\n",
"print(\"All rows of X whose corresponding value in y is 2\",)\n",
"for i in data:\n",
" if i[4]==2:\n",
" print(i[0:4])\n",
"#################################"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"# Create a numpy array y1 of same length as y and number of columns 3. \n",
"# Values of y1 are such that,\n",
"# If y contains 0, y1 should have (1,0,0)\n",
"# If y contains 1, y1 should have (0,1,0)\n",
"# If y contains 2, y1 should have (0,0,1)\n",
"# This type of encoding is known as one-hot encoding\n",
"#################################\n",
"y1 = np.empty((0,3),int)\n",
"for i in range(0,len(Y)):\n",
" if(Y[i])==0:\n",
" y1=np.append(y1,np.array([[1,0,0]]),axis=0)\n",
" elif(Y[i])==1:\n",
" y1=np.append(y1,np.array([[0,1,0]]),axis=0)\n",
" elif(Y[i])==2:\n",
" y1=np.append(y1,np.array([[0,0,1]]),axis=0) \n",
"#print(y1)\n",
"#################################"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 32, 64, 3)\n"
]
}
],
"source": [
"A=np.random.randint(100,size=(32,64,3))\n",
"# Convert A into shape (1,32,64,3) without using np.reshape()\n",
"#################################\n",
"temp=np.empty((1,32,64,3),int)\n",
"for i in range(0,32):\n",
" for j in range(0,64):\n",
" for k in range(0,3):\n",
" temp[0][i][j][k]=A[i][j][k]\n",
"A=mat\n",
"print(A.shape)\n",
"#################################"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Expected Output:<br>(1,32,64,3)"
]
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading