1

I have a Pandas dataframe which has 4 columns. 2 columns have float64 type. The others have int type. I want to input them to LSTM layers. I need the data types be kept as they are. But when I use dataframe.values to convert them to Numpy array, the dataset data type changed to float64. How can I keep the data type as what they are?

(2 columns: float64 , 2 columns:int)

Behdad
  • 1,459
  • 3
  • 24
  • 36
  • I think you should feed all your values as `float` to your model. – Rishabh Agrahari Jun 21 '18 at 18:03
  • 1
    numpy arrays cannot have mixed types. – tnknepp Jun 21 '18 at 18:03
  • You can't have different datatypes on different columns. If you want a 2D 4xN array, you have to pick a single datatype for all of the columns. The only alternatives are to have a 1D array with a [structured datatype with four values](https://docs.scipy.org/doc/numpy/user/basics.rec.html) (which, I suspect, keras will have no idea what to do with), or to have a list of 4 separate 1D arrays. – abarnert Jun 21 '18 at 18:06
  • @tnknepp How can I input mixed types dataset to Keras? – Behdad Jun 21 '18 at 18:09
  • @RishabhAgrahari Should it always feed as `float`? All columns? – Behdad Jun 21 '18 at 18:12
  • @abarnert Is it better to use 2 different input (with different data types) to layers? – Behdad Jun 21 '18 at 18:13
  • @BehdadAhmadi I don't know, but that's a separate question that's (presumably) about the best way to use Keras, not about how to convert things between Pandas and NumPy or how to deal with multiple datatypes in NumPy, so I'd ask a new question. – abarnert Jun 21 '18 at 18:33

1 Answers1

-1

Maybe you want to check this one out Store different datatypes in one NumPy array?

There, you will find the same exact question with the one you asked. That is "How to store different datatypes in numpy array" (cmiiw).

Basically, you could either do "Record array" or "Structured array".

Edit: I don't know about keras parameter(s), or if this kind of structure will be suppprted by it. But if it's about storing 2 different datatype in one single numpy array, I guess you could use this. :)

Hope this could help.

Michael Harley
  • 831
  • 1
  • 9
  • 20
  • How can I convert pandas dataframe to `Structured array` or `Record array`? – Behdad Jun 21 '18 at 18:16
  • Try to see the link I gave you. Maybe you want to try the record one, because if you're using structured one, you can store it but you can't access the attribute. – Michael Harley Jun 21 '18 at 18:21
  • 1
    records = numpy.rec.fromarrays((a, b), names=('keys', 'data')) ||| this code will help you create "Record Array", FYI, variable "a" is a single numpy array, and variable "b" is a single numpy array. And was concatenated within a single numpy array called records. You can call numpy array A by calling it "records.keys", and call number array B by calling it "records.data". (Sorry, but as much as I want to give a better explaination, the mobile version of StackOverflow doesn't help me to create a more tidy explaination) – Michael Harley Jun 21 '18 at 18:24
  • If your answer is just "see the answer to this other equivalent question", don't write an answer, just vote to close as a dup (or flag, if you don't have enough rep for voting). – abarnert Jun 21 '18 at 18:32
  • I do want to just comment at his question tho, but my rep point was 46 at that moment. But I do want to help, what should I do then? I can't give a better/complex explaination than that, since I'm writting this on my mobile phone. I'm sorry. – Michael Harley Jun 21 '18 at 18:36