调用.predict()时,我得到ValueError:无法将字符串转换为float

2024-09-30 20:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我对数据科学的世界还比较陌生,我不明白为什么会出现以下错误:

Traceback (most recent call last):
  File "main.py", line 73, in <module>
    print(classifyAnswer(getAnswer(f_content)))
  File "main.py", line 67, in classifyAnswer
    SVM_predict = loaded_model.predict([array_answer])
  File "C:\Users\A144995\lib\site-packages\sklearn\svm\base.py", line 574, in predict
    y = super().predict(X)
  File "C:\Users\A144995\lib\site-packages\sklearn\svm\base.py", line 322, in predict
    X = self._validate_for_predict(X)
  File "C:\Users\A144995\lib\site-packages\sklearn\svm\base.py", line 454, in _validate_for_predict
    accept_large_sparse=False)
  File "C:\Users\A144995\lib\site-packages\sklearn\utils\validation.py", line 496, in check_array
    array = np.asarray(array, dtype=dtype, order=order)
  File "C:\Users\A144995\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'No'

正在读取的模型进行了拟合和建模,没有任何问题,但是当我对示例文本进行分类(从文本文件读取到字符串中)时,我得到了错误。我正在执行的代码如下:

f = open("samplechat.txt", 'r')
f_content = f.read()
f.close()

def questionFound(txt):

    x = re.search("(d|D)oes anyone in the household have (l|L)ife (s|S)upport or (m|M)edical (d|D)evices", str(txt))

    return x

def findAgentName(txt):

    return 0

def getAnswer(txt):

    x = re.search("((d|D)oes anyone in the household have (l|L)ife (s|S)upport or (m|M)edical (d|D)evices (that|which) depend (upon|on) supply\?)( *\[Read])( [\w+/g(,|\s)]+)([\d]+\s[\w]+\W\s[\d]+ [,]) ([\d]+:[\d]+[AM|PM|am|pm]+)\s([\w]+)", str(txt))

    return x.group(13)

def classifyAnswer(answer):

    array_answer = [answer]

    classifier = 'webchatls.sav'

    loaded_model = pickle.load(open(classifier, 'rb'))

    SVM_predict = loaded_model.predict([array_answer])

    result = SVM_predict[0]

    return result

print(classifyAnswer(getAnswer(f_content))) 

我对这个还是新手,所以任何关于调试这个的指导都是非常棒的


Tags: answerinpytxtreturnlibpackagesline