0

I have a following list and a numpy array : For the list :

features = np.array(X_train.columns).tolist() 

results :

['Attr1', 'Attr2', 'Attr3', 'Attr4', 'Attr5', 'Attr6', 'Attr7', 'Attr8', 'Attr9', 'Attr10', 'Attr11', 'Attr12', 'Attr13', 'Attr14', 'Attr15', 'Attr16', 'Attr17', 'Attr18', 'Attr19', 'Attr20', 'Attr21', 'Attr22', 'Attr23', 'Attr24', 'Attr25', 'Attr26', 'Attr27', 'Attr28', 'Attr29', 'Attr30', 'Attr31', 'Attr32', 'Attr33', 'Attr34', 'Attr35', 'Attr36', 'Attr37', 'Attr38', 'Attr39', 'Attr40', 'Attr41', 'Attr42', 'Attr43', 'Attr44', 'Attr45', 'Attr46', 'Attr47', 'Attr48', 'Attr49', 'Attr50', 'Attr51', 'Attr52', 'Attr53', 'Attr54', 'Attr55', 'Attr56', 'Attr57', 'Attr58', 'Attr59', 'Attr60', 'Attr61', 'Attr62', 'Attr63', 'Attr64']

and array name ab

aa=(lr.coef_) #I put a regression result on numpy array so I can split them, I want to put them as a list
ab=np.split(aa,len(aa))

results :

[array([[ 0.04181571,  0.62369216, -0.23559375,  0.78663624, -0.13935947,
        -0.1118698 , -0.05672835, -1.73851643, -0.42134655,  0.79001534,
         0.05048936, -0.09287526,  0.10103251, -0.0587092 , -0.05300849,
         0.72827807,  1.15870475, -0.13861187, -0.42572654,  0.19369654,
        -0.33319238, -0.06805035,  0.14067888, -0.07418516, -0.04400882,
        -0.78701564, -0.10921816, -0.26166642,  0.06800944,  0.07672145,
         0.22109349, -0.15389544,  2.41697614,  0.21749429, -0.0766771 ,
         0.77580103,  0.04128744, -0.92835969, -0.41802274,  0.89865658,
        -0.12102089, -0.28887104,  0.10421332,  0.14445757,  0.02719274,
        -1.73622976, -0.34980593,  0.35199196,  0.56110135,  0.4460968 ,
        -1.13265322,  0.26188587,  0.14336352,  0.2341355 , -0.10077637,
         0.43080231, -0.05521557, -0.1996818 ,  0.00513076, -0.14477274,
         0.04712721,  0.15380395, -2.51974007, -0.03988658]])]

Now, I want to make a dictionary for them but here I'm confused of how should I turn the array into list.

This is what I've done :

for x in features :
    for y in ab:
        print({x:y})

and the result is not as desired, since it's failed to break down the array :

{'Attr1': array([[ 0.04181571,  0.62369216, -0.23559375,  0.78663624, -0.13935947,
        -0.1118698 , -0.05672835, -1.73851643, -0.42134655,  0.79001534,
         0.05048936, -0.09287526,  0.10103251, -0.0587092 , -0.05300849,
         0.72827807,  1.15870475, -0.13861187, -0.42572654,  0.19369654,
        -0.33319238, -0.06805035,  0.14067888, -0.07418516, -0.04400882,
        -0.78701564, -0.10921816, -0.26166642,  0.06800944,  0.07672145,
         0.22109349, -0.15389544,  2.41697614,  0.21749429, -0.0766771 ,
         0.77580103,  0.04128744, -0.92835969, -0.41802274,  0.89865658,
        -0.12102089, -0.28887104,  0.10421332,  0.14445757,  0.02719274,
        -1.73622976, -0.34980593,  0.35199196,  0.56110135,  0.4460968 ,
        -1.13265322,  0.26188587,  0.14336352,  0.2341355 , -0.10077637,
         0.43080231, -0.05521557, -0.1996818 ,  0.00513076, -0.14477274,
         0.04712721,  0.15380395, -2.51974007, -0.03988658]])}
{'Attr2': array([[ 0.04181571,  0.62369216, -0.23559375,  0.78663624, -0.13935947,
        -0.1118698 , -0.05672835, -1.73851643, -0.42134655,  0.79001534,
         0.05048936, -0.09287526,  0.10103251, -0.0587092 , -0.05300849,
         0.72827807,  1.15870475, -0.13861187, -0.42572654,  0.19369654,
        -0.33319238, -0.06805035,  0.14067888, -0.07418516, -0.04400882,
        -0.78701564, -0.10921816, -0.26166642,  0.06800944,  0.07672145,
         0.22109349, -0.15389544,  2.41697614,  0.21749429, -0.0766771 ,
         0.77580103,  0.04128744, -0.92835969, -0.41802274,  0.89865658,
        -0.12102089, -0.28887104,  0.10421332,  0.14445757,  0.02719274,
        -1.73622976, -0.34980593,  0.35199196,  0.56110135,  0.4460968 ,
        -1.13265322,  0.26188587,  0.14336352,  0.2341355 , -0.10077637,
         0.43080231, -0.05521557, -0.1996818 ,  0.00513076, -0.14477274,
         0.04712721,  0.15380395, -2.51974007, -0.03988658]])}
{'Attr3': array([[ 0.04181571,  0.62369216, -0.23559375,  0.78663624, -0.13935947,
        -0.1118698 , -0.05672835, -1.73851643, -0.42134655,  0.79001534,
         0.05048936, -0.09287526,  0.10103251, -0.0587092 , -0.05300849,
         0.72827807,  1.15870475, -0.13861187, -0.42572654,  0.19369654,
        -0.33319238, -0.06805035,  0.14067888, -0.07418516, -0.04400882,
        -0.78701564, -0.10921816, -0.26166642,  0.06800944,  0.07672145,
         0.22109349, -0.15389544,  2.41697614,  0.21749429, -0.0766771 ,
         0.77580103,  0.04128744, -0.92835969, -0.41802274,  0.89865658,
        -0.12102089, -0.28887104,  0.10421332,  0.14445757,  0.02719274,
        -1.73622976, -0.34980593,  0.35199196,  0.56110135,  0.4460968 ,
        -1.13265322,  0.26188587,  0.14336352,  0.2341355 , -0.10077637,
         0.43080231, -0.05521557, -0.1996818 ,  0.00513076, -0.14477274,
         0.04712721,  0.15380395, -2.51974007, -0.03988658]])}.......

