TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Frequency Encoding

Feature Engineering
Easy

Frequency encoding replaces each categorical value with its relative frequency (proportion) in the dataset. A category appearing 30 times out of 100 entries is encoded as 0.3. This creates a numerical feature that captures how common each category is, often useful because rare categories behave differently from common ones.

Given a list of categorical values, replace each value with the proportion of times it appears in the list.

Algorithm

Count the occurrences of each unique value, then divide each count by the total number of values to get the proportion.

Loading visualization...

Examples

Input:

values = ["a", "b", "a", "c", "a"]

Output:

[0.6, 0.2, 0.6, 0.2, 0.6]

"a" appears 3/5 = 0.6, "b" appears 1/5 = 0.2, "c" appears 1/5 = 0.2. Each position gets its category's frequency.

Input:

values = ["cat", "dog", "cat", "cat", "dog"]

Output:

[0.6, 0.4, 0.6, 0.6, 0.4]

"cat" has frequency 3/5 = 0.6 and "dog" has frequency 2/5 = 0.4.

Hint 1

First count occurrences using a dictionary: for each value, increment its count. Then divide each count by len(values) to get proportions.

Hint 2

Build the counts dict, then return [counts[v] / len(values) for v in values]. Each position gets its category's proportion.

Requirements

  • Count occurrences of each unique value
  • Divide each count by the total number of values
  • Replace each value with its proportion
  • Return a list of floats with the same length as the input

Constraints

  • values is non-empty
  • Values can be strings or integers
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems
Target EncodingOrdinal EncodingInteraction FeaturesCyclic EncodingPolynomial Features

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Frequency Encoding

Feature Engineering
Easy

Frequency encoding replaces each categorical value with its relative frequency (proportion) in the dataset. A category appearing 30 times out of 100 entries is encoded as 0.3. This creates a numerical feature that captures how common each category is, often useful because rare categories behave differently from common ones.

Given a list of categorical values, replace each value with the proportion of times it appears in the list.

Algorithm

Count the occurrences of each unique value, then divide each count by the total number of values to get the proportion.

Loading visualization...

Examples

Input:

values = ["a", "b", "a", "c", "a"]

Output:

[0.6, 0.2, 0.6, 0.2, 0.6]

"a" appears 3/5 = 0.6, "b" appears 1/5 = 0.2, "c" appears 1/5 = 0.2. Each position gets its category's frequency.

Input:

values = ["cat", "dog", "cat", "cat", "dog"]

Output:

[0.6, 0.4, 0.6, 0.6, 0.4]

"cat" has frequency 3/5 = 0.6 and "dog" has frequency 2/5 = 0.4.

Hint 1

First count occurrences using a dictionary: for each value, increment its count. Then divide each count by len(values) to get proportions.

Hint 2

Build the counts dict, then return [counts[v] / len(values) for v in values]. Each position gets its category's proportion.

Requirements

  • Count occurrences of each unique value
  • Divide each count by the total number of values
  • Replace each value with its proportion
  • Return a list of floats with the same length as the input

Constraints

  • values is non-empty
  • Values can be strings or integers
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems
Target EncodingOrdinal EncodingInteraction FeaturesCyclic EncodingPolynomial Features

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.