numpy的不会追加数组 [英] NumPy Won't Append Arrays

查看:300
本文介绍了numpy的不会追加数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在玩剪刀,石头,布神经网络,但我碰到的一个巨大的问题。

I'm currently working on a neural network to play Rock-Paper-Scissors, but I've run into an enormous issue.

我有神经网络predict未来会发生什么基于三个动作,其中由人的一举一动,新列表是一个数组,一个历史,它包含两个previous移动和新。神经网络训练,然后和学习了这一点。我为code可以在下面找到。

I'm having the neural network predict what will happen next based on a history of three moves, where with every move by the human, a new list is made in an array that contains the two previous moves and the new one. The neural network then trains and learns off of this. My code for that can be found below.

#add new situation, with what is currently happening to make current prediction with adjusted weights

current_turn = np.array([[input_data[len(input_data) - 1][1], input_data[len(input_data) - 1][2], output_data[len(output_data) - 1][0]]])
np.append(input_data, current_turn, axis = 0)

我使用了Python系统numpy的,并且它拒绝这两个数组追加,使得神经网络不是学习。

I'm using the Python system NumPy, and it is refusing to append these two arrays, such that the neural network isn't learning.

修改的:一是认识到必须重新指定数组这个新追加数组的答复。当我尝试这样做以后,如下图所示,它再次是行不通的。

Edit: One of the responses recognized that one must reassign the array to this newly appended array. When I tried this later on, as shown below, it once again would not work.

if human_choice == "r":
        output_data = np.append(output_data, ([0]))
elif human_choice == "p":
        output_data = np.append(output_data, ([0.5]))
elif human_choice == "s":
        output_data = np.append(output_data, ([1]))

有没有更好的方式加入这些阵列使得算法可以学到什么?

Is there a better way to join these arrays such that the algorithm can learn?

注:追加没有得出任何错误,但似乎没有做的工作

推荐答案

作为的文档说:

值被追加到的复制的数组的。

Values are appended to a copy of this array.

(重点煤矿)。

所以 np.append 创建一个新的列表,而不是你原来的修改。你必须写:

So np.append creates a new list instead of modification of your original one. You have to write:

input_data = np.append(input_data, current_turn, axis = 0)

例如:

import numpy as np
my_array = np.array([1, 2, 3])
print(my_array) 
# [1 2 3]
my_array = np.append(my_array, [4])
print(my_array)
# [1 2 3 4]

另请参阅这个问题如果有兴趣为什么 np.append 行为以这样的方式。

See also this question if are interested why np.append behaves in such a way.

这篇关于numpy的不会追加数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