Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Feature Scaling Function

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

Your question is Feature Scaling Function. 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

Smartsheet machine learning pipelines may contain features with very different numeric ranges. Implement a function that transforms a feature matrix using either min-max normalization or population z-score standardization.

Formal Specification

Given X, a non-empty rectangular matrix represented as a list of rows, where each row is one sample and each column is one feature, and a string method:

  1. For method = "minmax", scale each column to the range [0, 1] using (x - minimum) / (maximum - minimum).
  2. 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 variance by the number of rows.
  3. If a column has no variation, output 0.0 for every value in that column.
  4. Return a new matrix. Do not modify X.

Constraints

  • 1 <= len(X) <= 10^4
  • 1 <= len(X[0]) <= 100
  • X is rectangular
  • Every feature value is a finite number
  • 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