问题
I want to correlate s1 and s2 variables in my zip_list. However, I have this error:
"return multiarray.correlate2(a, v, mode) ValueError: object of too small depth for desired array"
Is there anyone who could help me?
s1 = []
s2 = []
date = []
for f in files:
with open(f) as f:
f.next()
rows = csv.reader(f)
for row in rows:
item_list = []
for row_item in row:
output_string = map(lambda x: '0' if x=='NULL' else x, row_item.split(","))
item_list.append(output_string)
date = item_list[0]
s1 = item_list[2]
s2 = item_list[3]
zip_list = []
for x, y in zip(s1, s2):
pos = {"s1": x, "s2": y}
zip_list.append(pos)
print zip_list
for line in zip_list:
print np.correlate(x,y)
input values:
s1: ['113']
['116']
['120']
['120']
['117']
['127']
['124']
['118']
['124']
['128']
['128']
['125']
['112']
['122']
['125']
['133']
['128']
s2: ['125']
['123']
['120']
['115']
['124']
['120']
['120']
['119']
['119']
['122']
['121']
['116']
['116']
['119']
['116']
['113']
zip_list: [{'s2': '114', 's1': '52'}]
[{'s2': '114', 's1': '52'}]
[{'s2': '121', 's1': '67'}]
[{'s2': '121', 's1': '67'}]
[{'s2': '124', 's1': '72'}]
[{'s2': '124', 's1': '72'}]
[{'s2': '124', 's1': '76'}]
[{'s2': '124', 's1': '76'}]
[{'s2': '122', 's1': '80'}]
[{'s2': '122', 's1': '80'}]
[{'s2': '115', 's1': '74'}]
[{'s2': '115', 's1': '74'}]
[{'s2': '114', 's1': '69'}]
[{'s2': '114', 's1': '69'}]
[{'s2': '115', 's1': '64'}]
[{'s2': '115', 's1': '64'}]
[{'s2': '111', 's1': '63'}]
[{'s2': '111', 's1': '63'}]
[{'s2': '112', 's1': '56'}]
[{'s2': '112', 's1': '56'}]
[{'s2': '116', 's1': '49'}]
[{'s2': '116', 's1': '49'}]
[{'s2': '119', 's1': '54'}]
[{'s2': '119', 's1': '54'}]
[{'s2': '119', 's1': '54'}]
回答1:
First, reducing your code to the bare minimum will give you more insight in where it fails:
import numpy as np
s1 = np.array([['113'],['116'],['120'],['120'],['117'],['127'],['124'],['118'],
['124'],['128'],['128'],['125'],['112'],['122'],['125'],['133'],['128']])
s2 = np.array([['125'],['123'],['120'],['115'] ,['124'],['120'],['120'],['119'],
['119'],['122'],['121'],['116'],['116'],['119'],['116'],['113']])
Those are two numpy arrays of 3-character-long strings:
>>> s1.dtype
dtype('<U3')
Correlating strings is not something you'd likely do with the numpy library (there exist other libraries that do word analyses), so you're most likely after using these as actual numbers. Convert them first:
s1 = s1.astype(np.int)
s2 = s2.astype(np.int)
Now, the error actually comes from your use of an identifier which was used only in a loop, but referenced outside of that loop. More specifically, your piece of code here:
zip_list = []
for x, y in zip(s1, s2):
pos = {"s1": x, "s2": y}
zip_list.append(pos)
for line in zip_list:
print np.correlate(x,y) # <- x and y here take the last known values
As shown in the comment I've added, x
and y
will refer to the last setting of these two identifiers, which was during their last run through the first for-loop. That means, at the line where you're trying to correlate, you're actually executing this piece of code:
np.correlate(np.array(['133'], dtype='<U3'), np.array(['113'], dtype='<U3')) # Doesn't make sense
Moreover, you're doing this over and over, for the exact same values because x and y have not been rebound to different values. Depending on which version of numpy you're using, you'll get a different error message. Mine differs a bit from yours but not by much.
If you really want to "correlate" the two numbers per line (unlikely, because that's the same as pairwise multiplication), you should change your second for-loop to this:
for a,b in zip_list:
print(np.correlate(a,b))
If you want to correlate the two one-dimensional arrays s1 and s2 though (which is rather likely), just get rid of the 2nd for-loop (and the first one isn't necessary either) and write:
>>> np.correlate(np.squeeze(s1), np.squeeze(s2)) # see note below
array([232662, 234543])
which is the correlation of 2 one-dimensional arrays (the squeeze
function gets rid of the unnecessary 2D nature) of unequal size (sizes m
and m+1
) using the function's "valid" mode.
来源:https://stackoverflow.com/questions/35426578/value-error-object-of-too-small-depth-for-desired-array