我正在使用Python3进行成像,并使用SVM对模型进行训练。但是当我运行“KeyError:'NU'”时,我得到了错误。我怎样才能解决这个问题

2024-10-04 09:24:47 发布

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

错误是什么意思KeyError:“NU”以及如何解决此问题 我真的找不到正确的解决办法。我有两门课,分别是“u”和“UN” 所有代码都能正常工作,但最后一节mySVM模型的准确性给出了错误。我还添加了classWisedata

CLASSES = len(np.unique(labels))
print(CLASSES)


"""Function to Create the dictionary of labels as Keys 
    and their corresponding list of images as Values """

def classWiseData(x,y):      
    data = {}

    for i in range(CLASSES):
        data[i] = []

    for i in range(x.shape[0]):
        data[y[i]].append(x[i])

    for k in data.keys():
        data[k] = np.array(data[k])

    return data


def accuracy(x,y):

    count = 0
    for i in range(x.shape[0]):
        prediction = predict(x[i])

        #Checking Predictions with Labels
        if(prediction==y[i]):
            count += 1

    return count/x.shape[0]
--------------------------------------------------------------------------------------------------------
data = classWiseData(image_data,labels)
svm_classifiers = trainSVMs(image_data,labels)

print("Accuracy of mySVM model: ",accuracy(image_data,labels))

--------------------------------------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)
<ipython-input-231-3a65bb35ee50> in <module>
----> 1 data = classWiseData(image_data,labels)
      2 svm_classifiers = trainSVMs(image_data,labels)
      3 
      4 print("Accuracy of mySVM model: ",accuracy(image_data,labels))

<ipython-input-214-329a3085304d> in classWiseData(x, y)
     13 
     14     for i in range(x.shape[0]):
---> 15         data[y[i]].append(x[i])
     16 
     17     for k in data.keys():

KeyError: 'NU'

--------------------------------------------------------------------------------------------------------


from sklearn import svm

svm_classifier = svm.SVC(kernel='linear',C=1.0)

svm_classifier.fit(image_data,labels)

print("Accuracy of sklearn SVM model: ",svm_classifier.score(image_data,labels))s

--------------------------------------------------------------------------------------------------------

请帮我做这个。我第一次在SVM上工作


Tags: ofinimagefordatalabelsrangeclasses