【发布时间】:2020-08-20 20:39:02
【问题描述】:
我正在尝试使用 SVM 在论文上复制一个实验,以增加我在机器学习方面的学习/知识。在本文中,作者提取特征并选择特征大小。他,然后给出一个表格,其中F代表特征向量的大小,N代表人脸图像
然后他使用 F >= 9 和 N >= 15 个参数。
现在,我想做的是像他在论文中那样实际抓取我提取的特征。
基本上,这就是我提取特征的方式:
def load_image_files(fullpath, dimension=(64, 64)):
descr = "A image classification dataset"
images = []
flat_data = []
target = []
dimension=(64, 64)
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for person in os.listdir(path):
personfolder = os.path.join(path, person)
for imgname in os.listdir(personfolder):
class_num = CATEGORIES.index(category)
fullpath = os.path.join(personfolder, imgname)
img_resized = resize(skimage.io.imread(fullpath), dimension, anti_aliasing=True, mode='reflect')
flat_data.append(img_resized.flatten())
images.append(skimage.io.imread(fullpath))
target.append(class_num)
flat_data = np.array(flat_data)
target = np.array(target)
images = np.array(images)
print(CATEGORIES)
return Bunch(data=flat_data,
target=target,
target_names=category,
images=images,
DESCR=descr)
如何选择提取和存储的特征数量?或者我如何手动存储具有我需要的特征数量的向量?例如一个大小为 9 的特征向量
我正在尝试以这种方式分离我的功能:
X_train, X_test, y_train, y_test = train_test_split(
image_dataset.data, image_dataset.target, test_size=0.3,random_state=109)
model = ExtraTreesClassifier(n_estimators=10)
model.fit(X_train, y_train)
print(model.feature_importances_)
不过,我的输出是:
[0. 0. 0. ... 0. 0. 0.]
对于 SVM 分类,我尝试使用 OneVsRestClassifier
model_to_set = OneVsRestClassifier(SVC(kernel="poly"))
parameters = {
"estimator__C": [1,2,4,8],
"estimator__kernel": ["poly", "rbf"],
"estimator__degree":[1, 2, 3, 4],
}
model_tunning = GridSearchCV(model_to_set, param_grid=parameters)
model_tunning
model_tunning.fit(X_train, y_train)
prediction = model_tunning.best_estimator_.predict(X_test)
然后,一旦我调用预测,我得到:
Out[29]:
array([1, 0, 4, 2, 1, 3, 3, 0, 1, 1, 3, 4, 1, 1, 0, 3, 2, 2, 2, 0, 4, 2,
2, 4])
【问题讨论】:
-
您能否提供
target的结构 - 这是您的功能当前所在的位置。在不了解各个元素是什么的情况下,提出具体的解决方案具有挑战性 -
我用我用来提取特征的函数编辑了这个问题
标签: python numpy feature-extraction feature-selection