You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "srowen (via GitHub)" <gi...@apache.org> on 2025/02/21 11:07:39 UTC
Re: [PR] [SPARK-51282][ML][PYTHON][CONNECT] Optimize OneVsRestModel transform by eliminating the JVM-Python data exchange [spark]
srowen commented on code in PR #50041:
URL: https://github.com/apache/spark/pull/50041#discussion_r1965290086
##########
python/pyspark/sql/internal.py:
##########
@@ -130,3 +130,42 @@ def make_interval(unit: str, e: Union[Column, int, float]) -> Column:
"SECOND": "secs",
}
return F.make_interval(**{unit_mapping[unit]: F.lit(e)})
+
+ @staticmethod
+ def get_vector(vec: Column, idx: Column) -> Column:
+ unwrapped = F.unwrap_udt(vec)
+ is_dense = unwrapped.getField("type") == F.lit(1)
+ values = unwrapped.getField("values")
+ size = F.when(is_dense, F.array_size(values)).otherwise(unwrapped.getField("size"))
+ sparse_idx = InternalFunction.array_binary_search(unwrapped.getField("indices"), idx)
Review Comment:
Does a dense vector even have `indices`?
##########
python/pyspark/sql/internal.py:
##########
@@ -130,3 +130,42 @@ def make_interval(unit: str, e: Union[Column, int, float]) -> Column:
"SECOND": "secs",
}
return F.make_interval(**{unit_mapping[unit]: F.lit(e)})
+
+ @staticmethod
+ def get_vector(vec: Column, idx: Column) -> Column:
+ unwrapped = F.unwrap_udt(vec)
+ is_dense = unwrapped.getField("type") == F.lit(1)
+ values = unwrapped.getField("values")
+ size = F.when(is_dense, F.array_size(values)).otherwise(unwrapped.getField("size"))
+ sparse_idx = InternalFunction.array_binary_search(unwrapped.getField("indices"), idx)
+ value = (
+ F.when(is_dense, F.get(values, idx))
+ .when(sparse_idx >= 0, F.get(values, sparse_idx))
+ .otherwise(F.lit(0.0))
+ )
+
+ return F.when((0 <= idx) & (idx < size), value).otherwise(
+ F.raise_error(F.printf(F.lit("Vector index must be in [0, %s), but got %s"), size, idx))
+ )
+
+ @staticmethod
+ def array_argmax(arr: Column) -> Column:
Review Comment:
Wouldn't maybe array_position and array_max together do the same thing?
(array_position returns a 1-based index, so you'd subtract 1)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org