I have to convert the following MATLAB code:
% calculate ECDF from D at n points
function X = ECDF_representation(D, n)
m = mean(D); X = [];
for d=1:size(D,2),
[f, x] = ecdf(D(:,d)+
randn(size(D(:,d)))*0.01);
ll=interp1(f,x,linspace(0,1,n),'cubic');
X=[X ll];
end
X= [X m];
end
into Python. Here's what I've tried so far:
import numpy as np
from scipy import interpolate
from statsmodels.distributions.empirical_distribution import ECDF
def ecdf_representation(D, n):
"""calculate ECDF from D at n points"""
m = np.mean(D)
X = []
for d in xrange(D.shape[1] + 1):
f, x = ECDF([D[:, d] + np.random.randn(np.shape(D[:, d])) * 0.01])
ll = interpolate.interp1d(f, x, np.linspace(0, 1, n), 'cubic')
X = [X, ll]
X = [X, m]
return X
I get the error:
in ecdf_representation
for d in xrange(D.shape[1] + 1):
IndexError: tuple index out of range
If I switch the line for d in xrange(D.shape[1] + 1): to for d in xrange(D.shape[0] + 1):
I resolve the original error but get a new one that is:
line 25, in ecdf_representation
f, x = ECDF([D[:, d] + np.random.randn(np.shape(D[:, d])) * 0.01])
IndexError: too many indices for array
EDIT:
D is a 1-d array because of the value being returned in the following function where data is a pandas data frame.
I think I should be returning something else into the ecdf_representation function, but I'm not sure what.
def segment_energy(data, th):
mag = np.linalg.norm(data.loc[:, ['x', 'y', 'z']], axis=1)
mag = np.array(mag)
mag -= np.mean(mag)
above = np.where(mag >= th * np.std(mag))
indicator = np.zeros(mag.shape)
indicator[above] = 1
plt.plot(mag)
plt.plot(indicator * 1000, 'r')
plt.show()
return indicator
Related
whenever i run my code i get this error: only integer scalar arrays can be converted to a scalar index.
import numpy as np
from scipy.sparse import csr_matrix
x = np.array([[1,1,1],[2,2,2],[3,3,3]])
z = np.array([[1,2,3],[1,2,3],[1,2,3]])
xt = np.array([1,2,3])
o = np.array([[0,0,2],[1,0,0],[0,0,0]])
zt = 0
c =15e-1
fs = 10
dt = 1/fs
K = 50
L = len(xt)
N = len(np.reshape(x,(x.size)))
def H(x,z,o,xt):
tof = ((z - zt) + np.sqrt((x -xt)**2 + (z -zt)**2))/c
kd = np.round(tof/dt)
kd = np.reshape(kd,(kd.size,1),order='F')
kd = kd.astype(int)
i = np.ones([K*L], dtype=int)
j = np.ones([N],dtype=int)
H = csr_matrix(kd,(i,j))
return H
print(H(x,z,o,xt))
How can i solve it? thanks
why is the value of y in the input different from the output?
in the input I put 14.6 210 then the output produces 1.46210 this makes my newton interpolation calculation not correct
import numpy as np
x = [0,8,16,24]
y = [14.6210,11.8430,9.8700,8.4180]
xinput = 12
n = len(x)-1
ST = np.zeros((n+1,n+1))
ST[:,0] = y
for k in range (1,n+1):
for i in range (0,n-k+1):
ST[i,k] = (ST[i+1,k-1] - ST[i,k-1]) / (x[i+k] - x[i])
print(ST)
p = ST[0,0]
for i in range(1, n+1):
a = ST[0,i]
for k in range (0,i):
a = a * (xinput - x[k])
p = p + a
print(p)
I am trying to implement the algorithm of GMRES with right-preconditioner P for solving the linear system Ax = b . The code is running without error; however, it pops into unprecise result for me because the error I have is very large. For the GMRES method (without preconditioning matrix - remove P in the algorithm), the error I get is around 1e^{-12} and it converges with the same matrix.
import numpy as np
from scipy import sparse
import matplotlib.pyplot as plt
from scipy.linalg import norm as norm
import scipy.sparse as sp
from scipy.sparse import diags
"""The program is to split the matrix into D-diagonal; L: strictly lower matrix; U strictly upper matrix
satisfying: A = D - L - U """
def splitMat(A):
n,m = A.shape
if (n == m):
diagval = np.diag(A)
D = diags(diagval,0).toarray()
L = (-1)*np.tril(A,-1)
U = (-1)*np.triu(A,1)
else:
print("A needs to be a square matrix")
return (L,D,U)
"""Preconditioned Matrix for symmetric successive over-relaxation (SSOR): """
def P_SSOR(A,w):
## Split up matrix A:
L,D,U = splitMat(A)
Comp1 = (D - w*U)
Comp2 = (D - w*L)
Comp1inv = np.linalg.inv(Comp1)
Comp2inv = np.linalg.inv(Comp2)
P = w*(2-w)*np.matmul(Comp1inv, np.matmul(D,Comp2inv))
return P
"""GMRES_SSOR using right preconditioning P:
A - matrix of linear system Ax = b
x0 - initial guess
tol - tolerance
maxit - maximum iteration """
def myGMRES_SSOR(A,x0, b, tol, maxit):
matrixSize = A.shape[0]
e = np.zeros((maxit+1,1))
rr = 1
rstart = 2
X = x0
w = 1.9 ## in ssor
P = P_SSOR(A,w) ### preconditioned matrix
### Starting the GMRES ####
for rs in range(0,rstart+1):
### first check the residual:
if rr<tol:
break
else:
r0 = (b-A.dot(x0))
rho = norm(r0)
e[0] = rho
H = np.zeros((maxit+1,maxit))
Qcol = np.zeros((matrixSize, maxit+1))
Qcol[:,0:1] = r0/rho
for k in range(1, maxit+1):
### Arnodi procedure ##
Qcol[:,k] =np.matmul(np.matmul(A,P), Qcol[:,k-1]) ### This step applies P here:
for j in range(0,k):
H[j,k-1] = np.dot(np.transpose(Qcol[:,k]),Qcol[:,j])
Qcol[:,k] = Qcol[:,k] - (np.dot(H[j,k-1], Qcol[:,j]))
H[k,k-1] =norm(Qcol[:,k])
Qcol[:,k] = Qcol[:,k]/H[k,k-1]
### QR decomposition step ###
n = k
Q = np.zeros((n+1, n))
R = np.zeros((n, n))
R[0, 0] = norm(H[0:n+2, 0])
Q[:, 0] = H[0:n+1, 0] / R[0,0]
for j in range (0, n+1):
t = H[0:n+1, j-1]
for i in range (0, j-1):
R[i, j-1] = np.dot(Q[:, i], t)
t = t - np.dot(R[i, j-1], Q[:, i])
R[j-1, j-1] = norm(t)
Q[:, j-1] = t / R[j-1, j-1]
g = np.dot(np.transpose(Q), e[0:k+1])
Y = np.dot(np.linalg.inv(R), g)
Res= e[0:n] - np.dot(H[0:n, 0:n], Y[0:n])
rr = norm(Res)
#### second check on the residual ###
if rr < tol:
break
#### Updating the solution with the preconditioned matrix ####
X = X + np.matmul(np.matmul(P,Qcol[:, 0:k]), Y) ### This steps applies P here:
return X
######
A = np.random.rand(100,100)
x = np.random.rand(100,1)
b = np.matmul(A,x)
x0 = np.zeros((100,1))
maxit = 100
tol = 0.00001
x = myGMRES_SSOR(A,x0,b,tol,maxit)
res = b - np.matmul(A,x)
print(norm(res))
print("Solution with gmres\n", np.matmul(A,x))
print("---------------------------------------")
print("b matrix:", b)
I hope anyone could help me figure out this!!!
I'm not sure where you got you "Symmetric_successive_over-relaxation" SSOR code from, but it appears to be wrong. You also seem to be assuming that A is symmetric matrix, but in your random test case it is not.
Following SSOR's Wikipedia entry, I replaced your P_SSOR function with
def P_SSOR(A,w):
L,D,U = splitMat(A)
P = 2/(2-w) * (1/w*D+L)*np.linalg.inv(D)*(1/w*D+L).T
return P
and your test matrix with
A = np.random.rand(100,100)
A = A + A.T
and your code works up to a 12 digit residual error.
I am working on a program that can convolve two lists and I need to do it from scratch. I have the correct output from my for loop, however I can't return it because the appended output will only save the last iteration of the for loop. I've tried enumerate, append, extend, and several others, I just don't know python enough to get a list or ideally an array of ints from my for loop.
import numpy as np
#def conv395(x,h):
#Function to convolve x input signal and h impulse response
h = [1,2,-1,1]
x = [1,1,2,1,2,2,1,1]
M = len(h)-1
L = len(x)
Ly = M+L
def ConV(x,h,n):
y = (h[m]*x[(n-1)-m])
return (y)
for n in range(0,Ly+1):
mx = n-L+1
if mx < 1:
n = n+1
#print("SPACE")
Y1 = 0
if n <= M+1:
Y = []
for m in range(n):
#print(n)
y = ConV(x,h,n)
Y1 = y + Y1
Y.append(Y1)
print Y,
OUTPUT
[1] [3] [3] [5]
HOWEVER
Y type=list size=1 Value=[5]
I need the Value to match the output in a list or array.
Any help would be greatly appreciated,thanks!
something like this maybe?
h = [1,2,-1,1]
x = [1,1,2,1,2,2,1,1]
M = len(h)-1
L = len(x)
Ly = M+L
LOL = []
def ConV(x,h,n):
y = (h[m]*x[(n-1)-m])
return (y)
for n in range(0,Ly+1):
mx = n-L+1
if mx < 1:
n = n+1
#print("SPACE")
Y1 = 0
if n <= M+1:
Y = []
for m in range(n):
#print(n)
y = ConV(x,h,n)
Y1 = y + Y1
Y.append(Y1)
LOL.append(Y)
print Y,
print LOL
how can I convert this code (theano) to a simple python lign
[h_vals, _, y_vals] = theano.scan(fn=lstm_Step,
sequences=[dict(input=inputs, taps=[0])],
outputs_info=[h0, c0, None],
non_sequences=[Whx, Whh, Wcx, Wch, Wyh, bh, bc, by],
strict=True)[0]
this is an example of what I means,
import theano
import theano.tensor as tt
def add_multiply(a, b, k):
return a + b + k, a * b * k
def python_main():
x = 1
y = 2
k = 1
tuples = []
for i in range(5):
x, y = add_multiply(x, y, k)
tuples.append((x, y, k))
return tuples
def theano_main():
x = tt.constant(1, dtype='uint32')
y = tt.constant(2, dtype='uint32')
k = tt.scalar(dtype='uint32')
outputs, _ = theano.scan(add_multiply, outputs_info=[x, y], non_sequences=[k], n_steps=5)
g = theano.grad(tt.sum(outputs), k)
f = theano.function(inputs=[k], outputs=outputs + [g])
tuples = []
xvs, yvs, _ = f(1)
for xv, yv in zip(xvs, yvs):
tuples.append((xv, yv, 1))
return tuples
print 'Python:', python_main()
print 'Theano:', theano_main()
So as you say #Nurzhan I need to know what this library does especially what's the means of theano.scan.