无法在python中调用该函数

时间:2017-06-28 12:38:47

标签: python pandas scikit-learn

j=pd.read_excel('train1.xls', 'sheet1', na_values=['NA', '?'],header=None)
j.columns=['News','Sentiment']
train = [(j.News,j.Sentiment)]
cl = DecisionTreeClassifier(train)

获取TypeError:basic_extractor()只需要2个参数(给定1个) 但是在使用以下代码时,我没有收到任何错误: -

train = [('I love this sandwich.', 'pos'),
    ('This is an amazing place!', 'pos'),
    ('I feel very good about these beers.', 'pos'),
    ('I do not like this restaurant', 'neg'),
    ('I am tired of this stuff.', 'neg'),
    ("I can't deal with this", 'neg'),
    ("My boss is horrible.", "neg")
 ]
cl = DecisionTreeClassifier(train)

这次它正在发挥作用。你知道第一种情况下的问题是什么吗?

1 个答案:

答案 0 :(得分:2)

我认为你需要zip

#for python 2 omit list
train = list(zip(j.News,j.Sentiment))

样品:

a = train = [('I love this sandwich.', 'pos'),
    ('This is an amazing place!', 'pos'),
    ('I feel very good about these beers.', 'pos'),
    ('I do not like this restaurant', 'neg'),
    ('I am tired of this stuff.', 'neg'),
    ("I can't deal with this", 'neg'),
    ("My boss is horrible.", "neg")
 ]
j = pd.DataFrame(a, columns=['News','Sentiment'])
print (j)
                                  News Sentiment
0                I love this sandwich.       pos
1            This is an amazing place!       pos
2  I feel very good about these beers.       pos
3        I do not like this restaurant       neg
4            I am tired of this stuff.       neg
5               I can't deal with this       neg
6                 My boss is horrible.       neg
train = list(zip(j.News,j.Sentiment))
print (train)
[('I love this sandwich.', 'pos'), ('This is an amazing place!', 'pos'), ('I feel very good about these beers.', 'pos'), ('I do not like this restaurant', 'neg'), ('I am tired of this stuff.', 'neg'), ("I can't deal with this", 'neg'), ('My boss is horrible.', 'neg')]