【问题标题】:cuPy error : Implicit conversion to a host NumPy array via __array__ is not allowed,cuPy 错误:不允许通过 __array__ 隐式转换为主机 NumPy 数组,
【发布时间】:2023-03-12 11:07:02
【问题描述】:

将数组转换为 cuPy 数组时出现此错误: TypeError: 不允许通过 array 隐式转换为主机 NumPy 数组,要显式构造 GPU 数组,请考虑使用 cupy.asarray(...) 要显式构造主机数组,请考虑使用 .to_array()

    '''
      import cudf
      import cuml
      import cupy

      hospital_data=cudf.read_csv('nin-health-facilities.csv')
      cupy_lat = cupy.asarray(hospital_data['latitude'])
      cupy_long =cupy.asarray(hospital_data['longitude'])
    '''

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-33-999a5fd5e344> in <module>()
      4 
      5 hospital_data=cudf.read_csv('nin-health-facilities.csv')
----> 6 cupy_lat = cupy.asarray(hospital_data['latitude'])
      7 cupy_long =cupy.asarray(hospital_data['longitude'])

1 frames
cupy/_core/core.pyx in cupy._core.core.array()

cupy/_core/core.pyx in cupy._core.core.array()

cupy/_core/core.pyx in cupy._core.core._send_object_to_gpu()

/usr/local/lib/python3.7/site-packages/cudf/core/frame.py in __array__(self, dtype)
   1649     def __array__(self, dtype=None):
   1650         raise TypeError(
-> 1651             "Implicit conversion to a host NumPy array via __array__ is not "
   1652             "allowed, To explicitly construct a GPU array, consider using "
   1653             "cupy.asarray(...)\nTo explicitly construct a "

TypeError: Implicit conversion to a host NumPy array via __array__ is not allowed, To explicitly construct a GPU array, consider using cupy.asarray(...)
To explicitly construct a host array, consider using .to_array()

【问题讨论】:

    标签: python arrays cupy rapids


    【解决方案1】:

    使用cupy.asarray(series) 将 cuDF 系列转换为 CuPy 数组需要与 CuPy 兼容的数据类型。您可能需要仔细检查您的 Series 是 int、float 或 bool 类型,而不是 string、decimal、list 或 struct 类型。

    import cudf
    import cupy
    ​
    s = cudf.Series([0,1,2])
    cupy.asarray(s)
    array([0, 1, 2])
    

    隐式转换错误消息的目的是表明您不能通过数组协议将 GPU 对象传递给通常需要 CPU 对象的函数。在这种情况下,它可能是一个红鲱鱼。

    import cudf
    import cupy
    ​
    s = cudf.Series(["a","b","c"])
    cupy.asarray(s)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    /tmp/ipykernel_53245/4047643563.py in <module>
          3 
          4 s = cudf.Series(["a","b","c"])
    ----> 5 cupy.asarray(s)
    ...
    TypeError: Implicit conversion to a host NumPy array via __array__ is not allowed, To explicitly construct a GPU array, consider using cupy.asarray(...)
    To explicitly construct a host array, consider using .to_array()
    

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多