Skip to main content
 首页 » 编程设计

python之无法调用python中的函数

2024年10月01日9haluo1

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) 

这次它起作用了。你知道第一种情况的问题是什么吗?

请您参考如下方法:

我想你需要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')]