AttributeError: function' object has no attribute 'linreg' - python

I am new to python and programming and am working my way through "Machine Learning: An Algorithmic Perspective." I was told to normalise data, seperate it into training and testing data, recover the beta vector, and then use the sum-of-least squares error. I keep getting,
File "/Users/shaune/Dropbox/Shaune PhD/auto-mpg.py", line 34, in
beta=linreg.linreg(trainin,traintgt)
AttributeError: 'function' object has no attribute 'linreg'
when running the following:
import os
import pylab as pl
import numpy as np
from pylab import *
from numpy import *
import linreg
os.chdir('/Users/shaune/Dropbox/Shaune PhD')
auto=np.loadtxt('auto-mpg.data.txt',comments='"')
#normalise the data
auto=(auto-auto.mean(axis=0))/auto.var(axis=0)
#seperate the training and testing data
trainin=auto[::2,:8]
testin=auto[1::2,:8]
traintgt=auto[::2,1:2]
testtgt=auto[1::2,1:2]
#recover the beta vector
def linreg(trainin,traintgt):
trainin=np.concatenate((trainin,-np.ones((np.shape(trainin)[0],1))),axis=1)
beta=np.dot(np.dot(np.linalg.inv(np.dot(np.transpose(trainin),trainin)),np.transpose(trainin)),traintgt)
traintgt=np.dot(trainin, beta)
#sum of squares error to get predicted values on test set (want small values)
beta=linreg.linreg(trainin,traintgt)
testin=concatenate((testin,-np.ones((np.shape(testin)[0],1))),axis=1)
testout=dot(testin,beta)
error=sum((testout-testtgt)**2)
print error
Please help! Thanks.

The definition of this function
def linreg(trainin,traintgt):
is overwriting the name linreg that you imported with
import linreg
Rename the function. The comment says recover the beta vector, so perhaps a better name is recover_beta. That is, change the def statement to
def recover_beta(trainin,traintgt):
You'll probably want to add a return statement to the function while you are at it. Currently it doesn't return anything.

Related

Is it possible to somehow take this integral?

