Python TypeError:字符串索引必须是整数。不知道我做错了什么

Python TypeError:字符串索引必须是整数。不知道我做错了什么,python,typeerror,naivebayes,particle-swarm,Python,Typeerror,Naivebayes,Particle Swarm,我有一个数据集,有两列,一列标识电子邮件是否被归类为垃圾邮件,另一列显示电子邮件内容。我一直在尝试用PSO和ABC实现朴素贝叶斯。但是我得到了错误TypeError:字符串索引必须是整数 email_train,email_test,spam_train,spam_test train_test_split(dfTotal.Email,dfTotal.Spam,test_size=0.3,random_state=0) email_test_dtm = cv.transform(email_t

我有一个数据集,有两列,一列标识电子邮件是否被归类为垃圾邮件,另一列显示电子邮件内容。我一直在尝试用PSO和ABC实现朴素贝叶斯。但是我得到了错误TypeError:字符串索引必须是整数

email_train,email_test,spam_train,spam_test train_test_split(dfTotal.Email,dfTotal.Spam,test_size=0.3,random_state=0)

email_test_dtm = cv.transform(email_test)
# convert to TFIDF form
email_test_tf = tf.fit_transform(email_test_dtm)
email_test_tf
人工蜂群

 from Hive import Hive 
from Hive import Utilities
from sklearn.metrics import log_loss

# ---- SOLVE TEST CASE WITH ARTIFICIAL BEE COLONY ALGORITHM

def run(lowBounds,upBounds,evaluator):

    model = Hive.BeeHive(lower = lowBounds, # MUST BE A LIST !
                         upper = upBounds , # MUST BE A LIST ! 
                         fun       = evaluator ,
                         numb_bees =  100       ,
                         max_itrs  =  2       ,)

    # runs model
    cost,sol = model.run()

    # plots convergence
    Utilities.ConvergencePlot(cost)

    # prints out best solution
    print("Fitness Value ABC: {0}".format(model.best))
    ABC_model = MultinomialNB(alpha=10**sol[0]).fit(email_train_tf,spam_train) # Create the optimized model with best parameter
    result = ABC_model.predict(email_test_tf) # predict with the ABC_model
    return sol,result
进口光学性

 import optunity
    import optunity.metrics
朴素贝叶斯

from sklearn.naive_bayes import MultinomialNB

nb = MultinomialNB()
nb.get_params()
# fit tf-idf representation to NB model
nb.fit(email_train_tf, spam_train)

# class predictions for testing set
result1 = nb.predict(email_test_tf)

def evaluator(params):

    nBayes = MultinomialNB(alpha=10**params[0]).fit(email_train_tf,spam_train)
    
    
    pred_proba = nBayes.predict_proba(email_test_tf)
    return log_loss(spam_test,pred_proba)
    

sol,result3 = run([-2],[1],evaluator)
我收到的回溯如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-006c296828ed> in <module>
      8 
      9 
---> 10 sol,result3 = run([-2],[1],evaluator)

<ipython-input-32-6852d973eb15> in run(lowBounds, upBounds, evaluator)
     19 
     20     # plots convergence
---> 21     Utilities.ConvergencePlot(cost)
     22 
     23     # prints out best solution

c:\users\lidak\article\src\hive\Hive\Utilities.py in ConvergencePlot(cost)
     55     labels = ["Best Cost Function", "Mean Cost Function"]
     56     plt.figure(figsize=(12.5, 4));
---> 57     plt.plot(range(len(cost["best"])), cost["best"], label=labels[0]);
     58     plt.scatter(range(len(cost["mean"])), cost["mean"], color='red', label=labels[1]);
     59     plt.xlabel("Iteration #");

TypeError: string indices must be integers

<Figure size 900x288 with 0 Axes>
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
8.
9
--->10 sol,result3=运行([-2],[1],计算器)
运行中(下限、上下限、评估器)
19
20#图收敛
--->21.公用设施、汇流图(成本)
22
23#打印出最佳解决方案
c:\users\lidak\article\src\hive\hive\Utilities.py在ConvergencePlot中(成本)
55标签=[“最佳成本函数”、“平均成本函数”]
56 plt.图(figsize=(12.5,4));
--->57 plt.绘图(范围(长度(成本[“最佳”),成本[“最佳”,标签=标签[0]);
58 plt.散射(范围(透镜(成本[“平均值]))、成本[“平均值]、颜色=红色、标签=标签[1]);
59 plt.xlabel(“迭代#”);
TypeError:字符串索引必须是整数

看起来像
成本
是一个字符串吗?我如何修复itcheck什么是
成本
与你期望的是什么,并努力使它成为你期望的样子。按照其他用户的建议,你正在使用什么版本的python?看起来像
成本
是一个字符串吗?我如何修复itcheck什么是
成本
与你期望的是什么你期望它是什么,并努力使它成为你期望的样子。遵循其他用户的建议,你使用的是什么版本的python?