Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! Help me with python ML , please! For numerical features, impelement a class that normalizes each feature values by subtracting the mean and dividing

Hello! Help me with python ML, please!
For numerical features, impelement a class that normalizes each feature values by subtracting the mean and dividing by the standard deviation.
Implement a fit method that takes as input feature matrix X and computes and saves mean and std values 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 normalizes each column using precomputed mean and std values for this feature.
from sklearn.base import TransformerMixin, BaseEstimator
class Scaler(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
"""
Compute and save mean and standard deviation values
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 =[]
self.column_std =[]
for i in range(X.shape[1]):
# extract column values
if type(X) is not np.ndarray:
X_i = X.values[:, i]
else:
X_i = X[:, i]
# compute mean and std
# YOUR CODE HERE
raise NotImplementedError()
return self
def transform(self, X):
"""
Normalizes values of each feature
by subtructing the corresponding precomputed feature mean
and dividing by the corresponding standard deviation.
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)):
# extract column values
if type(X) is not np.ndarray:
X_i = X.values[:, i]
else:
X_i = X[:, i]
# YOUR CODE HERE
raise NotImplementedError()
X_transformed = np.column_stack(X_transformed)
return X_transformed
# TEST Scaler class
A = np.arange(9).reshape(3,3)
scaler = Scaler()
scaler.fit(A)
assert np.allclose(scaler.column_mean, [3.0,4.0,5.0]), "Computed mean values are incorrect."
assert np.allclose(scaler.column_std,[2.44949,2.44949,2.44949]), "Computed std values are incorrect."
scaler.fit(pd.DataFrame(A))
A_transformed = scaler.transform(pd.DataFrame(A))
assert np.allclose(A_transformed,
[[-1.22474,-1.22474,-1.22474],
[0.,0.,0.],
[1.22474,1.22474,1.22474]]), "Scaled values are incorrect."

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Formal SQL Tuning For Oracle Databases Practical Efficiency Efficient Practice

Authors: Leonid Nossov ,Hanno Ernst ,Victor Chupis

1st Edition

3662570564, 978-3662570562

More Books

Students also viewed these Databases questions