Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Feature Scaling From Scratch

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Feature Scaling From Scratch. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Wadhwani Institute for Artificial Intelligence models may receive numeric feature matrices whose columns use very different scales. Implement column-wise feature scaling from scratch without NumPy, pandas, or other numerical libraries.

Given a non-empty rectangular matrix X and a method, return a new matrix with one independently scaled value for every input value.

  • For method == "minmax", transform each column using (x - min) / (max - min), so its minimum becomes 0 and maximum becomes 1.
  • For method == "standard", transform each column using (x - mean) / standard_deviation, where the mean and standard deviation are computed across all rows. Use population standard deviation, dividing by the number of rows.
  • If a column has the same value in every row, return 0.0 for that entire column under either method.
  • Do not mutate X.

Formal Specification

Input is X, a list of r rows, each containing c numeric values, and method, either "minmax" or "standard". Return a list of r rows containing floating-point values with the same shape as X.

Constraints

  • 1 <= len(X) <= 10^4
  • 1 <= len(X[0]) <= 100
  • Every row has the same number of numeric values
  • Values are finite numbers
  • method is either "minmax" or "standard"

Function Signature

def scale_features(X, method):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output