I am new to numpy and I am NOT understanding the documentation as regards diff. the code below throws the error. I am baffled any help would be appreciated.
Traceback (most recent call last):
File "/home/dave/Desktop/mcmtest/testhv calc.py", line 11, in <module>
r = np.log(close_prices).diff()
AttributeError: 'numpy.ndarray' object has no attribute 'diff'
here is the test code.
import numpy as np
from numpy import sqrt,mean,log,diff
import pandas as pd
close_prices = [178.97,175.5,171.07,171.85,172.43,172.99,167.37,164.34,162.71,\
156.41,155.15,159.54,163.03,156.49,160.5,167.78,167.43,166.97,167.96,171.51,171.11]
print (close_prices)
r = np.log(close_prices).diff()
print(r)
Given that numpy.ndarray is the Python type of "numpy arrays", the error is just saying that arrays don't have a diff method. diff is a function defined in the numpy module.
Instead of np.log(close_prices).diff(), do
np.diff(np.log(close_prices))
After having cleared my last error, I ran into another error and this error is more fundamental to the ode solver. Below is my error.
Traceback (most recent call last):
File "cont_inside_f.py", line 36, in <module>
x,t,u=solver(0,1e-2,10,[0,0],[a,eta,k,lam])
File "cont_inside_f.py", line 24, in solver
r.integrate(r.t+dt)
File "/usr/local/lib/python2.7/dist-packages/scipy/integrate/_ode.py", line 408, in integrate
self.f_params, self.jac_params)
File "/usr/local/lib/python2.7/dist-packages/scipy/integrate/_ode.py", line 1032, in run
tuple(self.call_args) + (f_params,)))
_dop.error: failed in processing argument list for call-back fcn.
I created a function that returns a list of lists. I wish to pass first element of this list to the ode solver for integration and second element of this returned list is something that I wish to plot later down my code. Kindly help me solve this error, thanks in advance!
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
from numpy import tanh,array,sin,cos
def f(t,Y,param):
x1,x2=Y[0],Y[1]
a,eta,k,lam=param[0],param[1],param[2],param[3]
e=x1-2
de=-2*x1+a*x2+sin(x1)
s=de+lam*e
u=(1/(a*cos(2*x1)))*(-eta*tanh(s)-k*s-(-2*x1+a*x2+sin(x1))*cos(x1)+2*(-2*x1+a*x2+sin(x1))+a*x2*cos(x1))
x1dot=-2*x1+a*x2+sin(x1)
x2dot=-x2*cos(x1)+cos(2*x1)*u
x=[x1dot,x2dot]
return [x,u]
def solver(t0,dt,t1,y0,param):
x,u=[[] for i in range(2)],[]
#import pdb;pdb.set_trace()
r=ode(f(t0,y0,param)[0]).set_integrator('dopri5',method='bdf')
r.set_initial_value(y0,t0).set_f_params(param)
while r.successful() and r.t<t1:
r.integrate(r.t+dt)
for i in range(2):
x[i].append(r.y[i])
t.append(r.t)
#u.append(f(r.t,[r.y[0],r.y[1]],param)[1])
u.append(f(t0,y0,param)[1])
#print(t)
return x,t,u
if __name__=='__main__':
a,eta,k,lam=2,1.2,3,2
x,t,u=solver(0,1e-2,10,[0,0],[a,eta,k,lam])
for i in range(3):
if i!=2:
plt.subplot(3,1,i+1)
plt.plot(t,x[i])
else:
plt.subplot(3,1,i+1)
plt.plot(t,u)
plt.show()
Here
r=ode(f(t0,y0,param)[0]).set_integrator('dopri5',method='bdf')
you do not pass a function pointer but the value at the point t0,y0. You could use a lambda expression to correct this,
r=ode(lambda t,y: f(t,y,param)[0]).set_integrator('dopri5',method='bdf')
As the parameter passing is done here directly, you can not do it twice via the indirect set_parameter_f, as that messes up the argument stack. Just remove that part.
You also need to initialize the list for the t values, and the current value of u is obtained via
u.append(f(r.t,r.y,param)[1])
I have been trying to use the pyRserve for time series forecast and intent to use the auto.arima function in R.
I used the following code to solve the problem of functions with a dot in their names such as auto.arima:
import pyRserve
import pandas as pd
import numpy
conn = pyRserve.connect()
df = pd.read_excel('D:/My Path/C9.xlsx', sheet_name='C9')
aList = df['Value'].tolist() # Cast the desired column into a python list
aList = numpy.array(aList)
conn.r.List = aList
auto_arima = getattr(conn.r, 'auto.arima')
conn.r.sapply(conn.ref.List, auto_arima)
but, it returned this error:
Traceback (most recent call last):
File "D:/Forecast/Python/R2Python/R2P_Practice.py", line 21, in <module>
auto_arima = getattr(conn.r, 'auto.arima')
File "C:\Python27\lib\site-packages\pyRserve\rconn.py", line 308, in __getattr__
'defined in Rserve' % realname)
NameError: no such variable or function "auto.arima" defined in Rserve
It seems the auto.arima is not defined in Rserve. Why isn't it there? How can I fix this?
I would like to use the pandas package for python. Some functionalities work, but when I try to pass "include" argument into the describe() function I get an error:
train_df.describe(include=['O'])
Full code looks like thie following:
import numpy as np
import pandas as pd
import random as rnd
import matplotlib.pyplot as plt
# aquire data
train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('test.csv')
train_df.describe(include=['O'])
I get the following error:
>> python survival.py
Traceback (most recent call last):
File "survival.py", line 10, in <module>
train_df.describe(include=['O'])
TypeError: describe() got an unexpected keyword argument 'include'
Using the .describe() on its own seems to work. Any ideas? Thank you.
I am trying to create a countplot with sns. I adapted the following code:
sns.countplot(x="deck", data=titanic, palette="Greens_d")
I use a data frame called dfvp where XP is a categorical variable which can take two string values (either defense or prosecution).
here is my adapted code:
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
Here is the error message that I get:
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
Traceback (most recent call last):
File "<ipython-input-220-20d65ae5d282>", line 1, in <module>
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
AttributeError: 'module' object has no attribute 'countplot'
FYI: I use ANACONDA and Python 3.4. on a PC with Windows 8.
Could you tell me how to fix this/what I am doing wrong?
EDIT:
Here is a MCVE
import seaborn as sns
dfvp = read_csv('C:\\Users\\VP_Prod_study_2_data changed_3.csv')
sns.countplot(x="XP", data=dfvp, palette="Greens_d")