17、修改以下代码块,使用摄氏度代替华氏度。如果将该修改后的 UDF(用户定义函数)应用于相同的数据框,结果会有何不同?
输出是相同的。归一化过程不会基于温度的单位而改变。
以下是修改后的函数:
def scale_temperature_C(temp_by_day: pd.DataFrame) -> pd.DataFrame:
"""Returns a simple normalization of the temperature for a site, in Celcius.
If the temperature is constant for the whole window, defaults to 0.5."""
def f_to_c(temp):
return (temp - 32.0) * 5.0 / 9.0
18、已知有一个名为 gsod 的 Spark 数据框,以及一个名为 scale_temperature 的函数。该函数返回的数据框包含六个列:stn, year, mo, da, temp, 和 temp_norm。现在要对 gsod 数据框按 ‘year’ 和 ‘mo’ 进行分组,并应用分组映射 UDF,代码如下:gsod_exo = gsod.groupby(“year”, “mo”).applyInPandas(scale_temperature, schema=???) 。请完成代码中 schema 的定义,并说明这样应用分组映射 UDF 会发生什么。
模式应该是:
schema = "year string, mo string, stn string, da string, temp double, temp_norm double"
如果这样应用分组映射 UDF,数据将按 year 和 mo 分组,然后对每个分组应用 scale_temperature 函数,最终返回一个包含指定列的 Spark 数据框。
19、修改以下代码块,使其以 ArrayType 形式返回线性回归的截距和斜率。(提示:截距在拟合模型的 intercept_ 属性中)
from sklearn.linear_model import LinearRegression
from typing import Sequence
import pyspark.sql.functions as F
import pyspark.sql.types as T
import pandas as pd
@F.pandas_udf(T.ArrayType(T.DoubleType()))
def rate_of_change_temperature(day: pd.Series, temp: pd.Series) -> Sequence[float]:
"""Returns the intercept and slope of the daily temperature for a given period of time."""
model = LinearRegression().fit(
X=day.astype(int).values.reshape(-1, 1),
y=temp
)
return [model.intercept_, model.coef_[0]]
20、使用以下代码找出每年中温度最高的日期,并计算平均温度。分析当出现两个以上相同最高温度情况时会怎样。代码如下:each_year = Window.partitionBy(“year”) (gsod .withColumn(“min_temp”, F.min(“temp”).over(each_year)) .where(“temp = min_temp”) .select(“year”, “mo”, “da”, “stn”, “temp”) .orderBy(“year”, “mo”, “da”) .show())
提供的代码是用于找出每年中温度最低的日期,而非温度最高的日期。若要找出每年温度最高的日期,需将 F.min 替换为 F.max 。
当有两个以上日期温度相同且都是最高温度时,这些日期都会被选中并显示。代码会按 year 、 mo 、 da 排序后展示。
代码中未涉及计算平均温度的部分。若要计算平均温度,可在筛选出每年最高温度的记录后,按 year 分组并使用 F.avg 函数计算。
21、如何使用gsod_light数据框创建一个完整的排名,即让temp_per_month_asc中的每条记录都有唯一的排名?对于orderBy()值相同的记录,排名顺序无关紧要。已知代码如下:temp_per_month_asc = Window.partitionBy(“mo”).orderBy(“count_temp”); gsod_light = spark.read.parquet(“./data/window/gsod_light.parquet”); gsod_light.withColumn( “rank_tpm”, F.rank().over(temp_per_month_asc) ).show() 请修改代码以实现需求。
可以使用 row_number() 函数来为每条记录分配唯一的排名。示例代码如下:

最低0.47元/天 解锁文章
1003

被折叠的 条评论
为什么被折叠?



