【问题标题】:Reading an .arff file and trying to ignore the header读取 .arff 文件并尝试忽略标题
【发布时间】:2019-06-13 19:27:15
【问题描述】:

我是 python 新手,我的代码需要一些帮助。我正在阅读一个 . arff 文件与我的 jupyter notebook 使用 pyhton2.7。我想知道我需要在 arff.lodarff 中放入哪个参数,或者另一种方法,所以我可以忽略我的数据标题。

rain,meta = arff.loadarff(open('train.arff', 'r'))

在我阅读文件后,我正在做一些数学运算,我得到了这个错误。

希望有人能帮我弄清楚。

train,meta = arff.loadarff(open('train.arff', 'r'))
train = pd.DataFrame(train)
print(train)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-192-3b2868d1fd43> in <module>()
----> 1 ne = getNeighbors(X_train, y_train, X_test, k = 3)
      2 print(ne)

<ipython-input-191-75b4da86d04e> in getNeighbors(X_train, y_train, X_test, k)
      6             for (trainpoint,y_train_label) in zip(X_train,y_train):
      7                 # calculate the distance and append it to a distances_label with the associated label.
----> 8                 distances_label.append((distance(testpoint, trainpoint), y_train_label))
      9             k_neighbors_with_labels += [sorted(distances_label)[0:k]] # sort the distances and taken the first k neighbors
     10         return k_neighbors_with_labels

<ipython-input-186-22e861402349> in distance(testpoint, trainpoint)
      2 def distance(testpoint, trainpoint):
      3     # distance between testpoint and trainpoint.
----> 4     dist = np.sqrt(np.sum(np.power(float(testpoint)-float(trainpoint), 2)))
      5     return dis
      6 

ValueError: could not convert string to float: sepal_length

【问题讨论】:

    标签: python machine-learning scipy classification arff


    【解决方案1】:

    假设testpoint是距离功能中的数组。

    但是如果不是?

    您使用的是熊猫dataframes,这些不仅仅是数组,这就是你获得列名的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2016-04-18
      • 2012-04-14
      • 2023-03-25
      • 2015-12-22
      相关资源
      最近更新 更多