HyukjinKwon commented on code in PR #39628:
URL: https://github.com/apache/spark/pull/39628#discussion_r1073041810
##########
python/pyspark/ml/functions.py:
##########
@@ -637,6 +641,62 @@ def predict_columnar(x1: np.ndarray, x2: np.ndarray) ->
Mapping[str, np.ndarray]
# |[12.0, 13.0, 14.0...|[12.0, 13.0, 14.0]|54.0|39.0|
# |[16.0, 17.0, 18.0...|[16.0, 17.0, 18.0]|70.0|51.0|
# +--------------------+------------------+----+----+
+
+ def make_multi_sum_fn():
+ def predict_row(x1: np.ndarray, x2: np.ndarray) -> list[Mapping[str,
float]]:
+ # x1.shape = [batch_size, 4]
+ # x2.shape = [batch_size, 3]
+ return [{'sum1': np.sum(x1[i]), 'sum2': np.sum(x2[i])} for i in
range(len(x1))]
+ return predict_row
+
+ multi_sum_udf = predict_batch_udf(
+ make_multi_sum_fn,
+ return_type=StructType([
+ StructField("sum1", FloatType(), True),
+ StructField("sum2", FloatType(), True)
+ ]),
+ batch_size=5,
+ input_tensor_shapes=[[4], [3]],
+ )
+
+ df.withColumn("sum", multi_sum_udf("t1", "t2")).select("t1", "t2",
"sum.*").show(5)
+ # +--------------------+------------------+----+----+
+ # | t1| t2|sum1|sum2|
+ # +--------------------+------------------+----+----+
+ # |[0.0, 1.0, 2.0, 3.0]| [0.0, 1.0, 2.0]| 6.0| 3.0|
+ # |[4.0, 5.0, 6.0, 7.0]| [4.0, 5.0, 6.0]|22.0|15.0|
+ # |[8.0, 9.0, 10.0, ...| [8.0, 9.0, 10.0]|38.0|27.0|
+ # |[12.0, 13.0, 14.0...|[12.0, 13.0, 14.0]|54.0|39.0|
+ # |[16.0, 17.0, 18.0...|[16.0, 17.0, 18.0]|70.0|51.0|
+ # +--------------------+------------------+----+----+
+
+ def make_multi_times_two_fn():
+ def predict(x1: np.ndarray, x2: np.ndarray) -> Mapping[str,
np.ndarray]:
+ # x1.shape = [batch_size, 4]
+ # x2.shape = [batch_size, 3]
+ return {"t1x2": x1 * 2, "t2x2": x2 * 2}
+ return predict
+
+ multi_times_two_udf = predict_batch_udf(
+ make_multi_times_two_fn,
+ return_type=StructType([
+ StructField("t1x2", ArrayType(FloatType()), True),
+ StructField("t2x2", ArrayType(FloatType()), True)
+ ]),
+ batch_size=5,
+ input_tensor_shapes=[[4], [3]],
+ )
+
+ df.withColumn("x2", multi_times_two_udf("t1", "t2")).select("t1", "t2",
"x2.*").show(5)
+ #
+--------------------+------------------+--------------------+------------------+
+ # | t1| t2| t1x2|
t2x2|
+ #
+--------------------+------------------+--------------------+------------------+
+ # |[0.0, 1.0, 2.0, 3.0]| [0.0, 1.0, 2.0]|[0.0, 2.0, 4.0, 6.0]| [0.0,
2.0, 4.0]|
+ # |[4.0, 5.0, 6.0, 7.0]| [4.0, 5.0, 6.0]|[8.0, 10.0, 12.0,...| [8.0,
10.0, 12.0]|
+ # |[8.0, 9.0, 10.0, ...| [8.0, 9.0, 10.0]|[16.0, 18.0, 20.0...|[16.0,
18.0, 20.0]|
+ # |[12.0, 13.0, 14.0...|[12.0, 13.0, 14.0]|[24.0, 26.0, 28.0...|[24.0,
26.0, 28.0]|
+ # |[16.0, 17.0, 18.0...|[16.0, 17.0, 18.0]|[32.0, 34.0, 36.0...|[32.0,
34.0, 36.0]|
+ #
+--------------------+------------------+--------------------+------------------+
Review Comment:
backtick doesn't work in sphinx IIRC. I meant ```` ``` ````
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]