Question: Hello! Help with python, please! For numerical features, impelement a class that fills missing values in each feature with its mean value. Implement a fit

Hello! Help with python, please!
For numerical features, impelement a class that fills missing values in each feature with its mean value.
Implement a fit method that takes as input feature matrix X and computes and saves mean value of each feature (assuming that all the features are numerical). The feature matrix can be either pd.DataFrame, or np.ndarray.
Implement a transform method that takes as input feature matrix X and replaces NaN values in each feature with a corresponding mean value and returns a transformed feature matrix.
from sklearn.base import TransformerMixin, BaseEstimator
class MeanImputer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
"""
Compute and save mean value of each feature in the feature matrix.
Parameters
----------
X : pd.DataFrame or numpy.ndarray of shape (n_samples, n_features)
Feature matrix.
y : pd.Series or numpy.ndarray of shape (n_samples,)(default : None)
Target values. Optional.
Returns
-------
self : returns an instance of self.
"""
self.column_mean =[]
for i in range(X.shape[1]):
if type(X) is not np.ndarray:
X_i = X.values[:, i]
else:
X_i = X[:, i]
# calculate mean for each column and add to the list `self.column_mean`
# YOUR CODE HERE
raise NotImplementedError()
return self
def transform(self, X):
"""
Fills missing values in each feature with a corresponding mean value.
Parameters
----------
X : pd.DataFrame or numpy.ndarray of shape (n_samples, n_features)
Feature matrix.
Returns
-------
X_transformed : array-like of shape (n_samples, n_features)
Transformed feature matrix.
"""
X_transformed =[]
for i in range(len(self.column_mean)):
if type(X) is not np.ndarray:
X_i = X.values[:, i]
else:
X_i = X[:, i]
# Fill missing values using mean values
# YOUR CODE HERE
raise NotImplementedError()
X_transformed = np.column_stack(X_transformed)
return X_transformed
# TEST MeanImputer class
A = np.array([
[0, np.nan, 3],
[np.nan, 6,7],
[np.nan, 8, np.nan],
])
mean_imp = MeanImputer()
mean_imp.fit(A)
print(mean_imp.column_mean)
assert mean_imp.column_mean ==[0.0,7.0,5.0], "Computed mean values are incorrect."
mean_imp.fit(pd.DataFrame(A))
A_transformed = mean_imp.transform(pd.DataFrame(A))
assert ~np.isnan(A_transformed).any(), "Transformed feature matrix still contains NaNs."
assert np.allclose(A_transformed,
[[0.,7.,3.],
[0.,6.,7.],
[0.,8.,5.]]), "Filled values are incorrect."

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!