Q1. You have a numpy array of temperatures in Celsius: [0, 20, 30, 40]. Convert to Fahrenheit using vectorized operation. (F = C*9/5 + 32).
c = np.array([0, 20, 30, 40])
f = c * 9/5 + 32 # [32., 68., 86., 104.]No loop needed. Vectorized operations are applied element-wise, fast and concise.Q2. You have two arrays a = [10,20,30], b = [1,2,3]. Compute a/b, a**b, and np.exp(a).
a / b # [10., 10., 10.]
a ** b # [10, 400, 27000]
np.exp(a) # extremely large numbersUniversal functions (ufuncs) like np.exp, np.log, np.sin apply element-wise. Used for activation functions (sigmoid, tanh).Q3. Compute the sum, cumulative sum, mean, and standard deviation of an array [1,2,3,4,5] using NumPy methods.
arr = np.arange(1,6)
s = arr.sum() # 15
cs = arr.cumsum() # [1,3,6,10,15]
m = arr.mean() # 3.0
std = arr.std() # about 1.414These are descriptive statistics.Q4. You have a 2D array representing scores of 3 students in 4 subjects: [[85,90,88,92],[78,85,80,86],[92,95,90,94]]. Compute the mean score per student (row-wise) and per subject (column-wise).
scores = np.array([[85,90,88,92],
[78,85,80,86],
[92,95,90,94]])
student_means = scores.mean(axis=1) # ~ [88.75, 82.25, 92.75]
subject_means = scores.mean(axis=0) # ~ [85,90,86,90.67]axis=1 across columns, axis=0 across rows.Q5. Clip values in an array to a range [0,1]. For example, arr = [-0.5, 0.2, 1.3, 0.7]. Use np.clip.
arr = np.array([-0.5, 0.2, 1.3, 0.7])
clipped = np.clip(arr, 0, 1) # [0., 0.2, 1., 0.7]Useful for bounding probabilities or gradients. Also can do arr[arr<0]=0; arr[arr>1]=1 but clip is easier.