x[1::2, ::2] = 1
print(x)<jupyter_output>[[0. 1. 0. 1.]
 [1. 0. 1. 0.]
 [0. 1. 0. 1.]
 [1. 0. 1. 0.]]<jupyter_text>Q31. Write a NumPy program to create a 3x3x3 array filled with arbitrary values.<jupyter_code>import numpy as np
x = np.random.random((3, 3, 3))
print(x)<jupyter_output>[[[0.87169043 0.16845734 0.86545461]
  [0.71147845 0.25207571 0.12543201]
  [0.90138652 0.55375416 0.05953588]]

 [[0.30443163 0.32260459 0.64969417]
  [0.88021015 0.75464034 0.04331251]
  [0.21070551 0.91985838 0.82608406]]

 [[0.61044164 0.60470635 0.80709703]
  [0.44013109 0.44076098 0.84672559]
  [0.64298421 0.01622318 0.37075796]]]<jupyter_text>Q32. Write a NumPy program to compute sum of all elements, sum of each column and sum of each row of a given array.<jupyter_code>import numpy as np
x = np.array([[0,1],[2,3]])
print("Original array:")
print(x)
print("Sum of all elements:")
print(np.sum(x))
print("Sum of each column:")
print(np.sum(x, axis=0))
print("Sum of each row:")
print(np.sum(x, axis=1))<jupyter_output>Original array:
[[0 1]
 [2 3]]
Sum of all elements:
6
Sum of each column:
[2 4]
Sum of each row:
[1 5]<jupyter_text>Q33. Write a NumPy program to compute the inner product of two given vectors.<jupyter_code>import numpy as np
x = np.array([4, 5])
y = np.array([7, 10])
print("Original vectors:")
print(x)
print(y)
print("Inner product of said vectors:")
print(np.dot(x, y))<jupyter_output>Original vectors:
[4 5]
[ 7 10]
Inner product of said vectors:
78<jupyter_text>Q34. Write a NumPy program to add a vector to each row of a given matrix.<jupyter_code>import numpy as np
m = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 1, 0])
print("Original vector:")
print(v)
print("Original matrix:")
print(m)
result = np.empty_like(m) 
for i in range(4):
  result[i, :] = m[i, :] + v
print("\nAfter adding the vector v to each row of the matrix m:")
print(result)<jupyter_output>Original vector:
[1 1 0]
Original matrix:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

After adding the vector v to each row of the matrix m:
[[ 2  3  3]
 [ 5  6  6]
 [ 8  9  9]
 [11 12 12]]<jupyter_text>Q35. Write a NumPy program to save a given array to a binary file.<jupyter_code>import numpy as np
import os
a = np.arange(20)
np.save('temp_arra.npy', a)
print("Check if 'temp_arra.npy' exists or not?")
if os.path.exists('temp_arra.npy'):
    x2 = np.load('temp_arra.npy')
    print(np.array_equal(a, x2))<jupyter_output>Check if 'temp_arra.npy' exists or not?
True<jupyter_text>Q36. Write a NumPy program to save two given arrays into a single file in compressed format (.npz format) and load it.<jupyter_code>import numpy as np
import os
x = np.arange(10)
y = np.arange(11, 20)
print("Original arrays:")
print(x)
print(y)
np.savez('temp_arra.npz', x=x, y=y)
print("Load arrays from the 'temp_arra.npz' file:")
with np.load('temp_arra.npz') as data:
    x2 = data['x']
    y2 = data['y']
    print(x2)
    print(y2)<jupyter_output>Original arrays:
[0 1 2 3 4 5 6 7 8 9]
[11 12 13 14 15 16 17 18 19]
Load arrays from the 'temp_arra.npz' file:
[0 1 2 3 4 5 6 7 8 9]
[11 12 13 14 15 16 17 18 19]<jupyter_text>Q37. Write a NumPy program to save a given array to a text file and load it.<jupyter_code>import numpy as np
import os
x = np.arange(12).reshape(4, 3)
print("Original array:")
print(x)
header = 'col1 col2 col3'
np.savetxt('temp.txt', x, fmt="%d", header=header) 
print("After loading, content of the text file:")
result = np.loadtxt('temp.txt')
print(result)<jupyter_output>Original array:
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
After loading, content of the text file:
[[ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]
 [ 9. 10. 11.]]<jupyter_text>Q38. Write a NumPy program to convert a given array into bytes, and load it as array.<jupyter_code>import numpy as np
import os
a = np.array([1, 2, 3, 4, 5, 6])
print("Original array:")
print(a)
a_bytes = a.tostring()
a2 = np.fromstring(a_bytes, dtype=a.dtype)
print("After loading, content of the text file:")
print(a2)
print(np.array_equal(a, a2))<jupyter_output>Original array:
[1 2 3 4 5 6]
After loading, content of the text file:
[1 2 3 4 5 6]
True<jupyter_text>Q39. Write a NumPy program to convert a given array into a list and then convert it into a list again.<jupyter_code>import numpy as np
a = [[1, 2], [3, 4]]
x = np.array(a)
a2 = x.tolist()
print(a == a2)<jupyter_output>True<jupyter_text>Q40. Write a NumPy program to compute the x and y coordinates for points on a sine curve and plot the points using matplotlib.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.2)
y = np.sin(x)
print("Plot the points using matplotlib:")
plt.plot(x, y)
plt.show()<jupyter_output>Plot the points using matplotlib:<jupyter_text>Q41. Write a NumPy program to convert numpy dtypes to native python types.<jupyter_code>import numpy as np
print("numpy.float32 to python float")
x = np.float32(0)
print(type(x))
pyval = x.item()
print(type(pyval))<jupyter_output>numpy.float32 to python float
<class 'numpy.float32'>
<class 'float'>In honor of