Anisotropic
Anisotropic

Reputation: 645

Converting Django models queryset into a numpy matrix

I want to pull out a set of fields for objects in a Django queryset and convert them into a matrix with the fields as columns for some computation. I've gotten to the point of :

qs = models.Devices.objects.all()

params = qs.values('b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8')

which returns a list of dictionaries ordered by object.

I want to know if there's a good way to convert these into rows of a numpy matrix and also if there is a way to keep an index of where these values are held in the db?

I want to do some calculations (sometimes different ones depending on the parameter type) and then eventually use the resultant vectors to populate/overwrite an existing column in the db such that the ordering is the same.

Upvotes: 1

Views: 1016

Answers (1)

Rod Xavier
Rod Xavier

Reputation: 4043

You could

  1. Use the values_list() method to get a list of lists. Include the id in the values.
  2. Use list comprehension on the result of the query to remove the ids from the params and generate a list of ids.
  3. Use the params as the parameter for numpy.matrix().
  4. Perform operations on the matrix.
  5. Iterate over the ids and save the values from each row of the matrix to the corresponding object.

    import numpy as np
    qs = models.Devices.objects.all()
    params = qs.values_list('id', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8')
    ids = [arr.pop(0) for arr in params]
    matrix = np.matrix(params)
    # perform operations here
    for index, id in enumerate(ids):
        row = matrix[index].flat
        obj = qs.get(id=id)
        obj.b1 = row.next()
        obj.b2 = row.next()
        obj.b3 = row.next()
        obj.b4 = row.next()
        obj.b5 = row.next()
        obj.b6 = row.next()
        obj.b7 = row.next()
        obj.save()
    

Upvotes: 2

Related Questions