FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
13.7k views
in Python Programming by Goeduhub's Expert (2.2k points)
edited by

Assignment/Task 4

Question on Numpy-

  1. Import the numpy package under the name np and Print the numpy version and the configuration 
  2. Create a null vector of size 10
  3. Create Simple 1-D array and check type and check data types in array
  4. How to find number of dimensions, bytes per element and bytes of memory used?
  5. Create a null vector of size 10 but the fifth value which is 1
  6. Create a vector with values ranging from 10 to 49
  7. Reverse a vector (first element becomes last)
  8. Create a 3x3 matrix with values ranging from 0 to 8
  9. Find indices of non-zero elements from [1,2,0,0,4,0]
  10. Create a 3x3 identity matrix
  11. Create a 3x3x3 array with random values
  12. Create a 10x10 array with random values and find the minimum and maximum values
  13. Create a random vector of size 30 and find the mean value
  14. Create a 2d array with 1 on the border and 0 inside
  15. How to add a border (filled with 0's) around an existing array? 
  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

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

  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])
  19. Write a program to show how Numpy taking less memory compared to Python List?

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

7 Answers

0 like 0 dislike
by (150 points)
selected by
 
Best answer

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

import numpy as np

# NumPy version

np.__version__

# NumPy configuration

np.show_config()

2. Create a null vector of size 10

Answer: np.zeros(10)

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

Answer:

a = np.arange(0, 10)

# type

type(a)

# data type

a.dtype

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

Answer:

a = np.arange(0, 10)

# No of dimensions

a.ndim

# No of bytes per element

a.itemsize

# No of bytes

a.nbytes

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

Answer:

null = np.zeros(10)

null[4] = 1

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

Answer: np.arange(10,50)

7. Reverse a vector (first element becomes last)

Answer:

b = np.arange(10,50)

b[::-1]

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

Answer:

c = np.arange(0,9)

c.reshape(3,3)

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

Answer:

d = np.array([1, 2, 0, 0, 4, 0])

np.nonzero(d)

10. Create a 3x3 identity matrix

Answer:

# identity() function

np.identity(3)

# eye() function

np.eye(3, 3)

11. Create a 3x3x3 array with random values

Answer: np.random.random((3, 3, 3))

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

Answer:

e = np.random.random((10,10))

# minimum value

np.min(e)

# maximum values

np.max(e)

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

Answer:

f = np.random.random((5,6))

# mean values of matrix

np.mean(f)

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

Answer:

g = np.ones((5,5))

g[1:4, 1:4] = 0

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

Answer:

h = np.ones((5,5))

h[:1, :] = 0

h[:, :1] = 0

h[4:, :] = 0

h[:, 4:] = 0

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

Answer:

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

# To get 13

i[1,5]

# To get first row only

i[0,:]

# to get third column only

i[:,2]

# To get [2, 4, 6]

i[0,1::2]

# replace 13 by 20

i[1,5] = 20

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

Answer:

j = np.arange(1,17)

# Dimension of j

j.ndim

# convert 1D to 2D

j.reshape((2,8))

18. Create the following pattern without hardcoding. Use only numpy 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])

Answer:

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

np.append(np.repeat(a,3), np.tile(a,3))

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

Answer:

l = range(100)

= 2

import sys

# Size of Python list

sys.getsizeof(a) * len(l)

# Size of numpy array

al = np.arange(100)

al.size * al.itemsize)

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

Answer:

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")

1 like 0 dislike
by (113 points)

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

ans)import numpy as np

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

       print(a)

2.Create a null vector of size 10

   import numpy as np

   a=np.array(10)

   print(a)

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

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

    print(a)

    print(a,ndim)

    print(a[0])

    print(a[1])

    print(type(a))

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

0 like 0 dislike
by (342 points)

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

import numpy as np

print(np.__version__)

print(np.show_config())

2. Create a null vector of size 10.

x = np.zeros(10)

print(x)

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

x = np.arange(5)

print(x)

print(type(x))

x.dtype

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

x = np.arange(10)

print(x.size)

print(x.ndim)

print(x.itemsize)

print(x.nbytes)

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

x = np.zeros(10)

x[4] = 1

print(x)

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

x = np.arange(1050)

print(x)

7. Reverse a vector (first element becomes last)

x = np.arange(10)

print(x[::-1])

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

x = np.arange(9)

x = x.reshape(3,3)

print(x)

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

x = [1,2,0,0,4,0]

arr = np.array(x)

res = np.nonzero(arr)

res

10. Create a 3x3 identity matrix

id = np.identity(3)

print(id)

11. Create a 3x3x3 array with random values

x = np.random.random((3,3,3))

print(x)

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

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

x = np.random.random(30)

print(x)

print(np.mean(x))

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

x = np.ones((4,4))

print(x)

print('2d array with 1 on the border and 0 inside')

x[1:-1,1:-1] = 0

print(x)

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

x = np.ones((4,4))

x = np.pad(x, pad_width=1, mode='constant', constant_values=0)

print(x)

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

x = np.array([[ 1234567], [ 891011121314]])

print(x)

print(x[1,-2])

print('1st Row Only',x[0,:])