Could you help me to build a list for ab array? And how should I turn them into dictionary?

Th expected results : {[Attr1 : 0.04181571],  Attr2 : 0.623692160, and so on...}

Thank you very much!

AMC
  • 2,642
  • 7
  • 13
  • 35
dhika91
  • 55
  • 1
  • 6
  • Does this answer your question? [Convert two lists into a dictionary](https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary) – AMC Mar 23 '20 at 20:50
  • 1
    `ab` is not just a list or array as you seem to think. It's a 1-element list containing an array, which is 2D. You just need to make `ab` be the first element of the list, or don't bother splitting maybe? – Mad Physicist Mar 23 '20 at 20:57
  • 1
    Your creation of `ab` is confusing. – hpaulj Mar 23 '20 at 21:00
  • This is the result that I got :{'Attr1': array([[ 0.04181571, 0.62369216, -0.23559375, 0.78663624, -0.13935947, -0.1118698 , -0.05672835, -1.73851643, -0.42134655, 0.79001534, 0.05048936, -0.09287526, 0.10103251, -0.0587092 , -0.05300849, 0.72827807, 1.15870475, -0.13861187, -0.42572654, 0.19369654, do you know which part should be split again? because the elements of ```ab``` is concentrated in as one and can't be split – dhika91 Mar 23 '20 at 21:01

3 Answers3

1

you could use the built-in function zip :

dict(zip(features, ab[0].ravel()))

you can check the docs for numpy.ravel

Return a contiguous flattened array.

A 1-D array, containing the elements of the input, is returned.


since your ab variable is obtained with numpy.split ab is a list with one numpy array as you showed

kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Sorry, but the result is similar with the instance above, any other solution perhaps? – dhika91 Mar 23 '20 at 20:52
  • why ? it is not working or ?could you share an error ? – kederrac Mar 23 '20 at 20:53
  • This is the result that I got :{'Attr1': array([[ 0.04181571, 0.62369216, -0.23559375, 0.78663624, -0.13935947, -0.1118698 , -0.05672835, -1.73851643, -0.42134655, 0.79001534, 0.05048936, -0.09287526, 0.10103251, -0.0587092 , -0.05300849, 0.72827807, 1.15870475, -0.13861187, -0.42572654, 0.19369654, do you know which part is the problem? because the elements of ab is concentrated in as one and can't be split – dhika91 Mar 23 '20 at 21:05
  • 1
    @dhika91 check now my answer :) – kederrac Mar 23 '20 at 21:18
  • 1
    Now it's working ! Thank you very much ! I should have realized it that the ```ab``` is 1D – dhika91 Mar 23 '20 at 21:30
0

Two ways of making a dictionary from two lists:

In [277]: {k:v for k,v in zip(['a','b','c'],[1,2,3])}                                                                
Out[277]: {'a': 1, 'b': 2, 'c': 3}

In [278]: dict(zip(['a','b','c'], [1,2,3]))                                                                          
Out[278]: {'a': 1, 'b': 2, 'c': 3}
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Sorry, but the result is similar with the instance above, any other solution perhaps? – dhika91 Mar 23 '20 at 20:52
  • Your loop created a list of dictionaries, one key each, and the same array as value. We have showed you how to make one dictionary, with multiple keys. If you want one number per key, you need to flatten the `ab` list. I don't think the `split` is doing anything for you. Also check the shape of the array. Looks like it will be (1,64), where as you want a 'flat' one, shape (64,). – hpaulj Mar 23 '20 at 21:00
0

Instead of

for x in features :
    for y in ab:
        print({x:y})

use

result = {}
for x, y in zip(features, ab) :
    result[x] = y

or — even simpler:

result = dict(zip(features, ab))

The explanation:

zip(features, ab) creates the generator of pairs ('Attr1', 0.04181571), ('Attr2', 0.623692160), and so on, then we make directory from them.

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Sorry, but the result is similar with the instance above, any other solution perhaps? – dhika91 Mar 23 '20 at 20:52
  • Use `ab[0]` instead of just `ab` in my solution(s). Or — perhaps — even `ab[0][0]` – MarianD Mar 23 '20 at 20:58
  • This is the result that I got :{'Attr1': array([[ 0.04181571, 0.62369216, -0.23559375, 0.78663624, -0.13935947, -0.1118698 , -0.05672835, -1.73851643, -0.42134655, 0.79001534, 0.05048936, -0.09287526, 0.10103251, -0.0587092 , -0.05300849, 0.72827807, 1.15870475, -0.13861187, -0.42572654, 0.19369654, do you know which part is the problem? because the elements of ab is concentrated in as one and can't be split – dhika91 Mar 23 '20 at 21:06
  • Try my solution(s) with simply `aa` instead of `ab` (effectively omitting your command `ab=np.split(aa,len(aa))`). – MarianD Mar 23 '20 at 21:11
  • Did you try my solution(s) with `ab[0][0]` instead of `ab`? – MarianD Mar 23 '20 at 21:16