【问题标题】:How to assign Numpy array values to other variable如何将 Numpy 数组值分配给其他变量
【发布时间】:2018-12-26 17:57:58
【问题描述】:

这是我的代码:

y_predForThisMatchType = model.predict(X_test, num_iteration=model.best_iteration)
print(type(y_predForThisMatchType))
y_predForThisMatchType = y_predForThisMatchType.reshape(-1)
print(type(y_predForThisMatchType))

count = 0
for i in range (len(y_pred)):
    if y_pred.loc[i]  == abType:
        y_pred.loc[i] = y_predForThisMatchType[count]
        count = count + 1

输出:

类'numpy.ndarray'

类'numpy.ndarray'

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py:189:SettingWithCopyWarning:

试图在数据帧的切片副本上设置值

请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._setitem_with_indexer(indexer, value)

Python 只是打印上面的输出,仅此而已。该程序在技术上正在运行,但下面的代码没有被执行,没有显示真正的错误。

错误行: y_pred.loc[i] = y_predForThisMatchType[count]

y_pred 变量是 pandas 数据框。

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    您是否完全检查过您的输出?
    根据我的经验,您的代码正在运行。
    显示只是一个警告,可以通过以下方式禁用:

    pandas.options.mode.chained_assignment = None  # default='warn'
    

    【讨论】:

    • 是的,我已经检查了一切。
    • count = count + 1 你确定你的计数增量应该在if 内吗?还是应该是 1 级?
    • 是的,我确定。它应该在If里面
    • @John Doe 鉴于目前的信息,我无法辨别更多信息,抱歉
    • 有事,我现在要出去。我会更新这个问题,以便可以复制,并通知您。
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多