【发布时间】:2021-02-01 03:54:14
【问题描述】:
import pandas as pd
import numpy as np
class CLF:
Weights = 0
def fit(DF_input, DF_output, eta=0.1, drop=1000):
X, y = DF_input.to_numpy(copy=True), DF_output.to_numpy(copy=True)
N,d = X.shape
m = len(np.unique(y))
self.Weights = np.random.normal(0,1, size=(d,m))
INPUT = pd.read_csv(path_input)
OUTPUT = pd.read_csv(path_output)
clf = CLF()
clf.fit(INPUT, OUTPUT)
我为我编写的类定义了一个方法.fit()。第一步是将两个数据帧转换为 numpy 数组。但是,当我尝试使用该方法时出现以下错误,尽管 INPUT.to_numpy(copy=True) 和 OUTPUT.to_numpy(copy=True) 都可以正常工作。有人可以帮我吗?为什么to_numpy 被识别为属性而不是数据帧方法?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-a3d455104534> in <module>
1 clf = CLF()
----> 2 clf.fit(INPUT, OUTPUT)
<ipython-input-16-57babd738b2d> in fit(DF_input, DF_output, eta, drop)
4
5 def fit(DF_input, DF_output, eta=0.1,drop=1000):
----> 6 X, y = DF_input.to_numpy(copy=True), DF_output.to_numpy(copy=True)
7 N,d = X.shape
8 m = len(np.unique(y)) # number of classes
AttributeError: 'CLF' object has no attribute 'to_numpy'
【问题讨论】:
-
你的类方法
fit的CLF不应该接收self作为它的第一个参数吗?我认为python期望是一个接收自我的类方法,因此CLF认为DF_input它是自我,而CLF没有名为to_numpy的方法/属性(因为它是一个DataFrame方法)
标签: python pandas methods attributes numpy-ndarray