print('3rd Column Only',x[:,2])

print(x[0,:][1:6:2])

x[1,5]=20

print(x)

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

x= np.arange(4)

print(x)

x.reshape(2,2)

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

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

print(np.r_[np.repeat(a,3), np.tile(a,3)])

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

l = range(1000)

import sys

a = 10

print('Size Of List : ', sys.getsizeof(a)*len(l))

a1 = np.arange(1000)

print('Size Of Numpy Array : ', a1.size*a1.itemsize)

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

import time

import sys

size = 10000

list1 = range(size)

list2 = range(size)

n1 = np.arange(size)

n2 = np.arange(size)

start = time.time()

x = time.time()

res = [x+y for x,y in zip(list1,list2)]

print('Time Of List Is : ', (time.time()-start)*1000)

start = time.time()

res1 = n1+n2

print('Time Of Numpy Array Is : ', (time.time()-start)*1000)

0 like 0 dislike
by (278 points)
0 like 0 dislike
by (120 points)

GS_STP_4359

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

Ans:

import numpy as np

print(np.__version__)

print(np.show_config())

Q2.Create a null vector of size 10 

Ans:

import numpy as np

a=np.zeros([10])
print(a)

Output: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

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

Ans:
import numpy as np
a=np.array([10,20,30,40,50])
b=type(a)
c=a.dtype
print("type",b)
print("dtype=",c)
Output: 
type=<class 'numpy.ndarray'>
 dtype=int64

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

Ans:
a=np.array([10,20,30,40,50])
a.nbytes
np.shape
Output: nbytes 40
              shape(5, )

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

Ans:
import numpy as np
a=np.zeros([0])
a[5]=1
print(a)
Output:
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
  

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

Ans:

import numpy as np
a=np.arange(10,49)
print(a)
Output:
 [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48]
Q7. Reverse a vector (first element becomes last) 
Ans:
import numpy as np
a=np.array([10,20,30,40])
print("array=",a)
a=a[::-1]
print("reverse array=",a)
Output:
array=[10 20 30]
reverse array=[30 20 10]
Q8.Create a 3x3 matrix with values ranging from 0 to 8
Ans:
Cannot create 3*3 array using ranging 0 to 8.
0 to 8 only 7 vales but 3*3 array consist of 9 values
Q 9. Find indices of non-zero elements from [1,2,0,0,4,0] 
Ans:
Q10.Create a 3x3 identity matrix
Ans:
import numpy as np
a=np.array([[10,20,30],[40,50,60],[70,80,90]])
print(a)
Output:
[[10,20,30],
[40,50,60],
[70,80,90]]
Q11. Create a 3x3x3 array with random values
Ans:
import numpy as np
a=np.arange(0,28)
a=a.reshape(3,3,3)
Output:
[[[0 1 2]
 [3 4 5 ]
 [ 6 4 7 ]]
[[9 10 11] 
[12 13 14] 
[15 16 17]] 
[[18 19 20]
 [21 22 23] 
[24 25 26]]]
Q12. Create a 10x10 array with random values and find the minimum and maximum values 
Ans:
a=np.arange(0,100)
a=a.reshape(10,10)
a.min()
a.max()
Output:
[[0 1 2 3 4 5 6 7 8 9] 
[10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29] 
[30 31 32 33 34 35 36 37 38 39] 
[40 41 42 43 44 45 46 47 48 49]
 [50 51 52 53 54 55 56 57 58 59] 
[60 61 62 63 64 65 66 67 68 69] 
[70 71 72 73 74 75 76 77 78 79] 
[80 81 82 83 84 85 86 87 88 89] 
[90 91 92 93 94 95 96 97 98 99]]
min 0
max 99
Q13. Create a random vector of size 30 and find the mean value
import numpy as np
a=np.arange(0,29)
a.mean()
Output:
mean 14.5
Q14. Create a 2d array with 1 on the border and 0 inside
Ans:
a=np.ones(5,5)
a=a[1:-1,1:-1]=0
print(a)
Output:
[[1 1 1 1 1 ]
[1 0 0 0 1]
[1 0 0 0 1]
[1 0 0 0 1]
[1 1 1 1 1]]
Q15. How to add a border (filled with O's) around an existing array? 
Q16. How to Accessing/Changing specific elements, rows, columns, etc in Numpy array? Example - [[1234567] [89 10 11 12 13 14]] Get 13, get first row only, get 3rd column only, get [2, 4, 6], replace 13 by 20
0 like 0 dislike
by (138 points)

GO_STP_6734

Here is the answer for task 4 in my linkedin : https://www.linkedin.com/pulse/assignmenttask-4-aswin-kumar

0 like 0 dislike
by (120 points)
ma'am

how we can get [2,4,6] in question 16.

explain the Question 18,15.
by Goeduhub's Expert (9.3k points)
You can use Slicing ..
i[0,1::2]

For Question 18

Create the following pattern without hardcoding. Use only numpy 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])

Answer:

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

np.append(np.repeat(a,3), np.tile(a,3))

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

Answer:

h = np.ones((5,5))

h[:1, :] = 0

h[:, :1] = 0

h[4:, :] = 0

h[:, 4:] = 0

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...