I am rather new to python and I am using the pandas library to work with data frames. I created a function "elevate_power" that reads in a data frame with one column of floating point values (example x) , and a degree (example lambda), and outputs a dataframe where each column contains a power of the original column (example: output is x,x^2,x^3)
The problem is when I have a degree that is above 30, I get overflow error. Is there a way around this problem ?
I am not particularly worried about the precision, so I would not mind loosing some precision.
However, (and this is important), I need the output to be type float because I then call some numpy subroutines that give me errors if I change the type.
I have tried several tricks: for example I tried using decimal inside the function but then I cannot get the format back to floats, which is a problem because then I get errors when I call dot product and linear algebra solvers from numpy.
Any suggestion will be greatly appreciated,
This is the test code (which I ran with a low degree value so it won't crash):
def elevate_power(column, degree):
df = pd.DataFrame(column)
dfbase=df
if degree > 0:
for power in range(2, degree+1):
# first we'll give the column a name:
name = 'power_' + str(power)
df[name]= 0
df[name] = dfbase.apply(lambda x: x**power , axis=1)
return(df)
import pandas as pd
import numpy as np
test= pd.Series([1., 2., 3.])
test2=pd.DataFrame(test)
degree=5
print elevate_power(test2, degree )
np.dot(test2['power_2'],test2['power_3'])
The printout is :
0 power_2 power_3 power_4 power_5
0 1 1 1 1 1
1 2 4 8 16 32
2 3 9 27 81 243
276.0