TypeError:未确定对象的len()(SVM:将预测分数保存到csv文件)

时间:2017-05-29 11:09:51

标签: python image-processing classification svm

我想根据红色直方图值对一组图像进行分类。我们有3种类型的图像,目标是预测每个图像属于哪种类型。我可以将它保存到csv,显示图像所属的类型,但我也想显示预测分数,这给了我一个错误。这是代码:

import cv2
import os
import glob
import numpy as np
from skimage import io
import pandas as pd

root = "C:/Users/Ioanna/data/train"
folders = ["Type_1", "Type_2", "Type_3"]
extension = "*.jpg"


def file_is_valid(filename):
    try:
        io.imread(filename)
        return True
    except:
        return False

def compute_red_histogram(root, folders, extension):
    X = [] #2D array : rows=number of images
    y = [] #1D array : numeric class labels (0 for Type_1, 1 for Type_2, 2 for Type_3)
    for n, imtype in enumerate(folders):
        filenames = glob.glob(os.path.join(root, imtype, extension))
        for fn in filter(file_is_valid, filenames):
            print(fn)
            image = io.imread(fn)
            img = cv2.resize(image, None, fx=0.1, fy=0.1, interpolation=cv2.INTER_AREA)
            red = img[:, :, 0]
            h, _ = np.histogram(red, bins=np.arange(257), normed=True)
            X.append(h)
            y.append(n)
    return np.vstack(X), np.array(y)

X, y = compute_red_histogram(root, folders, extension)


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.5, random_state = 0)

# train svm classifier
from sklearn.svm import SVC
clf = SVC()
clf.fit(X_train, y_train)

y_test
clf.predict(X_test)
y_test == clf.predict(X_test)
score = clf.score(X_test, y_test)

prediction = pd.DataFrame(y_test, score, columns=['prediction', 'score']).to_csv('prediction.csv')

我得到了这个: TypeError:未确定对象的len() enter image description here

0 个答案:

没有答案