【问题标题】:TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe' in seabornTypeError:无法根据 seaborn 中的“安全”规则将数组数据从 dtype('int64') 转换为 dtype('int32')
【发布时间】:2020-06-06 04:25:58
【问题描述】:

我的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

data = sns.load_dataset("tips")

sns.lineplot(x="total_bill",y = "size",data = data)

当我执行最后一行时,它给出了类型错误提示

TypeError: 无法将数组数据从 dtype('int64') 转换为 dtype('int32') 根据规则 'safe'

请帮我解决它。提前致谢。

【问题讨论】:

  • 我无法重现您的错误(sns.__version__=0.9.0,pd.__version__=0.25.3,matplotlib.__version__=3.1.1)。
  • 我有 numpy 32 位我认为这可能是 import numpy.distutils.system_info as sysinfo sysinfo.platform_bits 32 的原因

标签: python machine-learning seaborn valueerror


【解决方案1】:

您可以像这样将数组转换为 int32 之前:

x = np.array([1, 2, 3, 4], dtype=np.int64)
print(type(x[0]))

>> <class 'numpy.int64'>

x = np.array(x, dtype=np.int32)
print(type(x[0]))

>> <class 'numpy.int32'>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-20
    • 2021-11-09
    • 1970-01-01
    • 2019-03-11
    • 2020-09-06
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多