【问题标题】:Converting CSV to CLF in Python在 Python 中将 CSV 转换为 CLF
【发布时间】:2018-12-06 02:54:27
【问题描述】:

我收到一个 TypeError “TypeError:只有整数标量数组可以转换为标量索引”。

我不太清楚为什么,也找不到任何东西来解释我为什么会收到这个错误。有人可以解释一下我做错了什么并提出纠正方法吗?

import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier

data1 = np.loadtxt('0003_1.csv', delimiter=",")

indices = np.random.permutation(len(data1.data))

split = round(len(indices) * 0.8)
x_train = data1.data[indices[:split]]
y_train = data1.target[indices[:split]]
x_test = data1.data[indices[split:]]
y_test = data1.target[indices[split:]]


clf = MLPClassifier(hidden_layer_sizes=(100, 100, 100), max_iter=500, alpha=0.0001, solver='sgd', verbose=10, random_state=21, tol=0.000000001)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
accuracy_score(y_test, y_pred)

【问题讨论】:

  • 哪一行产生了错误? data1.data 应该是什么? data1.target?
  • @hp x_train = data1.data[indices[:split]] 是发生错误的地方。数据是数据,目标是机器学习的目标。 mathworks.com/matlabcentral/answers/…

标签: python csv numpy


【解决方案1】:

假设您没有逐行尝试此代码,沿途查看结果是否公平?

您没有提供csv 文件,但是以这种方式调用的loadtxt 只能生成一个二维浮点数组,所以让我们用np.ones 模拟它:

In [637]: data1 = np.ones((10,10))

这样的数组确实有data属性,也就是memoryview

In [638]: data1.data
Out[638]: <memory at 0x7fc5b6916c18>

它没有target 属性。您的 csv 可能有这样的列名称(但您没有阅读标题),但这个 loadtxt 没有以这种方式加载它们。

In [639]: data1.target
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-639-43b9ce1927aa> in <module>()
----> 1 data1.target

AttributeError: 'numpy.ndarray' object has no attribute 'target'

但是,让我们继续解决您的错误。 .data 有一个 len 就像 data1,所以 indices 有效:

In [640]: indices = np.random.permutation(len(data1.data))
In [641]: indices
Out[641]: array([0, 7, 6, 4, 8, 5, 2, 1, 9, 3])
In [642]: split = round(len(indices) * 0.8)
In [643]: split
Out[643]: 8
In [644]: indices[:split]
Out[644]: array([0, 7, 6, 4, 8, 5, 2, 1])

但是 memoryview 不能用切片索引:

In [645]: data1.data[indices[:split]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-645-b6cf2f74578c> in <module>()
----> 1 data1.data[indices[:split]]

TypeError: only integer scalar arrays can be converted to a scalar index

二维数组可以用这个切片来索引:

In [646]: data1[indices[:split]]
Out[646]: 
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

所以问题的根源在于认为data1.datadata1.target 是有用的表达式。实际上,您没有加载您期望的数据对象,或者以您期望的方式加载。而且你没有检查data1

【讨论】:

    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2019-07-07
    • 2018-12-09
    • 2015-11-30
    • 2014-11-19
    相关资源
    最近更新 更多