1

这是我的代码:

X_train = pd.read_csv('data/train_features.txt',header=0,names=['area','length','index','complexity','lines','curves','intensity'])
y_train = pd.read_csv('labels/train_labels.txt',header=0,names=['r','g','b'])

l_model = linear_model.LinearRegression()
l_model.fit(X_train,y_train)

X_test = pd.read_csv('data/test_features.txt',header=0,names=['area','length','index','complexity','lines','curves','intensity'])

with open('test.svg','rwb') as o:
    tree = ET.parse(o)
    root = tree.getroot()
    for index,child in enumerate(root):
        features = np.array(X_test.loc[index,:])
        child.set('fill',l_model.predict(features))
    tree.write('best_guess.svg')

如您所见,我正在尝试遍历 svg 文件的根元素中的每个子元素,并最终将填充元素设置为预测我的线性模型。

但是,在发生任何这种情况之前,我会遇到此错误,我相信是因为我的 for 循环没有正确获取索引 - 换句话说,我认为您不能使用 svg 的根元素来执行枚举技巧就像你可以使用 python 数组一样。这是错误:

Traceback (most recent call last):
  File "guess.py", line 26, in <module>
    features = np.array(X_test.loc[index,:])
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1225, in __getitem__
    return self._getitem_tuple(key)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 738, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 863, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1371, in _getitem_axis
    self._has_valid_type(key, axis)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1333, in _has_valid_type
    error()
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1320, in error
    (key, self.obj._get_axis_name(axis)))
KeyError: 'the label [2809] is not in the [index]' 

似乎不喜欢我调用 index.html 。所以是的,如何在获取索引的同时遍历根目录?

4

0 回答 0