1

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.

3
  • 1
    If you know the problem is to do with appending arrays, can you boil down your question into an example that's just about that? CBA to wade through large amounts of code that are highly specific to the neural-network/rock-paper-scissors context Commented Jan 4, 2016 at 18:56
  • @jez Absolutely. I've made the change. Commented Jan 4, 2016 at 19:11
  • You are trying to use append as though it were a list method. But np.append is not a method, and does not work 'in-place'. It is just another way of calling np.concatenate. Commented Jan 4, 2016 at 20:06

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the answer. When I did this, it didn't work on another append later in the program. I will add this to the question immediately.
I've made the edit. I would sincerely appreciate your input.
@tdh, could you please explain, what does it mean "it once again would not work"? What is hapenning and what should happen?
@tdh, that seems to be strange. I added an example to the answer which definitely works for me. Could you please reproduce your problem in a similar way (e.g. create MCVE, see stackoverflow.com/help/mcve)? Or provide more debug info.
|
0

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

1 Comment

I sort of wish they never created the append function. This is a typical example of its misuse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.