0

Trying to convert a python dict that has:

  • nteger keys

  • length 4 integer lists as values

    {38.0: [139.0, 1.8, 36.0, 18.2], etc}

into a numpy array, for analysis with sci-kit learn bayesian ridge regression.

3
  • 1
    define "convert" - what is the result supposed to look like? Commented Dec 13, 2015 at 15:48
  • What sort of array? Do you want an array with n rows and 5 columns, n rows and 4 columns, etc.? should the array just be rows of the lists in the dictionary or something else? Commented Dec 13, 2015 at 15:48
  • Normal input to array is a list, or list of lists. Commented Dec 13, 2015 at 16:49

1 Answer 1

1

Assuming you mean what I think you mean this is a simple way to do it (it's a bit shorter to use append but for large arrays always preallocate using numpy).

    myDict = {38.0: [139.0, 1.8, 36.0, 18.2], 39.0: [139.0, 1.8, 36.0, 18.2]}
    y = np.zeros(len(myDict))
    X = np.zeros((len(myDict), 4))
    i = 0
    for key, values in myDict.iteritems():
        y[i] = key
        X[i, :] = values
        i += 1
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.