1
私はこのクラスを持っている:Python:メソッドを正しく呼び出す方法は?
class Tumor(object):
"""
Wrapper for the tumor data points.
Attributes:
idNum = ID number for the tumor (is unique) (int)
malignant = label for this tumor (either 'M' for malignant
or 'B' for benign) (string)
featureNames = names of all features used in this Tumor
instance (list of strings)
featureVals = values of all features used in this Tumor
instance, same order as featureNames (list of floats)
"""
def __init__(self, idNum, malignant, featureNames, featureVals):
self.idNum = idNum
self.label = malignant
self.featureNames = featureNames
self.featureVals = featureVals
def distance(self, other):
dist = 0.0
for i in range(len(self.featureVals)):
dist += abs(self.featureVals[i] - other.featureVals[i])**2
return dist**0.5
def getLabel(self):
return self.label
def getFeatures(self):
return self.featureVals
def getFeatureNames(self):
return self.featureNames
def __str__(self):
return str(self.idNum) + ', ' + str(self.label) + ', ' \
+ str(self.featureVals)
と私は後で私のコードで別の関数で、それのインスタンスを使用しようとしています:
def train_model(train_set):
"""
Trains a logistic regression model with the given dataset
train_set (list): list of data points of type Tumor
Returns a model of type sklearn.linear_model.LogisticRegression
fit to the training data
"""
tumor = Tumor()
features = tumor.getFeatures()
labels = tumor.getLabel()
log_reg = sklearn.linear_model.LogisticRegression(train_set)
model = log_reg.fit(features, labels)
return model
しかし、私は私をテストするときに、このエラーを取得しておきますコード:
TypeError: __init__() takes exactly 5 arguments (1 given)
私はtrain_modelにおける腫瘍のインスタンスを作成するときに、私は5つの引数を使用していないことを理解し、私はので、どのように行うことができますか?
もちろんtumor = Tumor(idNum, malignant, featureNames, featureVals)
、あなたが実際にこれらのすべての値を必要とする:あなたはtrain_modelでインスタンスを作成する場所__init__
へ
しかし...あなたのコードのどこか他の場所に引数を持つ関数を呼び出すのはどうですか?それと同じことをしてください。 '腫瘍=腫瘍(1,2,3,4)' [編集:ああ、それは__init__メソッドをトリガするクラス名への呼び出しであることは明らかではない! OK、取得します。] – TessellatingHeckler
ID番号、ラベル、機能名、機能値がありますか? – user2357112
あなたの質問は本当に明確ではありません。 initメソッドはいくつかの値を必要とし、あなたのスクリプトはどこにあるかを知っている唯一のものです。 – lesingerouge