ASSIGNMENT - 4, GO_STP_8113

 GO_STP_8113

Assignment - 4

Goeduhub Technologies

Task link : https://www.goeduhub.com/11546/question-exercise-practice-solutions-science-machine-learning

#OnlineSummerTraining #machinelearning #datascience #PYTHONNUMPY #pandas #pythonprogramming #goeduhub

SOLUTIONS:

  1. Import the numpy package under the name np and Print the numpy version and the configuration 

import numpy as np
print(np.__version__)
np.show_config()


2. Create a null vector of size 10

import numpy as np
print(np.zeros(10))

3. Create Simple 1-D array and check type and check data types in array

import numpy as np
a = np.arange(0, 10)
print(type(a))
print(a.dtype)

4. How to find number of dimensions, bytes per element and bytes of memory used?

import numpy as np
a = np.arange(0, 10)
print(a.ndim)
print(a.itemsize)
print(a.nbytes)

5. Create a null vector of size 10 but the fifth value which is 1

import numpy as np
a = np.zeros(10)
a[4]=1
print(a)

6. Create a vector with values ranging from 10 to 49

import numpy as np
a = np.arange(10,50)
print(a)

7. Reverse a vector (first element becomes last)

import numpy as np
a = np.arange(10,50)
b=a[::-1]
print(b)

8. Create a 3x3 matrix with values ranging from 0 to 8

import numpy as np
a = np.arange(0,9)
b=a.reshape(3,3)
print(b)

9. Find indices of non-zero elements from [1,2,0,0,4,0]

import numpy as np
a = np.array([1, 2, 0, 0, 4, 0])
print(np.nonzero(a))

10. Create a 3x3 identity matrix

import numpy as np
x=np.identity(3)
np.eye(3, 3)
print(x)

11. Create a 3x3x3 array with random values

import numpy as np
x=np.random.random((3, 3, 3))
print(x)

12. Create a 10x10 array with random values and find the minimum and maximum values

import numpy as np
x=np.random.random((10,10))
print(x)
print(np.min(x))
print(np.max(x))

13. Create a random vector of size 30 and find the mean value

import numpy as np
x=np.random.random((1,31))
print(x)
print(np.mean(x))

14. Create a 2d array with 1 on the border and 0 inside

import numpy as np
x=np.ones((5,5))
x[1:4, 1:4]=0
print(x)

15. How to add a border (filled with 0's) around an existing array? 

import numpy as np
x=np.zeros((5,5))
x[1:4, 1:4]=1
print(x)

16. How to Accessing/Changing specific elements, rows, columns, etc in Numpy array?

Example -
[[ 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14]]

Get 13, get first row only, get 3rd column only, get [2, 4, 6], replace 13 by 20

import numpy as np

a = np.arange(1,15).reshape(2,7)

print(a)

print(a[0])

print(a[:,2])

print(a[0,1::2])

a[1,5]=20

print(a)


17. How to Convert a 1D array to a 2D array with 2 rows

import numpy as np
m = np.arange(1,11)
print(m)
n=m.reshape((2,5))
print(n)

18. Create the following pattern without hardcoding. Use only numpy functions and the below input array a.

Input:

a = np.array([1,2,3])`

Desired Output:

#> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])


import numpy as np
a = np.array([1, 2, 3])
print(a)
v=np.append(np.repeat(a,3), np.tile(a,3))
print(v)

19. Write a program to show how Numpy taking less memory compared to Python List?

import numpy as np
l = range(100)
a = 2
import sys
sys.getsizeof(a) * len(l)
al = np.arange(100)
al.size * al.itemsize


20. Write a program to show how Numpy taking less time compared to Python List?

import numpy as np
import time

import sys

# Python List

size = 10000000

l1 = range(size)

l2 = range(size)

start = time.time()

result = [(x * y) for x,y in zip(l1, l2)]

print("Time taken by List: ", (time.time() - start), "seconds")

# Numpy Array

nl1 = np.arange(size)

nl2 = np.arange(size)

start1 = time.time()

res = nl1 * nl2

print("Time taken by numpy array: ", (time.time() - start1), "seconds")




















Comments

Popular posts from this blog

Data Science Matplotlib Library Data Visualization, ASSIGNMENT - 6, GO_STP_8113