How to convert dataframe into numpy array?

Viewed 480

So I am currently making a neural network MLP (Multi-layer-Perceptron) on group classification on sea turtle speed between beach to sea on the seconds unit, and it looks somewhat like this.

enter image description here

Now on my Jupyter Notebook I type the following

data = pd.read_csv("SeaTurtles.csv")
data

It shows the data in dataframe

enter image description here

What I wanted to do is to separate these two groups which are group A for "Training" data and group B for "Testing" data. I wanted to convert the dataframe into a NumPy array so that it would make it easier for me to classify them into the SpeciesCode. So I type in:

newdf=data.drop(['Group1','Group 2'], axis = 1)
newdf

enter image description here

I need to split it into two test group A and B so I typed in

groupA=newdf.loc[newdf['Test*'] == 'A']
groupB=newdf.loc[newdf['Test*'] == 'B']

enter image description here

Group B enter image description here

What I did was to convert these into numpy array with .to_numpy()

groupA = groupA.to_numpy()
groupA

and it returns the "AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy' " enter image description here

My question is did I do something wrong or is there another way for me to convert this dataframe into numpy array so that I can start training the data? Thank you in advance.

1 Answers

Pandas dataframe is a two-dimensional data structure to store and retrieve data in rows and columns format.

You can convert pandas dataframe to numpy array using the df.to_numpy() method.

You can use the below code snippet to convert pandas dataframe into numpy array.

numpy_array = df.to_numpy()
print(type(numpy_array))

Output:

<class 'numpy.ndarray'>
Related