所以我对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列和大约1000行
但我收到了一条错误信息,我似乎不明白为什么会发生这种情况。以下是错误消息:
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
有人能帮我吗?谢谢
如果您想在
Bunch
对象中创建一个“sklearn like”数据集,您可能需要这样的内容:这是因为} 文档的“返回”部分):
sklearn.datasets
通常返回具有特定属性/键的Bunch对象(有关说明,请参阅^{相关问题 更多 >
编程相关推荐