0

I am not too far into python yet and here is the case. i got stuck trying to import a function from a class in another file. Here is my code:

import pandas as pd
from sklearn.model_selection import train_test_split


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 converting(self):
        sliceInput, sliceTarget = self.slicing()
        convertInput = sliceInput.to_numpy(float)
        convertTarget = sliceTarget.to_numpy(int)
        return convertInput, convertTarget

    def splitting(self):
        convertInput, convertTarget = self.converting()
        x = convertInput
        y = convertTarget
        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.31, random_state=0, shuffle=False)
        return x_train, x_test, y_train, y_test


if __name__ == "__main__":
    data_frame = pd.read_csv('data_manual.csv', sep=';')
    tes = Prep(data_frame)

    print(tes.converting())

which works fine.

How do I call or print the splitting(self) method from example x_train from another file, example.py in the same directory? I have tried

from preproces import Prep

a = Prep.splitting()
print(a)

get an error like this TypeError: splitting () missing 1 required positional argument: 'self'

I do not know what happened

riokuroky
  • 45
  • 5
  • You need to create an object of type `Prep` by doing `a = Prep(some_data)` and then calling `splitting` on that since `splitting` is an instance method as `a.splitting()` – sshashank124 Jan 04 '20 at 06:02
  • what is `some_data` means? sory i dont understand – riokuroky Jan 04 '20 at 06:05
  • Well as you can see, the `Prep` class `__init__` method has a required parameter called `data`. You need to supply some data to it. Look at the `if __name__ == "__main__":` block for an explanation – sshashank124 Jan 04 '20 at 06:06

0 Answers0