Sean
Sean

Reputation: 647

How to convert array to being 1 dimensional for use as an index

I'm trying to create an index from a numpy array, but everytime i try i get the following error 'ValueError: Cannot index with multidimensional key'. How can I get this 'indices' array into the correct format to work?

Here is the relevant code:

Dataframe:

default student balance income
0   No  No  729.526495  44361.625074
1   No  Yes 817.180407  12106.134700
2   No  No  1073.549164 31767.138947
3   No  No  529.250605  35704.493935
4   No  No  785.655883  38463.495879
... ... ... ... ...
9995    No  No  711.555020  52992.378914
9996    No  No  757.962918  19660.721768
9997    No  No  845.411989  58636.156984
9998    No  No  1569.009053 36669.112365
9999    No  Yes 200.922183  16862.952321
10000 rows × 4 columns


default.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 4 columns):
default    10000 non-null object
student    10000 non-null object
balance    10000 non-null float64
income     10000 non-null float64
dtypes: float64(2), object(2)
memory usage: 312.6+ KB
def regression (X,y,indices):
    reg = smf.logit('Default_ ~ balance + income',default,subset=indices).fit()
    beta_0 = reg.coeffs(1)
    print(reg.coeffs)

n_iter = 1

for i in range(0,n_iter):

    sample_size = len(default)
    X = default[['balance','income']]
    y = default['default']

    #create random set of indices
    indices = np.round(np.random.rand(len(default),1)*len(default)).astype(int)
    regression(X,y,indices)

Format of array im trying to use as index:


[[2573]
 [8523]
 [2403]
 ...
 [1635]
 [6665]
 [6364]]

Upvotes: 0

Views: 36

Answers (1)

Midren
Midren

Reputation: 409

Just collapse it to the one-dimensional array using flatten() https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

Upvotes: 1

Related Questions