tdh
tdh

Reputation: 872

NumPy Won't Append Arrays

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

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)

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?

Note: The "append" isn't drawing any errors, yet seems to not do its job.

Upvotes: 1

Views: 6636

Answers (2)

stackfy
stackfy

Reputation: 11

You can use numpy.concatenate method instead.

import numpy as np
arr = np.array([1, 2])

arr = np.concatenate((arr,[4]))
print(arr)
# [1 2 3 4]

see docs for more help: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.concatenate.html

Upvotes: 0

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8047

As the docs says,

Values are appended to a copy of this array.

(emphasis mine).

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)

Example:

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]

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

Upvotes: 5

Related Questions