Skip to main content
 首页 » 编程设计

python之精度为 0% 的 SVC 分类器

2025年02月15日7tintown

我正在使用 Sklearn 的 SVC 来区分不同的矩阵。 数据是 95 个相关矩阵,根据精神 split 症患者(50 个矩阵)和健康对照者(45 个矩阵)的 IRM 计算得出。它们相当大 (264*264),所以我并没有期待完美的结果,但 0% 的准确率似乎真的很低。

数据:95个264*264的矩阵,值在[-1,1]中

代码

代码如下:

## Datas 
#control_matrices: list of 45 matrices 
#patient_matrices: list of 50 matrices 
 
n_training = 25 #Number of matrices of control to train SVC (25 control and 25 patient) 
indices = np.triu_indices(264,1) #Since the matrices are symetric, I just take the upper triangle 
 
perm_control = np.random.permutation(45) #Doing a permutation to take random matrices for training 
contr_matrices = control_matrices[perm_control] #control_matrices is a list of matrices 
perm_patient = np.random.permutation(50) #Same with the patient matrices 
pat_matrices = patient_matrices[perm_patient]  
 
x_control = [m[indices] for m in contr_matrices[:n_training]] #Data for training 
x_patient = [m[indices] for m in pat_matrices[:n_training]] 
 
test_control = [m[indices] for m in contr_matrices[n_training:]] #Data for test once the SVM is trained 
test_patient = [m[indices] for m in pat_matrices[n_training:]] 
 
X = np.concatenate((x_control, x_patient)) 
Y = np.asarray( n_training*[0] + n_training*[1] ) #Control: 0 - Patient: 1 
 
 
perm = np.random.permutation(50) 
X = X[perm] 
Y = Y[perm] 
 
## Training 
 
clf = SVC() 
clf.fit(X,Y) 

预期结果

由于数据的大小与矩阵的数量相比是巨大的,我预计结果会很低(只比 50% 好一点点)。

实际结果

clf.score(np.concatenate((test_control, test_patient)), 20*[0]+25*[1]) 
 
>>> 0.0 

每当我运行代码(因此,使用不同的排列),以及从 10 到 45 的 n_training 时,都会发生同样的情况。但是 SVC 确实记得第一个矩阵,用于训练(clf.score(X,Y) 为 1.0。

其他尝试

clf=LinearSVC() 和 clf=LogisticRegression() 也是如此。

我也试过了,结果完全一样:

from sklearn.cross_validation import StratifiedKFold, cross_val_score 
from nilearn import connectome 
 
connectivity_coefs = connectome.sym_to_vec(matrices, ConnectivityMeasure)  
# This turns the matrices to a list of vectors 
 
Y = 45*[0] + 50*[1] 
 
cv = StratifiedKFold(Y, n_folds=3, shuffle=True) 
svc = LinearSVC() 
 
cv_scores = cross_val_score(svc, connectivity_coefs, Y, cv=cv, scoring='accuracy') 
 
print('Score: %1.2f +- %1.2f' % (cv_scores.mean(), cv_scores.std())) 
 
>>> Score: 0.00 +- 0.00 

我还尝试使用更简单的数据:矩阵 [0] 用于控制,[1] 用于患者。 SVC 工作得很好,所以首先我怀疑它与我使用的矩阵的大小有关(巨大的大小和很少的样本)。

但使用 matrices = np.random.rand(95,264,264),我得到 Score: 0.58 +- 0.03。

使用完整的矩阵而不是只计算上三角,我的准确率仍然是 0%。

我完全不明白这里发生了什么。

版本

Windows-8-6.2.9200 
Python 3.4.1 |Continuum Analytics, Inc.| (default, May 19 2014, 13:02:30) [MSC v.1600 64 bit (AMD64)] 
NumPy 1.9.1 
SciPy 0.15.1 
Scikit-Learn 0.15.2 

数据

这是获取我使用的矩阵的完整代码(来自开放数据集的 IRM):

from nilearn import datasets 
from nilearn import input_data 
from nilearn.connectome import ConnectivityMeasure 
import numpy as np 
from sklearn.svm import SVC, LinearSVC 
from sklearn.cross_validation import StratifiedKFold, cross_val_score 
from nilearn import connectome 
 
 
## Atlas for the parcellation and Dataset 
 
power = datasets.fetch_coords_power_2011() 
coords = np.vstack((power.rois['x'], power.rois['y'], power.rois['z'])).T 
datas = datasets.fetch_cobre(n_subjects=None, verbose=0) 
 
spheres_masker = input_data.NiftiSpheresMasker( 
                    seeds=coords, smoothing_fwhm=4, radius=5., 
                    detrend=True, standardize=True, 
                    high_pass=0.01, t_r=2, verbose=0) 
 
 
## Extracting useful IRM 
 
list_time_series = [] 
i = 0 
for fmri_filenames, confounds_file in zip(datas.func, datas.confounds): #Might take a few minutes 
    print("Sujet %s" % i) 
    if i != 38 and i != 41: #Subjects removed from the study 
        conf = np.genfromtxt(confounds_file) 
        conf = np.delete(conf, obj = 16, axis = 1) #Remove Global Signal 
        conf = np.delete(conf, obj = 0, axis = 0) #Remove labels 
        scrub = [i for i in range(150) if conf[i,7]==1]     
        conf = np.delete(conf, obj = 7, axis = 1) #Remove Scrub 
        if len(scrub) < 90: #Keep at least 60 non scrub 
            time_series = spheres_masker.fit_transform(fmri_filenames, confounds=conf) 
            time_series = np.delete(time_series, obj = scrub, axis = 0) #Remove scrub 
            list_time_series.append(time_series) 
        else: 
            list_time_series.append([]) 
    else: 
        list_time_series.append([]) 
    i+=1 
 
 
## Computing correlation matrices 
 
N = len(datas.phenotypic) 
control_subjects = [] 
patient_subjects = [] 
for i in range(N): 
    t = list_time_series[i] 
    if type(t) != list : 
        subject = datas.phenotypic[i] 
        if str(subject[4])=='b\'Control\'': 
            control_subjects.append(t) 
        else: 
            patient_subjects.append(t) 
control_subjects = np.asarray(control_subjects)             
patient_subjects = np.asarray(patient_subjects) 
 
connect_measure = ConnectivityMeasure(kind='tangent') 
control_matrices=connect_measure.fit_transform(control_subjects) 
patient_matrices=connect_measure.fit_transform(patient_subjects) 
 
matrices = np.concatenate((control_matrices, patient_matrices)) 

或者您可以下载它们 here .

感谢您的帮助!

请您参考如下方法:

不要使用["Control"]和["Patient"]作为输出标签,您应该将输出标签分配给一个数字(例如Control 为 0 和 Patient 为 1),因为 ML 算法只处理实数。 所以

Y = np.asarray( n_training*["Control"] + n_training*["Patient"] ) 

应该是

Y = np.asarray( n_training*[0] + n_training*[1] ) 

和

clf.score(np.concatenate((test_control, test_patient)), 20*['Control']+25*['Patient']) 

应该是

clf.score(np.concatenate((test_control, test_patient)), np.asarray( 20*[0] + 25*[1] ))