I’m trying to map array data to a list of column names
cols = list(X.columns) a = log_reg.coef_ pd.Series(data=a,index=cols)
but keep getting this error message
ValueError: Length of passed values is 1, index implies 32
This is a sample of the data in a
array([[-3.45917636e-04, 3.61924667e-01, -4.09270503e-01, -8.77365851e-01, 2.63110856e-01, 1.19371203e-01, -1.24971101e-01, 4.36160913e-01, -3.99315598e-01, -4.43522845e-01, -4.06608631e-01, -3.75995346e-01, -7.94023490e-02, -3.19954555e-01, -6.44072018e-01, -6.89515406e-01, -3.31630739e-01, -4.58094286e-01, 1.17109079e-01, 2.28902301e-02, 1.74657944e-01, 4.70689088e-01, 5.72027148e-01, 2.93303704e-01, -6.04354969e-03, -6.04084018e-01, 1.22102664e-01, -3.65628250e-02, 8.46421904e-02, -3.97469438e-03, 1.05515634e+00, -6.57530123e-01]])
and the list of rows in cols:
['Time', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10', 'V11', 'V12', 'V13', 'V14', 'V15', 'V16', 'V17', 'V18', 'V19', 'V20', 'V21', 'V22', 'V23', 'V24', 'V25', 'V26', 'V27', 'V28', 'Amount', 'Hours', 'Fraudulent']
The endgoal is to create a series showing the coefficients of each feature (represented by the cols list)
Advertisement
Answer
You array is having an array inside it. You have 32 column values, but your row array has a length of 1. As a result, your lengths of corresponding rows and columns don’t match. What you need instead is the inner array which has a length of 32. You can access it using the index 0.
Therefore, try the following:
pd.Series(data=a[0], index=cols)
In case your array has further nested arrays inside, and you don’t want to use multiple indices such as a[0][0]
etc., you can flatten your array using either of the two options
pd.Series(data=a.flatten(), index=cols)
or
pd.Series(data=a.ravel(), index=cols)