AttributeError with numpy array in NEAT algorithm for OpenAI Gym integration

I’m working on a school project where I need to implement the NEAT algorithm with OpenAI Gym for a game environment. I keep running into an issue with numpy arrays and the append method.

The main problem happens when I try to process the observation data from the game environment. Here’s the error I get:

AttributeError: 'numpy.ndarray' object has no attribute 'append'

Here’s my current implementation:

import retro
import numpy as np
import neat
import cv2

game_env = retro.make('SonicTheHedgehog-Genesis', 'GreenHillZone.Act1')

def run_evaluation(population, configuration):
    for individual_id, individual in population:
        observation = game_env.reset()
        action = game_env.action_space.sample()
        
        height, width, channels = game_env.observation_space.shape
        
        new_height = int(height/8)
        new_width = int(width/8)
        
        network = neat.nn.RecurrentNetwork.create(individual, configuration)
        max_score = 0
        current_score = 0
        step_count = 0
        position_x = 0
        best_position = 0
        
        finished = False
        
        while not finished:
            game_env.render()
            step_count += 1
            observation = cv2.resize(observation, (new_height, new_width))
            observation = cv2.cvtColor(observation, cv2.COLOR_BGR2GRAY)
            observation = np.reshape(observation, (new_height, new_width))
            
            pixel_data = np.ndarray.flatten(observation)
            
            for row in observation:
                for pixel in row:
                    pixel_data.append(pixel)  # This line causes the error
            
            network_output = network.activate(pixel_data)
            observation, reward, finished, game_info = game_env.step(network_output)
            pixel_data.clear()

config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                   neat.DefaultSpeciesSet, neat.DefaultStagnation,
                   'config-feedforward')
population = neat.Population(config)
result = population.run(run_evaluation)

I’m trying to flatten the image data to feed it into the neural network but I’m clearly doing something wrong with the numpy array handling. Can someone help me understand what’s going wrong and how to fix it properly?

This is for my school project and I really want to understand the concept behind it.

You’re already flattening the observation correctly with observation.flatten(), but then you’re trying to populate it again with those nested loops. That’s the problem. After flattening, your pixel_data array already has all the pixel values - the manual loops are doing nothing except breaking things since numpy arrays don’t have append methods like lists do. Just ditch the entire double for loop section and the clear() call. Your flattened array is ready to go straight into the network. Also, move the pixel_data creation after your cv2 preprocessing steps instead of before, since you’re modifying the observation first anyway. Way cleaner for your NEAT setup.

You’re treating numpy arrays like Python lists, which is the problem. When you flatten with np.ndarray.flatten(observation), you’ve already got all the pixel values in one array. The nested loops after that are pointless - you’re doing extra work and trying to append to an array that’s already complete. Just use pixel_data = observation.flatten() and ditch the for loops plus that pixel_data.clear() line. The flatten method gives you a copy collapsed into one dimension, which is exactly what your neural network needs. Also, move the pixel_data creation outside your while loop - you’re recreating it every iteration anyway, so you’ll get better performance.

You’re mixing up numpy arrays with Python lists. When you use np.ndarray.flatten(observation), you get a flattened numpy array, but then you’re trying to use .append() and .clear() - those methods don’t exist for numpy arrays. Additionally, you’re flattening twice, which is pointless. Here’s the fix: change pixel_data = np.ndarray.flatten(observation) to pixel_data = observation.flatten() and remove the entire for loop that tries to append pixels. Also, remove pixel_data.clear() since numpy arrays don’t need manual clearing like lists. This will resolve your AttributeError and tidy up your code.

you’re trying to use list methods on numpy arrays - that won’t work. once you flatten with observation.flatten(), you’ve got all the pixel values you need. drop the double for loop and that clear() line. numpy handles the memory stuff automatically.