Matrix string operations pandas dataframes

I have a pandas framework that contains three columns corresponding to the x, y, and z coordinates for the positions of the objects. I also have a transformation matrix ready to rotate these points at a specific angle. I previously punched every row of a data block by performing this conversion, but I found that it was very, very long time. Now I just want to do the transforms right away and add the results as extra columns.

I am looking for a working version of this line (which always returns a form mismatch):

largest_haloes['X_rot', 'Y_rot', 'Z_rot'] = np.dot(rot,np.array([largest_haloes['X'], largest_haloes['Y'], largest_haloes['Z']]).T)

Here's a minimal working example:

from __future__ import division
import math
import pandas as pd
import numpy as np

def unit_vector(vector):
    return vector / np.linalg.norm(vector)


largest_haloes = pd.DataFrame()
largest_haloes['X'] = np.random.uniform(1,10,size=30)
largest_haloes['Y'] = np.random.uniform(1,10,size=30)
largest_haloes['Z'] = np.random.uniform(1,10,size=30)

normal = np.array([np.random.uniform(-1,1),np.random.uniform(-1,1),np.random.uniform(0,1)])
normal = unit_vector(normal)

a = normal[0]
b = normal[1]
c = normal[2]

rot = np.array([[b/math.sqrt(a**2+b**2), -1*a/math.sqrt(a**2+b**2), 0], [(a*c)/math.sqrt(a**2+b**2), b*c/math.sqrt(a**2+b**2), -1*math.sqrt(a**2+b**2)], [a, b, c]])

largest_haloes['X_rot', 'Y_rot', 'Z_rot'] = np.dot(rot,np.array([largest_haloes['X'], largest_haloes['Y'], largest_haloes['Z']]).T)

, , _haloes ['X_rot', 'Y_rot', 'Z_rot'] _haloes ['X', 'Y', 'Z' ]. ? df.dot, , , , , .

+4
1

.

numpy

lh = largest_haloes.values
rotated_array = lh.dot(rot)

x = pd.DataFrame(data=rot,index=['X','Y','Z'])
rotated_df = largest_haloes.dot(x)
+1

Source: https://habr.com/ru/post/1657675/


All Articles