Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Home Forums Assignment NPTEL_ Computer Vision Week 0 Deep Learning Computer Vision, IIT Hyderabad

• Author
Posts
• #898

NPTEL Week 0 Unit 2 Assignment Basic of Numpy
https://github.com/AbhishekTyagi404/Deep-Learning-with-Computer-Vision/tree/master/Assignment

``````#!/usr/bin/env python
# coding: utf-8

# #### **Welcome to Assignment 0 on Deep Learning for Computer Vision.**
# In this assignment you will get a chance to understand and work on some of the commonly used functionalities of several Machine Learnign and Data Analysis libraries
#
# #### **Instructions**
# 1. Use Python 3.x to run this notebook
# 2. Write your code only in between the lines 'YOUR CODE STARTS HERE' and 'YOUR CODE ENDS HERE'.
# 3. Look up the documentation for each of the Python and Numpy function used.
# 4. This Assignment is just to make you understand several commonly used library functions.
# 5. This assignment is **NOT** evaluated

# In:

# DO NOT CHANGE THIS CODE
import numpy as np
np.random.seed(0)

# #### Python Basics
#

# In:

### Implement recursive fibonacci in the function below
def recursive_fibonacci(n):
if n<0:
print("Incorrect Input")
elif n==0:
return 0
elif n==1:
return 1
else:
return recursive_fibonacci(n-1) + recursive_fibonacci(n-2)

# In:

### Test your code using the function call below.
recursive_fibonacci(3)

# #### Lambda function

# In:

def sigmoid(x):
sigmoid = 1 / (1 + np.exp(-x))
return sigmoid

## Implement the above sigmoid function using python lambda functions
sigmoid_lambda = lambda x : sigmoid(x)

# In:

## Check you implementation by running this cell (should return True)
sigmoid(1) == sigmoid_lambda(1)

# #### List Comprehension

# In:

div_by_four = []
for number in range(200):
if number%4 == 0:
div_by_four.append(number)

## Implement the above function using list comprehensions
div_by_four_lc = div_by_four

# In:

## Check you implementation by running this cell (should return True)
div_by_four == div_by_four_lc

# #### Python map keyword
# Using the lambda functions and python's map keyword apply sigmoid on a list of numbers from 0 to 99. Store the output in a list and print it.
#
# **Observe what happens to the value of sigmoid as the value of x increases. Do you see any trend?**

# In:

out = map(sigmoid_lambda, div_by_four)
print(list(out))

# # Numpy Basics:
#

# #### Creating a numpy array with particular value
#

# In:

# Create a numpy array with size 2*2 with all elements as 8.
arr = np.array([[2, 4, 6], [6, 8, 10]], np.int32)

# #### Basic Numpy operations:

# In:

#Create a numpy array of size 3*3 with random values
rand_arr = np.random.rand(3,3)
print(rand_arr)

# In:

#Find the sum of all the elements, mean, maximum and minimum value of the numpy array defined above

arr_sum = np.sum([rand_arr])
arr_mean = np.mean([rand_arr])
arr_max = np.max([rand_arr])
arr_min = np.min([rand_arr])
print("Sum =", arr_sum, "\n" , "Mean=", arr_mean, "\n" ,"Max=", arr_max, "\n" ,"Min=", arr_min)

# #### Reshaping and Indexing of Numpy Array:

# In:

#create a 1D numpy array of shape 35 with values 1,2,3..,35

y = np.arange(35)
print(y)

# In:

# Reshape it as 2D array of shape (5,7)

y = y.reshape(5,7)
print(y.shape)
print(y)

# In:

# Extract all the elements from 2nd and 3rd row

y_row = y[2:4,:]
y_row

# In:

# Extract all the elements from 3rd and 5th and 7th column

y_column = y[:,2:7:2]
print(y_column)

# #### Horizontal and vertical stacking of numpy array

# In:

## horizontal and vertical stacking of 1D arrays
a = np.array([4.,2.])
b = np.array([3.,8.])

# Horizontal stacking
h_stack = np.hstack((a,b))

# Vertical Stacking
v_stack = np.vstack((a,b))
print(h_stack)
print(v_stack)

# #### <code>argmin</code> and <code>argmax</code> in numpy array

# In:

# Define an array
arr = np.array([[5,12,51,25] ,[25,29,2,27]])

#Find the position of maximum and minimum value of above array
max_idx = np.argmax(arr)
min_idx = np.argmax(arr)
print (max_idx,min_idx)

#Find the indices of maximum and minimum value along each of its columns.
max_col = np.argmax(arr,axis = 0)
min_col = np.argmin(arr,axis = 0)
print (max_col,min_col)

#Find the indices of maximum and minimum value along each of the its rows.
max_row = np.argmax(arr,axis = 1)
min_row = np.argmin(arr,axis = 1)
print (max_row,min_row)