【发布时间】:2021-05-20 11:52:36
【问题描述】:
所以我是 Python 的新手,我正在尝试使用 scikit 从我的计算机加载数据集。这是我的代码的样子:
**whatever.py**
import numpy as np
import csv
from sklearn.datasets.base import Bunch
class Cortex_nuc:
def cortex_nuclear():
with open('C:/Users/User/Desktop/Data_Cortex_Nuclear4.csv') as csv_file:
data_file = csv.reader(csv_file)
temp = next(data_file)
n_samples = int(float(temp[0]))
n_features = int(float(temp[1]))
data = np.empty((n_samples, n_features))
target = np.empty((n_samples,), dtype=np.float64)
for i, sample in enumerate(data_file):
data[i] = np.asarray(sample[:-1], dtype=np.float64)
target[i] = np.asarray(sample[-1], dtype=np.float64)
return Bunch(data=data, target=target)
然后我将它导入到我的项目中:
from whatever import Cortex_nuc
然后我尝试将其保存到 df:
df = Cortex_nuc.cortex_nuclear()
顺便说一句,这就是数据集的样子:
这只是数据集的一部分,否则它有 77 列和大约一千行。
但我收到一条错误消息,我似乎无法弄清楚为什么会发生这种情况。这是错误消息:
IndexError Traceback (most recent call last)
<ipython-input-5-a4935f2c187f> in <module>
----> 1 df = Cortex_nuc.cortex_nuclear()
~\whatever.py in cortex_nuclear()
20
21 for i, sample in enumerate(data_file):
---> 22 data[i] = np.asarray(sample[:-1], dtype=np.float64)
23 target[i] = np.asarray(sample[-1], dtype=np.float64)
24
IndexError: index 0 is out of bounds for axis 0 with size 0
有人可以帮帮我吗?谢谢!
【问题讨论】:
-
首先您可以使用
print()来查看变量中的内容。可能(n_samples, n_features)具有值(0,0),它创建的数组没有数据位置。您应该创建普通列表并在for-loop 中使用append()。并在循环后将此列表转换为数组。更短:首先学会使用print()调试代码。或者学习如何使用真正的调试器。
标签: python scikit-learn