2

I have this dataframe

     Begin    End    Duration  ID
42   40680    40846    167     18

and I want to convert a numpy array in this form :

array([40680 , 40860 ,167,18])

I am using for conversion as_matrix function and I used after it reshape(1,4) but it is not working!! It is getting me this format : [[40680 40846 167 18]] any suggestions please ? I need to convert it to that format so I can apply 'precision_recall_curve' function.

niemmi
  • 17,113
  • 7
  • 35
  • 42
Emna Jaoua
  • 361
  • 6
  • 18

1 Answers1

2

You have something like this:

pd.DataFrame({'a':[1],'b':[2],'c':[3]}, index=[42])
Out[27]: 
    a  b  c
42  1  2  3

You want to get a single row as a NumPy array:

df.loc[42].values
Out[30]: array([1, 2, 3])
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • sorry ! but it is not working! this is what I get ! [40680 40846 167 18] and I have applied y_true = eval_seg.loc[42].values! I am sure that the input format is a dataframe this is what I get when I print type of (eval_seg) – Emna Jaoua Dec 24 '16 at 09:33
  • @jaouaemna: Sorry but I have no idea what you are saying now. Maybe if you can add more detail with full executable code to your question.... – John Zwinck Dec 24 '16 at 09:41
  • well I have as input a dataframe ! and I want to convert it to numpy array as the format that I mentioned before. because I want to use that array as input in a function ''precision_recall_curve'' that calculates the precision and recall between two arrays. So when I use this array [40680 40846 167 18] the function get me as error : "ValueError: Data is not binary and pos_label is not specified" – Emna Jaoua Dec 24 '16 at 09:50
  • @jaouaemna: I see. That's a different question--you need to read the docs at http://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_curve.html and note that it requires "binary" input, not any numbers as you are using. – John Zwinck Dec 24 '16 at 09:52
  • Oh I see. I didn't pay attention to that! I will see how I can calculate the precision and recall in a different way or may be I could implement the formula directly in python. – Emna Jaoua Dec 24 '16 at 10:03