1

i am trying to convert the below pandas dataframe,

pd.DataFrame({'PE': [115.45, 8], 'PE FY1': [11, 12], 'EV/Sales':[0.4, 1.9], 'EV/EBIT':[16, 9.8],
                'EV/EBITDA': [10.8, 7.5]})

to an np.array but within multiple square brackets - as below - but have so far been unsuccesful. I am unsure of the correct name of this structure but need it in this for scikit learn. I the below works for what i am trying to do, just a matter of getting there.

q = np.array([[[115.45,11.00,0.40,16.00,10.80]], [[8.00,12.00,1.90, 9.80,7.50]]])

whatever i try i either end up with normal brackets in the wrong place or IndexError: tuple index out of range as i run it through the regressor and tree interpretor - as below

Latest_feature_values = pd.DataFrame({'PE': [115.45, 8], 'PE FY1': [11, 12], 'EV/Sales':[0.4, 1.9], 'EV/EBIT':[16, 9.8],
                'EV/EBITDA': [10.8, 7.5]})

Latest_feature_values = np.array(Latest_feature_values.values)
0

1 Answer 1

2
v = df.values
v

array([[ 115.45,   11.  ,    0.4 ,   16.  ,   10.8 ],
       [   8.  ,   12.  ,    1.9 ,    9.8 ,    7.5 ]])

If by multiple brackets, you mean that you want to expand the dimensions by 1 (so as to get an output of shape (2, 1, 5)), you have a few options -

np.expand_dims(v, 1)

Or,

v[:, np.newaxis]

Or,

v[:, None]

Or (limited to 2D arrays, needs changes for N-D arrays),

i, j = v.shape
v.reshape((i, 1, j))
array([[[ 115.45,   11.  ,    0.4 ,   16.  ,   10.8 ]],

       [[   8.  ,   12.  ,    1.9 ,    9.8 ,    7.5 ]]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.