unorderable类型:str()< INT()

时间:2016-12-22 22:03:01

标签: python-3.x pandas numpy

我正在使用pandas,numpy和sklearn设计一个基本的垃圾邮件分类器程序( python 3 )但是我收到此错误并且无法识别位置。我试图查看不同变量的数据类型,但没有找到位置。 (火腿=非垃圾邮件)。输入文件必须对此错误无效,因为它使用python 2.7 它的包/模块兼容性或数据类型转换错误。

import os
import io
import numpy
from pandas import DataFrame
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

def readFiles(path):
    for root, dirnames, filenames in os.walk(path):
        for filename in filenames:
            path = os.path.join(root, filename)

            inBody = False
            lines = []
            f = io.open(path, 'r', encoding='latin1')
            for line in f:
                if inBody:
                    lines.append(line)
                elif line == '\n':
                    inBody = True
            f.close()
            message = '\n'.join(lines)
            yield path, message


def dataFrameFromDirectory(path, classification):
    rows = []
    index = []
    for filename, message in readFiles(path):
        rows.append({'message': message, 'class': classification})
        index.append(filename)

    return DataFrame(rows, index=index)

data = DataFrame({'message': [], 'class': []})

data = data.append(dataFrameFromDirectory('D:/emails/spam', 'spam'))
data = data.append(dataFrameFromDirectory('D:/emails/ham', 'ham'))

来自ipython NoteBook的堆栈跟踪:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-555887356cc2> in <module>()
      3 import numpy
      4 from pandas import DataFrame
----> 5 from sklearn.feature_extraction.text import CountVectorizer
      6 from sklearn.naive_bayes import MultinomialNB
      7 

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\__init__.py in <module>()
     55 else:
     56     from . import __check_build
---> 57     from .base import clone
     58     __check_build  # avoid flakes unused variable error
     59 

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\base.py in <module>()
     10 from scipy import sparse
     11 from .externals import six
---> 12 from .utils.fixes import signature
     13 from .utils.deprecation import deprecated
     14 from .exceptions import ChangedBehaviorWarning as _ChangedBehaviorWarning

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\utils\__init__.py in <module>()
      9 
     10 from .murmurhash import murmurhash3_32
---> 11 from .validation import (as_float_array,
     12                          assert_all_finite,
     13                          check_random_state, column_or_1d, check_array,

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\utils\validation.py in <module>()
     16 
     17 from ..externals import six
---> 18 from ..utils.fixes import signature
     19 from .deprecation import deprecated
     20 from ..exceptions import DataConversionWarning as _DataConversionWarning

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\utils\fixes.py in <module>()
    404 
    405 
--> 406 if np_version < (1, 12, 0):
    407     class MaskedArray(np.ma.MaskedArray):
    408         # Before numpy 1.12, np.ma.MaskedArray object is not picklable

TypeError: unorderable types: str() < int()

1 个答案:

答案 0 :(得分:4)

我的版本集 - 相对较新,但不是最先进的版本:

In [509]: import sklearn
In [510]: sklearn.__version__
Out[510]: '0.17'
In [511]: np.__version__
Out[511]: '1.11.2'
In [512]: sklearn.utils.fixes._parse_version(np.__version__)
Out[512]: (1, 11, 2)
In [513]: sklearn.utils.fixes._parse_version(np.__version__)<(1,12,0)
Out[513]: True

最后一步是将从np.__version__字符串派生的一个元组与另一个元组进行比较。

我建议尽可能导入和打印:

np.__version__
scipy.__version__
sys.version
scklearn.__version__

按照Andras链接,问题是numpy版本号。如果numpy是新的测试版,则版本号的0b1部分会给出此测试问题。

In [517]: sklearn.utils.fixes._parse_version('1.12.0b1')<(1,12,0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-517-a2d159f6d08a> in <module>()
----> 1 sklearn.utils.fixes._parse_version('1.12.0b1')<(1,12,0)

TypeError: unorderable types: str() < int()

最简单的解决方案,如果可能的话,回到常规的numpy版本(类似'1.11.2'),而不是测试版。

如果这确实是一个numpy / sklearn版本问题,OP的反对票是不公平的。