TypeError:无法散列的类型:'numpy.ndarray' [英] TypeError: unhashable type: 'numpy.ndarray'

查看:100
本文介绍了TypeError:无法散列的类型:'numpy.ndarray'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个包含三列数据的文本文件中,我希望能够仅从第一列中的值等于above中定义的值的所有三列中获取一个slice数据.然后,我想将数据切片放入名为slice的新数组中(我正在使用 Python 2.7 )

From a text file containing three columns of data I want to be able to just take a slice of data from all three columns where the values in the first column are equal to the values defined in above. I then want to put the slice of data into a new array called slice (I am using Python 2.7)

above = range(18000, 18060, 5)

data = np.loadtxt(open('data.txt'), delimiter=None)

energies = (np.hsplit(data, 3))[0]

slice = set(energies)&set(above)

以上内容返回:

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    set(energies)&set(above)
TypeError: unhashable type: 'numpy.ndarray

推荐答案

您的变量energies可能具有错误的形状:

Your variable energies probably has the wrong shape:

>>> from numpy import array
>>> set([1,2,3]) & set(range(2, 10))
set([2, 3])
>>> set(array([1,2,3])) & set(range(2,10))
set([2, 3])
>>> set(array([[1,2,3],])) & set(range(2,10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'numpy.ndarray'

如果您使用自己的方法读取列数据,就会发生这种情况:

And that's what happens if you read columnar data using your approach:

>>> data
array([[  1.,   2.,   3.],
       [  3.,   4.,   5.],
       [  5.,   6.,   7.],
       [  8.,   9.,  10.]])
>>> hsplit(data,3)[0]
array([[ 1.],
       [ 3.],
       [ 5.],
       [ 8.]])

也许您可以简单地使用

>>> data[:,0]
array([ 1.,  3.,  5.,  8.])

相反.

(P.S.您的代码似乎不确定是data还是elementdata.我认为这只是一个错字.)

(P.S. Your code looks like it's undecided about whether it's data or elementdata. I've assumed it's simply a typo.)

这篇关于TypeError:无法散列的类型:'numpy.ndarray'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