• tensorflow深度学习模型读取parquet数据进行训练实现


    hdfs下载parquet数据,并使用pandas读取。

    1. import pandas as pd
    2. df = pd.read_parquet(hfs_path)

    如果没有装过一些读取parquet相关的包,会报如下错误:

    1. ImportError: Unable to find a usable engine; tried using: 'pyarrow', 'fastparquet'.
    2. pyarrow or fastparquet is required for parquet support

    此时,安装下相关包即可。

    1. $ conda install -c conda-forge pyarrow
    2. $ conda install -c conda-forge fastparquet

    或者

    1. !pip install pyarrow
    2. !pip install fastparquet

    如果下载安装的速度太慢,可以切换不同的镜像源

    命令如下:pip  install fastparquet -i https://mirrors.cloud.tencent.com/pypi/simple

        ** 安装fastparquet的时候报错,报错信息是系统没有gcc命令。按照系统提示执行 yum install gcc

     附录:国内不同的镜像源地址:

    清华:https://pypi.tuna.tsinghua.edu.cn/simple

    阿里云:http://mirrors.aliyun.com/pypi/simple/

    腾讯云:https://mirrors.cloud.tencent.com/pypi/simple

    豆瓣:http://pypi.douban.com/simple/

    中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

    华中理工大学:http://pypi.hustunique.com/

    山东理工大学:http://pypi.sdutlinux.org/ 
    ————————————————

    成功读取parquet数据后,就可以进行数据读取、类型转换操作了。

    1. def preprocess_parquet_features(feature_hdfs_path):
    2. feature_local_path = os.path.join(os.getcwd(), "parquet_features")
    3. mkdir_local_path(feature_local_path)
    4. os.system("""hadoop fs -get {}/part-* {}""".format(feature_hdfs_path, feature_local_path))
    5. # 格式转换
    6. features = []
    7. for parent, dirnames, filenames in os.walk(feature_local_path):
    8. for filename in filenames:
    9. if filename.endswith('.parquet') and filename.startswith('part-'):
    10. feature_file = os.path.join(parent, filename)
    11. features.append(pd.read_parquet(feature_file))
    12. data = pd.concat(features, ignore_index=True)
    13. features = []
    14. for num in range(data.order_vector.size):
    15. features.append(data.order_vector[num]['values'].tolist() + data.view_vector[num][
    16. 'values'].tolist() + data.add_cart_vector[num][
    17. 'values'].tolist())
    18. features_df = pd.DataFrame(features)
    19. from sklearn.model_selection import train_test_split
    20. X_train_full, X_test, y_train_full, y_test = train_test_split(features_df,
    21. data.label,
    22. test_size=0.2,
    23. random_state=42)
    24. X_train, X_valid, y_train, y_valid = train_test_split(X_train_full,
    25. y_train_full,
    26. test_size=0.2,
    27. random_state=42)
    28. return X_train_full, X_test, y_train_full, y_test, X_train, X_valid, y_train, y_valid

  • 相关阅读:
    人工智能迷惑行为大赏
    HTML网页设计结课作业——基于HTML+CSS仿学校官网页面
    猿创征文|HCIE-Security Day56:入侵防御技术
    Dijkstra算法求最短路
    永磁同步电机转子位置估算专题——正交锁相环
    带你学会指针进阶
    Android开发之——Jetpack Compose布局(03)
    Java 网络编程 —— 非阻塞式编程
    堆-c语言实现
    HarmonyOS ArkUi ArkWeb加载不出网页问题踩坑
  • 原文地址:https://blog.csdn.net/eylier/article/details/126731931