filling a k,k matrix with lower triangular data - python

I want to put the output of d2arm as a lower triangular in a k by k matrix. Any advice is welcome.
def r(k):
v = np.arange(0, k, 1)
w = abs(v - (k - 1))
return(np.repeat(v, w, axis = None))
def s(k) :
l = pd.DataFrame()
for i in np.arange(1, k):
r = pd.DataFrame(np.arange(i, k, 1))
l = l.append(r)
return(np.array(l)).T
def d2arm(d, f):
k = d.shape[1]
m1 = pd.DataFrame(r(k)).T
m2 = pd.DataFrame(s(k))
H = m2.append(m1)
a = np.array([])
for i in range(H.shape[1]):
lr = round(f(d.iloc[:, H.iloc[:, i]]), 2)
a = np.append(a, lr)
return(a)

Related

How to reduce run time in my python code?

I am currently working on a project that requires me to run a complete python code base. For research purpose, I need to run the code as fast as possible. Yet I am fairly new to programming and have no idea how to reduce run time. So I hope someone can help me on that. Any advice would be appreciated. Here's part of my code base, which used a lot of nested for loops, so it might significantly increase run time.
def a_j(r, a, A): # the Claussius-Mossotti factor, determined by a symmetric (3 × 3) matrix such that (A_i)^T = A_i
alph = np.array([[0,0,0],[0,0,0],[0,0,0]],complex)
for i in range(3):
for j in range(3):
alph[i,j] = (r * a * A[i,j])
return alph
def W_ext(x, k, rho, alpha, A): # particle–particle interaction term
n = x.shape[0] # the number of x vextors
result = np.zeros([3*n,3*n],complex)
u = np.zeros((n, 3)) # u = x - x'
for i in range(n):
for j in range(n):
if i != j:
u[i] = x[i] - x[j]
block_result = a_j(rho[i], alpha, A) * G((u[i]), k) * a_j(rho[j], alpha, A)
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return result.imag
def A_ext(rho, a, A): # single-particle term
n = rho.shape[0]
result = np.zeros([3*n,3*n],complex)
for i in range(n):
for j in range(n):
if i == j:
block_result = a_j(rho[i], a, A).imag
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return result # (3 x 3) matrix
def P_ext(e, A, W, omega):
eT = np.matrix.getH(e)
mm1 = np.matmul(A, e)
mm2 = np.matmul(W, e)
extinction = (np.dot(eT, mm1) + np.dot(eT, mm2)) * (omega/2.0)
return extinction
#ABSORPTION
def W_abs(x, k, rho, alpha, A, chi): # particle–particle interaction term
n = x.shape[0]
result = np.zeros([3*n,3*n],complex)
u = np.zeros((n, 3))
for i in range(n):
for j in range(n):
if i != j:
u[i] = x[i] - x[j]
block_result = np.matrix.getH(a_j(rho[i], alpha, A)) * (1.0 / np.conjugate(chi)).imag * a_j(rho[i], alpha, A) * G((u[i]), k) * a_j(rho[j], alpha, A)
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return 2.0 * result.real # (3 x 3) matrix
def A_abs(rho, a, A, chi): # single-particle term
n = rho.shape[0]
result = np.zeros([3*n,3*n],complex)
for i in range(n):
for j in range(n):
if i == j:
block_result = np.matrix.getH(a_j(rho[i], a, A)) * (1.0 / np.conjugate(chi)).imag * a_j(rho[i], a, A)
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return result # (3 x 3) matrix

Clustering With K-Means in Python

I have the code but i am trying to find a method to plot the results
import numpy as np
import random
def cluster_points(X, mu):
clusters = {}
for x in X:
bestmukey = min([(i[0], np.linalg.norm(x - mu[i[0]])) \
for i in enumerate(mu)], key=lambda t: t[1])[0]
try:
clusters[bestmukey].append(x)
except KeyError:
clusters[bestmukey] = [x]
return clusters
def reevaluate_centers(mu, clusters):
newmu = []
keys = sorted(clusters.keys())
for k in keys:
newmu.append(np.mean(clusters[k], axis=0))
return newmu
#def has_converged(mu, oldmu):
def find_centers(x, k):
# Initialize to K random centers
oldmu = random.sample(x, k)
mu = random.sample(x, k)
while not (set([tuple(a) for a in mu]) == set([tuple(a) for a in oldmu])):
oldmu = mu
# Assign all points in X to clusters
clusters = cluster_points(x, mu)
# Reevaluate centers
mu = reevaluate_centers(oldmu, clusters)
return (mu, clusters)
def init_board(N):
X = np.array([(random.uniform(-1, 1), random.uniform(-1, 1)) for i in range(N)])
return X
def init_board_gauss(N, k):
n = float(N)/k
X = []
for i in range(k):
c = (random.uniform(-1, 1), random.uniform(-1, 1))
s = random.uniform(0.05,0.5)
x = []
while len(x) < n:
a, b = np.array([np.random.normal(c[0], s), np.random.normal(c[1], s)])
# Continue drawing points from the distribution in the range [-1,1]
if abs(a) < 1 and abs(b) < 1:
x.append([a,b])
X.extend(x)
X = np.array(X)[:N]
return X

