English 中文(简体)
• 如何在花园使用花anda的分组/分组数据组上安装
原标题:how to iterator over grouped/partitioned dataframe in pyspark using pandas_udf

spark version: 3.2

I have pandas_udf defined

def calculate_shap(iterator: Iterator[pd.DataFrame]) -> Iterator[pd.DataFrame]:
    for X in iterator:
        yield pd.DataFrame(
            explainer.shap_values(np.array(X), check_additivity=False)[0],
            columns=columns_for_shap_calculation,
        )

return_schema = StructType()
for feature in columns_for_shap_calculation:
    return_schema = return_schema.add(StructField(feature, FloatType()))

shap_values = df.mapInPandas(calculate_shap, schema=return_schema)

在此情况下,我如何能够确保,当我们通过图印班达时,iterator Object将分成我想要确定的任何部分?

For example, if i have pyspark dataframe with 1Million rows with ID column that has value of 1,2,3,4 and

  1. 200K rows have value of 1
  2. 500K rows have value of 2
  3. 100K rows have value of 3
  4. 200K rows have value of 4

if this is the case, my iterator should be partitioned by ID, which is splited by [200K,500K,100K,200K] and perform pandas_udf.

我有一些想法使用

df = df.repartition("ID") and then pass to df.mapInPandas, but will this work this will change my number of partition but not iterator object??

或者

df = df.groupBy("ID") and then pass to df.mapInPandas, but how can I make this work using groupBy?

操纵的途径比较容易。 激光器物体

问题回答

重新划分数据范围。 使用<代码>再分配("ID”),以在应用<代码>的“ID”栏前的分立数据为准。

This physically shuffles data, creating partitions with rows having the same "ID" values. It directly affects the iterator object within mapInPandas.

df = df.repartition("ID")
shap_values = df.mapInPandas(calculate_shap, schema=return_schema)

Don t use groupBy immediately before mapInPandas. groupBy creates a GroupedData object, not a DataFrame compatible with mapInPandas.

#If at all grouping is important, Write a function -combining grouping and mapInPandas:

def grouped_calculate_shap(df):
    for id_value, group_df in df.groupBy("ID"):
        yield calculate_shap(group_df.toPandas())

shap_values = df.mapInPandas(grouped_calculate_shap, schema=return_schema)

希望是有助益的!





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签