Thursday, 21 December 2023

Top 20 NumPy Question Answer

 

1. Create a null vector of size 10.


array1 = np.zeros(10,dtype = int)
array1

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


array1 = np.zeros(10,dtype = int)
array1[4] = 1
array1

3. Write a NumPy program to add, subtract, multiply, divide arguments element-wise.


arr1 = np.array([1,2,3,4,5,6,7,8,9,10])
print( arr1)

add = arr1+10
print(add)

substract = arr1-10
print(substract)

multiply = arr1*10
print(multiply)

divide = arr1/10
print(divide)

4. Create a vector with values ranging from 15 to 45.


array1 = np.arange(15,45)
array1

5.Write a NumPy program to round elements of the array to the nearest integer.


arr1 = np.round([1.2,1.22,1.43,2.56])
arr1

6. Write a NumPy program to convert angles from degrees to radians for all elements in a given array.


rad_values = np.deg2rad([0,30,45,60,90])
rad_values

7. What is the use of all and any function in NumPy?


 all() - This function returns True, if all element of the iterable are True or iterable is empty.
 
 any() - This function returns True, if any one element of the iterable is True. If iterable is empty then returns False.

8. Create a 3x3 matrix with values ranging from 0 to 8.[[0 1 2] [3 4 5] [6 7 8]]

matrix1 = np.arange(0,9).reshape(3,3)

matrix1

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


arr1 = np.array([1,2,0,0,4,0])
np.where (arr1 !=0)

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


arr1 = np.random.randint(1,30,size = 30)
print(arr1)
mean = np.mean(arr1)
mean

11. How to convert 1D array to 3D array?


array1 = np.array([1,2,3,4,5,6,7,8,9,10], ndmin = 3)
array1

12. How to convert 4D array to 2D array?


We can not convert 4D array to 2D array. 

13. How to print only 3 decimal places in a python NumPy array?


array1 = np.around([1.2345,2.4586,3.59763],3)
array1

14. How to compute the median of a NumPy array?


arr1 = np.arange(1,30)
print(arr1)
median = np.median(arr1)
median

15. How to compute the standard deviation of a NumPy array?


arr1 = np.arange(1,30)
print(arr1)
std = np.std(arr1)
std

16. How to compute the mean of a NumPy array?

arr1 = np.arange(1,30)
print(arr1)
mean = np.mean(arr1)
mean

17. How to sort an array by the nth column?


arr1 = np.random.randint(1,10 , size=10)
print(arr1)
sort_arr1 = np.sort(arr1)
print(sort_arr1)

18. How to find common values between the two arrays?


X = np.array([1,2,3,4,5,6])
Y = np.array([6,4,5,8,7,9])

XY = np.intersect1d(X, Y)
  print("Common_values", XY)

19. How to round away from zero a float array?


arr1 = np.around(3.145256 , 3)
arr1

20. Create a 3x3 identity matrix


array1 = np.identity(3,dtype = int)
array1

Top 10 Pandas Question Answer

  1. Define the Pandas/Python pandas? Pandas is an open-source library for high-performance data manipulation in Python. 2. What are the dif...