Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have this code that reads numbers and is meant to calculate std and %rms using numpy

import numpy as np
import glob
import os

values = []
line_number = 6

road = '/Users/allisondavis/Documents/HCl'
for pbpfile in glob.glob(os.path.join(road, 'pbpfile*')): 
    lines = open(pbpfile, 'r').readlines()
    while line_number < len(lines) :
        variables = lines[line_number].split()
        values.append(variables)
        line_number = line_number + 3

a = np.asarray(values).astype(np.float)
std = np.std(a)
rms = std * 100
print rms

However I keep getting the error code:

Traceback (most recent call last):
File "rmscalc.py", line 17, in <module>
a = np.asarray(values).astype(np.float)
ValueError: setting an array element with a sequence.

Any idea how to fix this? I am new to python/numpy. If I print my values it looks something like this:

[[1,2,3,4],[2,4,5,6],[1,3,5,6]]
share|improve this question
    
nope @bernie didn't work – alli Aug 5 at 18:03
    
Look at the pieces of the expression to see what exactly is giving the problem. Look at (print) values. Try a=np.array(values). If it works print it. Check its dtype. – hpaulj Aug 5 at 18:13
    
I am able to print values. When doing a=np.array(values) - nothing new happens. the dtype is numpy.ndarray – alli Aug 5 at 18:21
    
I think the problem might be that each line of data is it's own "array" inside the list...is there a way to do this differently? – alli Aug 5 at 18:22
    
Just to test something out, could you try using tuples for your sub-sequences? Like: values.append(tuple(variables)) – Travis Vaught Aug 5 at 18:36

I can think of a modification to your code which can potentially fix your problem:

Initialize values as a numpy array, and use numpy append or concatenate:

values = np.array([], dtype=float)

Then inside loop:

values = np.append(values, [variables], axis=0)
# or
variables = np.array(lines[line_number].split(), dtype=float)
values = np.concatenate((values, variables), axis=0)


Alternatively, if you files are .csv (or any other type Pandas can read):

import pandas as pd
# Replace `read_csv` with your appropriate file reader

a = pd.concat([pd.read_csv(pbpfile)
               for pbpfile in glob.glob(os.path.join(road, 'pbpfile*'))]).values
# or
a = np.concatenate([pd.read_csv(pbpfile).values
                    for pbpfile in glob.glob(os.path.join(road, 'pbpfile*'))], axis=0)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.