在spark dataFrame 中使用 pandas dataframe

來源:互聯網
上載者:User
背景
項 pandas spark
工作方式 單機,無法處理大量資料 分布式,能處理大量資料
儲存方式 單機緩衝 可以調用 persist/cache 分布式緩衝
是否可變 是 否
index索引 自動建立 無索引
行結構 Pandas.Series Pyspark.sql.Row
列結構 Pandas.Series Pyspark.sql.Column
允許列重名 否 是

pandas dataFrame 無法支援大量資料的計算,可以嘗試 spark df 來解決這個問題。 一. xgboost 預測的例子 最佳化前

import xgboost as xgbimport pandas as pdimport numpy as np# 載入模型bst = xgb.Booster()bst.load_model("xxx.model")# 變數列表var_list=[...]df.rdd.map(lambda x : cal_xgb_score(x,var_list,ntree_limit=304)).write.toDF()# 計算分數def cal_xgb_score(x,var_list,ntree_limit=50):    feature_count = len(var_list)    x1 = pd.DataFrame(np.array(x).reshape(1,feature_count),columns=var_list)    # 資料變化操作    y1 = transformFun(x1)    test_x = xgb.DMatrix(y1.drop(['mobile','mobile_md5'],xais=1),missing=float('nan'))    y1['score'] = bst.predict(test_x,ntree_limit=ntree_limit)    y2 = y1[['mobile','mobile_md5','score']]    return {'mobile': str(y2['mobile'][0]),'mobille_md5':str(y2['mobile_md5'][0]),'score':float(y2['score'][0])}

每條資料都轉化為 pd,增加了額外開銷。 最佳化後:

def cal_xgb_score(x,var_list,ntree_limit=50):    feature_count = len(var_list)    //將 iterator 轉為list     x1 = pd.DataFrame(list(x),columns=var_list)    ...    //將 pdf 轉為字典    return y1[['mobile','mobile_md5','score']].to_dict(orient='record')
二. toPandas 的例子 最佳化前:
df.toPandas()
最佳化後:
import pandas as pddef _map_to_pandas(rdds):    return [pd.DataFrame(list(rdds))]def toPandas(df, n_partitions=None):    if n_partitions is not None: df = df.repartition(n_partitions)    df_pand = df.rdd.mapPartitions(_map_to_pandas).collect()    df_pand = pd.concat(df_pand)    df_pand.columns = df.columns    return df_pand# 98列,22W行,類型 array/string/Long/Int,分區 200df = spark.sql("...").sample(False,0.002)df.cache()df.count()# 原生的 toPandas 方法%timeit df.toPandas()# 分布式的 toPandas%timeit toPandas(df)#使用 apache arrow,spark 版本2.3以上spark.sql("set spark.sql.execution.arrow.enabled=true")%timeit df.toPandas()
總結 一. xgboost 預測

單條資料處理速度從 120 record / min 提高到 3278 record / min

tips: 如果一個分區資料量過大將會導致 executor oom 二. spark dataframe 轉 pandas dataframe

type cost (seconds)
native toPandas 12
distributed toPandas 5.91
arrow toPandas 2.52

toPandas 返回的資料歸根結底還是緩衝在 driver 的記憶體中的,不建議返回過大的資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.