NumPy Wont Append Arrays

2024/10/13 0:36:06

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 weightscurrent_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.

Answer

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.

https://en.xdnf.cn/q/118140.html

Related Q&A

(Django) Limited ForeignKey choices by Current User in UpdateView

I recently was able to figure out how to do this in the CreateView, but the same is not working for the UpdateView (Heres the original post on how to do it in the CreateView: (Django) Limited ForeignKe…

Merge list concating unique values as comma seperated retaining original order from csv

Here is my data:data.csvid,fname,lname,education,gradyear,attributes 1,john,smith,mit,2003,qa 1,john,smith,harvard,207,admin 1,john,smith,ft,212,master 2,john,doe,htw,2000,devHere is the code:from iter…

Unable to submit Spark job from Windows IDE to Linux cluster

I just read about findspark and found it quite interesting, as so far I have only used spark-submit which isnt be suited for interactive development on an IDE. I tried executing this file on Windows 10…

Updating variable values when running a thread using QThread in PyQt4

So problem occurred when I tried using Threading in my code. What I want to do is passing default values to the def __init__ and then calling the thread with its instance with updated values but someho…

Show terminal status in a Tkinter widget

I am using python2.7.10 on MacOs Sierra and have created a rsync over ssh connection with my raspberrypi. The Idea is to synchronize my local folder with my remote folder on the raspberrypi. My functio…

Python Convert HTML into JSON using Soup

These are the rulesThe HTML tags will start with any of the following <p>, <ol> or <ul> The content of the HTML when any of step 1 tags is found will contain only the following tags: …

I/O Error while saving Excel file - Python

Im using python to open an existing excel file and do some formatting and save and close the file. My code is working good when the file size is small but when excel size is big (apprx. 40MB) Im gettin…

How to stop the python turtle from drawing

Can anyone tell me why this code always has a line on the screen and also how to stop it?Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try…

Replace values in a string

So the challenge was to replace a specific word in a sentence with asterisks with equivalent length to that word - 3 letters 3 asterisks etc.Section One does not work, but Section Two does - can anyon…

Select n data points from plot

I want to select points by clicking om them in a plot and store the point in an array. I want to stop selecting points after n selections, by for example pressing a key. How can I do this? This is wha…