Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hot Fix] For Label Value Assignment #1097

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from pyspark.sql import DataFrame, functions as F, SparkSession
from pyspark.ml.feature import StringIndexer
from pyspark.sql.functions import monotonically_increasing_id

from .base_dist_transformation import DistributedTransformation
from . import DistMultiCategoryTransformation
Expand Down Expand Up @@ -47,6 +48,8 @@ def apply(self, input_df: DataFrame) -> DataFrame:
assert self.spark
processed_col_name = self.label_column + "_processed"

input_df = input_df.withColumn("unique_id", monotonically_increasing_id())

str_indexer = StringIndexer(
inputCol=self.label_column,
outputCol=processed_col_name,
Expand All @@ -60,16 +63,17 @@ def apply(self, input_df: DataFrame) -> DataFrame:
processed_col_name,
self.label_column,
)

# Labels that were missing and were assigned the value numLabels by the StringIndexer
# are converted to None
long_class_label = indexed_df.select(F.col(self.label_column).cast("long")).select(
long_class_label = indexed_df.select(F.col(self.label_column).cast("long"),
F.col("unique_id")).select(
F.when(
F.col(self.label_column) == len(str_indexer_model.labelsArray[0]), # type: ignore
F.lit(None),
)
.otherwise(F.col(self.label_column))
.alias(self.label_column)
.alias(self.label_column),
F.col("unique_id")
)

# Get a mapping from original label to encoded value
Expand All @@ -85,6 +89,7 @@ def apply(self, input_df: DataFrame) -> DataFrame:
map_dict = json.loads(mapping_str)
self.value_map[map_dict[self.label_column]] = map_dict[processed_col_name]

long_class_label = long_class_label.orderBy("unique_id").drop("unique_id")
return long_class_label

@staticmethod
Expand Down
Loading