Python 属性错误:';unicode';对象没有属性';形状';

Python 属性错误:';unicode';对象没有属性';形状';,python,reshape,attributeerror,Python,Reshape,Attributeerror,我有大约100个样本,有35个特征。我需要从一组特征中找到最佳特征。这些特征是特定类型癌症患者的临床和基因组细节。我需要找到能够提供高ACC的最佳组合 这是我的代码: import sys import csv import pandas as pd from mlxtend.feature_selection import SequentialFeatureSelector as SFS from sklearn.neighbors import KNeighborsClassifie

我有大约100个样本,有35个特征。我需要从一组特征中找到最佳特征。这些特征是特定类型癌症患者的临床和基因组细节。我需要找到能够提供高ACC的最佳组合

这是我的代码:

import sys

import csv

import pandas as pd

from mlxtend.feature_selection import SequentialFeatureSelector as SFS

from sklearn.neighbors import KNeighborsClassifier

from sklearn.cross_validation import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.pipeline import make_pipeline

with open("feature matrix", "r") as f:

for line in f:
        entries = line.split(',')
    for i in entries:
    if i.decode('utf-8')[0]==u'-':
        print 0-float(i.decode('utf-8')[1:])
            X = i.decode('utf-8')
y='survival'

X.shape 

y.shape

X_train, X_test, y_train, y_test= train_test_split(X, y, 
                                               stratify=y,
                                               test_size=0.3,
                                               random_state=1)


knn = KNeighborsClassifier(n_neighbors=2)

sfs1 = SFS(estimator=knn,

       k_features=(3,35),

       forward=True, 

       floating=False, 

       scoring='accuracy',

       print_progress=False,

       cv=5)

 pipe = make_pipeline(StandardScaler(), sfs1)

 pipe.fit(x_train,y_train)

 print('best combination (ACC: %.3f): %s\n' % 
 (sfs1.k_score_,sfs1.k_feature_idx_))
运行代码时,出现以下错误: 回溯(最近一次呼叫最后一次):

文件“sample_test.py”,第21行,在
X.形状
AttributeError:“unicode”对象没有属性“shape”

我还没有足够的经验来了解故障排除的下一步是什么。有没有更好的编码方法?

您之所以会出现错误,是因为您设置了unicode对象
X=I.decode('utf-8')
。我想你希望把X作为矩阵。在这种情况下,您可能需要预先分配矩阵,例如,
将numpy导入为np
np.zero(N)
,其中N是特征矩阵中的元素总数。然后执行如下操作:
对于索引,条目中的条目:
…[stuff]
X[index]=float(entry.decode('utf-8')[1:])
,或者任何正确的条目。最后,您需要重塑此数组:
X.restrap(n,m)
,其中n和m是要素矩阵的行数和列数@Jonathan Brooks谢谢您的回复
File "sample_test.py", line 21, in <module>

X.shape 

AttributeError: 'unicode' object has no attribute 'shape'