I have the following code, but it shows error:
IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement it is advised to analyze
the integrand in order to determine the difficulties. If the position of a
local difficulty can be determined (singularity, discontinuity) one will
probably gain from splitting up the interval and calling the integrator
on the subranges. Perhaps a special-purpose integrator should be used.
potC=sc.integrate.quad(lambda r: Psi(r,n2)*(-1/r)Psi(r,n1)(r**2),0,np.inf)
How to fix it?
import scipy as sc
import numpy as np
def Psi(r,n):
return 2*np.exp(-r/n)*np.sqrt(n)*sc.special.hyp1f1(1-n, 2, 2*r/n)/n**2
def p(n1,n2):
potC=sc.integrate.quad(lambda r: Psi(r,n2)*(-1/r)*Psi(r,n1)*(r**2),0,np.inf)
pot=potC[0]
return pot
print(p(15,15))
The error literally says what your problem is. Your function is not "well behaved" in some regions.
For example with n = 15 and r = 50 your special.hyp1f1(-14, 2, 2*50/15) result in NaN. I am not familiar with this function, so I do not know if this is the expected behaviour, but this is what happens.
You can try to isolate these points and exclude them from the integration (if you know lower and upper bounds of the function's value (if it is defined) you can also update the expected error of your integration) and just integrate in the well behaved regions.
If it is a bug in scipy, then please report it to them.
Ps.: Tested with scipy 1.8.0
Ps2.: With some reading I found, that you can get the values correctly, if you do your calculations with complex number, so the following code gives you a value:
import scipy as sc
from scipy import integrate
from scipy import special
import numpy as np
def Psi(r,n):
r = np.array(r,dtype=complex)
return 2*np.exp(-r/n)*np.sqrt(n)*special.hyp1f1(1-n, 2, 2*r/n)/n**2
def p(n1,n2):
potC=integrate.quad(lambda r: Psi(r,n2)*(-1/r)*Psi(r,n1)*(r**2),0,np.inf)
pot=potC[0]
return pot
print(p(15,15))

JiTCDDE not giving expected output

I have been trying to solve the following system of delay differential equations using JiTCDDE:
Model equations
And this is my code:
from jitcdde import y, t
from jitcdde import jitcdde
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
N1=30290000
a1=0.98*N1
eps=1/5
b1=0.000024246
eta=0.3
B1=0.7
m=0.0005
chi=0.071
k1=0.185
alpha1=0.1155
delta=0.0225
phi1=0.26
omega1=0.26
d=3
model=[b1-((y(0)*B1*(y(2)+y(3)+(eta*y(5))))/a1)-b1*y(0)-m*y(0,t-d),
((y(0)*B1*(y(2)+y(3)+(eta*y(5))))/a1)-(k1+eps)*y(1)-m*y(1,t-d),
k1*eps*y(1)-(alpha1+chi)*y(2)-m*y(2,t-d),
(1-k1)*eps*y(1)-(phi1+omega1)*y(3)-m*y(3,t-d),
k1*y(1)+alpha1*y(2)-chi*y(4),
(phi1+omega1)*y(3)-(chi+delta)*y(5),
chi*(y(4)+y(5))-b1*y(6)-m*y(6,t-d),
delta*y(5)]
I=jitcdde(model)
I.constant_past([(0.98*N1-13),0,5,7,0,1,0,0], time=0.0)
I.step_on_discontinuities()
e=[]
for i in range(50):
e.append(I.integrate(i)[1])
print(e)
The problem is, for the second array of the solution (which I am trying to access), the first few values are negative values when I have specified that for t<0, the value is is 0. I have tried out this same model using ddeint and it gives a monotonically increasing curve with positive values, which is what I expect.
I want jitcdde to work though, since this model should run even when there is no delay term.
The first array seems fine and I have checked my model to see if I had made any typos but everything looks good to me. I have also tried using adjust_diff and integrate_blindly, but the issue remains.

optimize.fmin error: IndexError: too many indices for array

I am trying to optimize a function in python, using optimize.fmin from scipy. The function should optimize a vector of parameters, given initial conditions and arguments. However, I keep receiving the following error when I try to run the optimization, while running the function itself works:
IndexError: too many indices for array, line 1, in parametrization
In brief, my code is like:
import numpy as np # import numpy library
import pandas as pd # import pandas library
from scipy import optimize # import optimize from scipy library
from KF_GATSM import KF_GATSM # import script with Kalman filter
yields=pd.read_excel('data.xlsx',index_col=None,header=None) # Import observed yields
Omega0=pd.read_excel('parameters.xlsx') # Import initial parameters
# Function to optimize
def GATSM(Omega,yields,N):
# recover parameters
Omega=np.matrix(Omega)
muQ,muP=parametrization(N,Omega) # run parametrization
Y=muQ+muP # or any other function
return Y
# Parametrization of the function
def parametrization(nstate,N,Omega):
muQ=np.matrix([[Omega[0,0],0,0]]).T # intercept risk-neutral world
muP=np.matrix([[Omega[1,0],Omega[2,0],Omega[3,0]]]).T # intercept physical world
return muQ,muP
# Run optimization
def MLE(data,Omega0):
# extract number of observations and yields maturities
N=np.shape(yields)[1]
# local optimization
omega_opt=optimize.fmin(GATSM,np.array(Omega0)[:,0],args=(yields,N))
return Y
I solved the issue. It seems that I cannot select the element of an array as follows in Scipy (although it works in Numpy):
Omega[0,0]
Omega[0]
The trick is to use:
Omega.item(0)

Attribute error: "module 'numpy.random' has no attribute 'uniform' "

Here are some of the things I tried to make it work
I've tried searching for any file named random.py that I created (except for library files) and searched online for solutions like updating numpy but still can't find any decent solution. Here is my code:
from numpy import random
import random
#from random import uniform
#inputs- i.e population
equation_inputs = [4,-2,3.5,5,-11,-4.7]
#number of weights
num_weights = 6
sol_per_pop = 9
pop_size = (sol_per_pop,num_weights)
#tuple of pop_size
new_population = numpy.random.uniform(low=-4.0,high=4.0,size=pop_size)
The error message goes as follows
AttributeError: module 'numpy.random' has no attribute 'uniform'
I tried importing random and also
from numpy import random
The numpy.random.uniform should actually return 9 lists each with 6 solutions
Just use random.uniform while importing the related class with an alias (using 'as') or else just use import numpy while importing
An example for using alias is :
from numpy import random as np_random
Then utilize np_random.uniform()

running PCA analysis matplotlib results print <matplotlib.mlab.PCA instance at 0xffa4ee6c>

I am trying to do a PCA analysis of some data using the matplotlib function however when I run it and try to print the results this gets printed as the results
import numpy as np
import os
import matplotlib
from matplotlib.mlab import PCA
x=np.zeros((62,2))
a=np.genfromtxt('1.txt').T[3] #list 62numbers
#print a
x[:,0]=a
print x[:,0]
b=np.genfromtxt('2.txt').T[3] #list 4numbers
x[:,1]=b
#print x
results=PCA(x)
print results
the result that gets printed is matplotlib.mlab.PCA instance at 0xffa4ee6c why is that?
If you look at the documentation for matplotlib.mlab.PCA you see from the header
class matplotlib.mlab.PCA(a)
that it is actually a class you are dealing with. When you do PCA(x) you are creating an instance of that class, and when you print it in the following line you are told that you have got yourself an instance of matplotlib.mlab.PCA.
You can confirm this by printing the output of dir(results), where you'll see what attributes you have on the object. It can helpful to determine what object you are dealing with.
What you want to do here is to use the attributes of this object you've got. For instance,
print results.Y # print the projection in PCA space

Categories