英文:
How can function call another functions in same class python
问题
我想创建一个包含一些函数的类,其中一个函数在类中调用另一个函数,类似这样:
```python
import pandas as pd
class Prep:
def __init__(self, data):
self.data = data
def slicing(self):
sliceInput = self.data.iloc[:, 1:8]
sliceTarget = self.data.iloc[:, 8]
return sliceInput, sliceTarget
def convert(self):
sliceInput, sliceTarget = self.slicing()
convertInput = sliceInput.to_numpy(float)
convertTarget = sliceTarget.to_numpy(int)
return convertInput, convertTarget
if __name__ == "__main__":
data_frame = pd.read_csv('data_manual.csv', sep=';')
tes = Prep(data_frame)
print(tes.convert())
我得到了以下错误:
NameError: name 'sliceInput' is not defined
如何在函数中调用 convertInput
和 convertTarget
?
为什么我会得到这个错误,我完全不理解。
<details>
<summary>英文:</summary>
I want to create a class that includes some functions, that one of them call another one in class, something like that:
import pandas as pd
class Prep:
def init(self, data):
self.data = data
def slicing(self):
sliceInput = self.data.iloc[:, 1:8]
sliceTarget = self.data.iloc[:, 8]
return sliceInput, sliceTarget
def convert(self):
convertInput = sliceInput.to_numpy(float)
convertTarget = sliceTarget.to_numpy(int)
return convertInput, convertTarget
if name == "main":
data_frame = pd.read_csv('data_manual.csv', sep=';')
tes = Prep(data_frame)
print(tes.convert())
i got error like this
`NameError: name 'sliceInput' is not defined`
how to call ` convertInput ` and `convertTarget` in function
why I get an error, I don't understand at all.
</details>
# 答案1
**得分**: 2
你需要在`convert`函数中添加`sliceInput, sliceTarget = self.slicing()`。
```python
import pandas as pd
class Prep:
def __init__(self, data):
self.data = data
def slicing(self):
sliceInput = self.data.iloc[:, 1:8]
sliceTarget = self.data.iloc[:, 8]
return sliceInput, sliceTarget
def convert(self):
sliceInput, sliceTarget = self.slicing()
convertInput = sliceInput.to_numpy(float)
convertTarget = sliceTarget.to_numpy(int)
return convertInput, convertTarget
if __name__ == "__main__":
data_frame = pd.read_csv('data_manual.csv', sep=';')
tes = Prep(data_frame)
print(tes.convert())
英文:
You need to add sliceInput, sliceTarget = self.slicing()
in covert
import pandas as pd
class Prep:
def __init__(self, data):
self.data = data
def slicing(self):
sliceInput = self.data.iloc[:, 1:8]
sliceTarget = self.data.iloc[:, 8]
return sliceInput, sliceTarget
def convert(self):
sliceInput, sliceTarget = self.slicing()
convertInput = sliceInput.to_numpy(float)
convertTarget = sliceTarget.to_numpy(int)
return convertInput, convertTarget
if __name__ == "__main__":
data_frame = pd.read_csv('data_manual.csv', sep=';')
tes = Prep(data_frame)
print(tes.convert())
答案2
得分: 2
只需将self
添加到已定义的属性中。
此外,因为在实例化Prep
对象后,data
是不可变的,所以最好在__init__()
方法中只初始化切片变量一次。
import pandas as pd
class Prep:
def __init__(self, data):
self.data = data
self.sliceInput = data.iloc[:, 1:8]
self.sliceTarget = data.iloc[:, 8]
def slicing(self):
return self.sliceInput, self.sliceTarget
def convert(self):
convertInput = self.sliceInput.to_numpy(float)
convertTarget = self.sliceTarget.to_numpy(int)
return convertInput, convertTarget
if __name__ == "__main__":
data_frame = pd.read_csv('data_manual.csv', sep=';')
tes = Prep(data_frame)
print(tes.convert())
注意:代码中的if __name__ == "__main__":
已被更正为正常的Python语法。
英文:
Just add self
to the defined attributes.
Also, because data
is immutable after you have instantiated a Prep
object, it would be better for performance if you just initialize the slice variables once in the __init__()
method.
import pandas as pd
class Prep:
def __init__(self, data):
self.data = data
self.sliceInput = data.iloc[:, 1:8]
self.sliceTarget = data.iloc[:, 8]
def slicing(self):
return self.sliceInput, self.sliceTarget
def convert(self):
convertInput = self.sliceInput.to_numpy(float)
convertTarget = self.sliceTarget.to_numpy(int)
return convertInput, convertTarget
if __name__ == "__main__":
data_frame = pd.read_csv('data_manual.csv', sep=';')
tes = Prep(data_frame)
print(tes.convert())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论