CVXPY error: "NotImplementedError: Strict inequalities are not allowed"

def PPNM_model(a,E, beta):
p = E.shape[1]
x = E*a
x = sum(x,beta*cp.square(x))
return x
def PPNM_model_cvxpy(a,E,beta):
first = E*a
second = beta*cp.square(first)
third = sum(first,second)
return third
def construct_ppnm_model(x_in,A,E, x_LMM, a_lmm):
p = E.shape[1]
d = E.shape[0]
N = A.shape[0]
x = np.zeros(shape=(N,d), dtype=np.float64)
a_ppnm = np.zeros(shape=(N,p), dtype=np.float64)
beta_ppnm = np.zeros(shape=(N,1), dtype=np.float64)
current_lmm = cp.Variable(p)
current_beta = cp.Variable()
b_min = 0
b_max = 100
all_zeros = np.squeeze(np.zeros(shape=(N,1), dtype=np.double))
sum_to_one_vector=np.ones(shape=(1,p),dtype=np.double)
for i in np.arange(0, N):
x_in_temp = x_in[i,:].astype(np.double)
current_lmm.value=a_lmm[i,]
objective =
cp.Minimize(cp.sum_squares((PPNM_model_cvxpy(current_lmm,
E, current_beta)) - x_in_temp))
constraints = [current_lmm >= 0,
current_lmm <= 1,
current_beta >= b_min,
current_beta <= b_max,
sum_to_one_vector*current_lmm == 1]
prob = cp.Problem(objective, constraints)
result = prob.solve()
a_ppnm[i,:]=current_lmm.value
beta_ppnm[i] = current_beta.value
current_vector = PPNM_model(a_ppnm[i,:], E, current_beta.value)
x[i,:]=current_vector
return x, a_ppnm, beta_ppnm
In this problem, matrix A is of shape (10000, 4) which is (total pixels, endmembers), E is of shape (198, 4) :(spectral bands, endmembers)
and x is of shape (10000, 198) :( pixels, spectral bands)
When I call the construct_ppnm_model like this:
x_ppnm, a_ppnm, beta_ppnm = construct_ppnm_model(hsi_2d, A, E, x_LMM, a_lmm)
I get the following error message:
NotImplementedError: Strict inequalities are not allowed.

tensorflow nested while-loop in python

def body_4(i, indices_set):
c_j = lambda j, indices_set_j : tf.less(j, len_src_sent)
j = tf.constant(0)
indices_set_j = indices_set
def body_j(j, indices_set_j):
align_middle_ = ALIGNMENT_SIZE / 2
align_start_ = 0 - j
align_end_ = len_src_sent - j
c_k = lambda k, indices_set_k: tf.less(k, len_src_sent)
k = tf.constant(1)
indices_set_k = indices_set_j
def body_k(k, indices_set_k):
indices_set_k = tf.concat([indices_set_k, tf.stack([tf.cast([i*len_src_sent+j, align_middle_ + align_start_ + k], tf.int64)])], 0)
k = tf.add(k, 1)
return k, indices_set_k
[index, indices_set_k] = tf.while_loop(c_k, body_k, loop_vars=[k, indices_set_k], shape_invariants=[k.get_shape(), tf.TensorShape([None, None])])
j = tf.add(j,1)
indices_set_j = indices_set_k
return j, indices_set_j
[index, indices_set] = tf.while_loop(c_j, body_j, loop_vars=[j, indices_set_j], shape_invariants=[j.get_shape(), tf.TensorShape([None, None])])
i = tf.add(i, 1)
return i, indices_set
c = lambda i, indices_set: tf.less(i, len_trg_sent-1)
i = tf.constant(0)
indices_set = tf.cast([0, ALIGNMENT_SIZE/2], tf.int64)
indices_set = tf.stack([indices_set])
[index, indices_set] = tf.while_loop(c, body_4, loop_vars=[i, indices_set], shape_invariants=[i.get_shape(), tf.TensorShape([None, None])])
I want to create a tensorflow graph to output a few indices_set for later use of sparsetensor. The indice element should look like [i*len_trg_sent+j,align_middle_ + align_start_ + k] where i is the index of first axis, j second axis and k third of a tensor with shape(len_trg_sent-1, len_src_sent, len_src_sent)
But the code above seems nothing but a dead loop. I am confused about the nested while loop in tensorflow and therefore would appreciate it if anyone can help me.

how can I convert this code (theano) to a simple python lign

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.

Categories