alli alli - 2 months ago 29x
Python Question

Numpy std calculation: TypeError: cannot perform reduce with flexible type

I am trying to read lines of numbers starting at line 7 and compiling the numbers into a list until there is no more data, then calculate standard deviation and %rms on this list. Seems straightforward but I keep getting the error:

Traceback (most recent call last):
File "", line 21, in <module>
std = np.std(values)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/", line 2817, in std
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/", line 116, in _std
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/", line 86, in _var
arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, keepdims=True)
TypeError: cannot perform reduce with flexible type

Here is my code below:

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 < 400 :
if lines[line_number] == '\n':
variables = lines[line_number].split()
line_number = line_number + 3
print values

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

EDIT: the rms calculation now works, but I only seem to be reading the 7th line and not every 3rd line after that like I would like to. Anybody have any ideas on how to best do this? Getting error code:

Traceback (most recent call last):
File "", line 13, in <module>
if lines[line_number] == '\n':
IndexError: list index out of range


values is a string array and so is a. Convert a into a numeric type using astype. For example,

a = np.asarray(values).astype(np.float32)
std = np.std(a)