pyspark.sql.functions.expm1
This function computes the result of e raised to the power of a given number, and then subtracts one. The mathematical representation is: expm1(x) = e^x – 1. While the formula may seem straightforward, you might wonder: why not just use a simple exponentiation? The answer lies in precision. For values of x close to zero, the result of e^x – 1 might not be accurate due to rounding errors. The expm1 function, however, ensures precision in these cases, making it invaluable for numerical computations. The pyspark.sql.functions.expm1 function is more than just a mathematical computation. It exemplifies the intricacies and precision PySpark brings to the table for big data computations.
Example with PySpark
Before we start, ensure you have PySpark and its dependencies set up. Now, let’s walk through an example using hardcoded data:
from pyspark.sql import SparkSession
from pyspark.sql.functions import expm1
# Initialize Spark session
spark = SparkSession.builder.appName("expm1_demo @ Freshers.in").getOrCreate()
# Create a DataFrame with hardcoded data
data = [("A", 0.1), ("B", 0.2), ("C", 0.01)]
df = spark.createDataFrame(data, ["ID", "Value"])
# Compute expm1 for each value
df_with_expm1 = df.withColumn("expm1_Value", expm1(df["Value"]))
# Show the results
df_with_expm1.show()
Output
+---+-----+--------------------+
| ID|Value| expm1_Value|
+---+-----+--------------------+
| A| 0.1| 0.10517091807564763|
| B| 0.2| 0.22140275816016985|
| C| 0.01|0.010050167084168058|
+---+-----+--------------------+
Spark important urls to refer