Question
Write a function `cc_std_consider_missing` that * has exactly the same input and output types as the `cc_std_ignore_missing` function, * and has similar functionality to `cc_std_ignore_missing`
Write a function `cc_std_consider_missing` that * has exactly the same input and output types as the `cc_std_ignore_missing` function, * and has similar functionality to `cc_std_ignore_missing` except that it can handle and ignore the NaN entries when computing the class conditional means. You can borrow most of the code from your `cc_std_ignore_missing` implementation, but you should make it compatible with the existence of NaN values in the features. Try and avoid the utilization of loops as much as possible. No loops are necessary. * **Hint**: You may find the `np.nanstd` function useful.
cc_std_ignore_missing:
def cc_std_ignore_missing(train_features, train_labels): N, d = train_features.shape #calculating std for labels when 0 y_when_zero = train_features[np.where(train_labels == 0)[0], :][:, [0,1,2,3,4,5,6,7]] std_zero = np.std(y_when_zero,axis=0).reshape(-1,1) #calculating std for labels when 1 y_when_one = train_features[np.where(train_labels == 1)[0], :][:, [0,1,2,3,4,5,6,7]] std_one = np.std(y_when_one,axis=0).reshape(-1,1) #stacking both obtained arrays columnwise sigma_y = np.hstack((std_zero, std_one)) assert sigma_y.shape == (d, 2) return sigma_y
cc_std_consider_missing:
def cc_std_consider_missing(train_features_with_nans, train_labels): N, d = train_features_with_nans.shape # your code here raise NotImplementedError assert not np.isnan(sigma_y).any() assert sigma_y.shape == (d, 2) return sigma_y
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Performing sanity checks on your implementation some_feats = np.array([[ 1. , 85. , 66. , 29. , 0. , 26.6, 0.4, 31. ], [ 8. , 183. , 64. , 0. , 0. , 23.3, 0.7, 32. ], [ 1. , 89. , 66. , 23. , 94. , 28.1, 0.2, 21. ], [ 0. , 137. , 40. , 35. , 168. , 43.1, 2.3, 33. ], [ 5. , 116. , 74. , 0. , 0. , 25.6, 0.2, 30. ]]) some_labels = np.array([0, 1, 0, 1, 0])
for i,j in [(0,0), (1,1), (2,3), (3,4), (4, 2)]: some_feats[i,j] = np.nan
some_std_y = cc_std_consider_missing(some_feats, some_labels)
assert np.array_equal(some_std_y.round(2), np.array([[ 2. , 4. ], [13.77, 0. ], [ 0. , 12. ], [14.5 , 17.5 ], [44.31, 0. ], [ 1.03, 9.9 ], [ 0.09, 0.8 ], [ 4.5 , 0.5 ]]))
# Checking against the pre-computed test database test_results = test_case_checker(cc_std_consider_missing, task_id=6) assert test_results['passed'], test_results['message']
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started