Maxima: turn eigenvectors into a matrix - maxima

The second part of the output of the eigenvectors function in Maxima is a list of the eigenvectors which correspond to the eigenvalues of the first part.
E.g.:
[[[1,-1/4],[1,1]],[[[1,2/3]],[[1,-1]]]]
(1,2/3) is the eigenvector of eigenvalue 1, and (1,-1) is the eigenvector of eigenvalue (-1/4).
How can I turn these vectors into a matrix (in this case it would be equivalent to matrix([1,1],[2/3,-1])).
Thanks

(%i1) display2d: false $
(%i2) r: [[[1,-1/4],[1,1]],[[[1,2/3]],[[1,-1]]]] $
(%i3) s: second(r) $
(%i4) s: map('first, s) $
(%i5) s: apply('maplist, cons("[", s)) $
(%i6) s: apply('matrix, s);
(%o6) matrix([1,1],[2/3,-1])

Here's an attempt. Notice I've extracted the pieces via multiple assignment first, so that it's easy to remember what the pieces mean.
(%i1) foo : [[[1,-1/4],[1,1]],[[[1,2/3]],[[1,-1]]]] $
(%i2) [[vals, mults], vecs] : foo;
1 2
(%o2) [[[1, - -], [1, 1]], [[[1, -]], [[1, - 1]]]]
4 3
(%i3) vals;
1
(%o3) [1, - -]
4
(%i4) mults;
(%o4) [1, 1]
(%i5) vecs;
2
(%o5) [[[1, -]], [[1, - 1]]]
3
(%i6) apply (append, vecs);
2
(%o6) [[1, -], [1, - 1]]
3
(%i7) apply (matrix, apply (append, vecs));
[ 2 ]
[ 1 - ]
(%o7) [ 3 ]
[ ]
[ 1 - 1 ]
(%i8) transpose (%);
[ 1 1 ]
[ ]
(%o8) [ 2 ]
[ - - 1 ]
[ 3 ]
Not sure if that will work when the number of eigenvectors is different from number of eigenvalues and other special cases. But I hope this gives you something to go on.

Related

How to implement sparse mean squared error loss in Keras

I wanted to modify the following keras mean squared error loss (MSE) such that the loss is only computed sparsely.
def mean_squared_error(y_true, y_pred):
return K.mean(K.square(y_pred - y_true), axis=-1)
My output y is a 3 channel image, where the 3rd channel is non-zero at only those pixels where loss is to be computed. Any idea how can I modify the above to compute sparse loss?
This is not the exact loss you are looking for, but I hope it will give you a hint to write your function (see also here for a Github discussion):
def masked_mse(mask_value):
def f(y_true, y_pred):
mask_true = K.cast(K.not_equal(y_true, mask_value), K.floatx())
masked_squared_error = K.square(mask_true * (y_true - y_pred))
masked_mse = (K.sum(masked_squared_error, axis=-1) /
K.sum(mask_true, axis=-1))
return masked_mse
f.__name__ = 'Masked MSE (mask_value={})'.format(mask_value)
return f
The function computes the MSE loss over all the values of the predicted output, except for those elements whose corresponding value in the true output is equal to a masking value (e.g. -1).
Two notes:
when computing the mean the denominator must be the count of non-masked values and not the
dimension of the array, that's why I'm not using K.mean(masked_squared_error, axis=1) and I'm
instead averaging manually.
the masking value must be a valid number (i.e. np.nan or np.inf will not do the job), which means that you'll have to adapt your data so that it does not contain the mask_value.
In this example, the target output is always [1, 1, 1, 1], but some prediction values are progressively masked.
y_pred = K.constant([[ 1, 1, 1, 1],
[ 1, 1, 1, 3],
[ 1, 1, 1, 3],
[ 1, 1, 1, 3],
[ 1, 1, 1, 3],
[ 1, 1, 1, 3]])
y_true = K.constant([[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[-1, 1, 1, 1],
[-1,-1, 1, 1],
[-1,-1,-1, 1],
[-1,-1,-1,-1]])
true = K.eval(y_true)
pred = K.eval(y_pred)
loss = K.eval(masked_mse(-1)(y_true, y_pred))
for i in range(true.shape[0]):
print(true[i], pred[i], loss[i], sep='\t')
The expected output is:
[ 1. 1. 1. 1.] [ 1. 1. 1. 1.] 0.0
[ 1. 1. 1. 1.] [ 1. 1. 1. 3.] 1.0
[-1. 1. 1. 1.] [ 1. 1. 1. 3.] 1.33333
[-1. -1. 1. 1.] [ 1. 1. 1. 3.] 2.0
[-1. -1. -1. 1.] [ 1. 1. 1. 3.] 4.0
[-1. -1. -1. -1.] [ 1. 1. 1. 3.] nan
To prevent nan from showing up, follow the instructions here. The following assumes you want the masked value (background) to be equal to zero:
# Copied almost character-by-character (only change is default mask_value=0)
# from https://github.com/keras-team/keras/issues/7065#issuecomment-394401137
def masked_mse(mask_value=0):
"""
Made default mask_value=0; not sure this is necessary/helpful
"""
def f(y_true, y_pred):
mask_true = K.cast(K.not_equal(y_true, mask_value), K.floatx())
masked_squared_error = K.square(mask_true * (y_true - y_pred))
# in case mask_true is 0 everywhere, the error would be nan, therefore divide by at least 1
# this doesn't change anything as where sum(mask_true)==0, sum(masked_squared_error)==0 as well
masked_mse = K.sum(masked_squared_error, axis=-1) / K.maximum(K.sum(mask_true, axis=-1), 1)
return masked_mse
f.__name__ = str('Masked MSE (mask_value={})'.format(mask_value))
return f

Is there a matrix_element_inv in Maxima?

In Maxima, we have matrix_element_add, matrix_element_mult and matrix_element_transpose.
Is there a matrix_element_inv, and if not, how could I make one?
If you want to invert matrix,first remember that not all matrix can be inverted, so first be sure that your matrix can be inverted.
For maxima working with matrix the operator for multiplying is .
so with A . A = A^2
if we want to get this value is A^^2
Normally the operator apply to each element of the matrix so if you would to invert all the elements:
(%i1) A: matrix ([17, 3], [-8, 11]);
[ 17 3 ]
(%o1) [ ]
[ - 8 11 ]
(%i9) A^-1;
[ 1 1 ]
[ -- - ]
[ 17 3 ]
(%o9) [ ]
[ 1 1 ]
[ - - -- ]
[ 8 11 ]
then to get the inverse of a matrix:
(%i2) B: A^^-1;
[ 11 3 ]
[ --- - --- ]
[ 211 211 ]
(%o2) [ ]
[ 8 17 ]
[ --- --- ]
[ 211 211 ]
(%i4) B.A;
[ 1 0 ]
(%o4) [ ]
[ 0 1 ]
(%i5) A.B;
[ 1 0 ]
(%o5) [ ]
[ 0 1 ]
be sure that your matrix is invertible:
(%i6) Bad: matrix ([2, 3], [4, 6]);
[ 2 3 ]
(%o6) [ ]
[ 4 6 ]
(%i7) Bad^^-1;
expt: undefined: 0 to a negative exponent.
-- an error. To debug this try: debugmode(true);
(%i8) newdet(Bad);
(%o8)/R/ 0
Now you should read carefully this section:
http://maxima.sourceforge.net/docs/manual/maxima_23.html
specially when telling about
matrix_element_add
so really there are only this opereators so doesn't exist a matrix_element_inv
so you can write your own using lambda functions as follows for example for getting the transpose of all the inverted elements:
(%i10) matrix_element_transpose: lambda ([x], x^-1)$
(%i11) transpose(A);
[ 1 1 ]
[ -- - - ]
[ 17 8 ]
(%o11) [ ]
[ 1 1 ]
[ - -- ]
[ 3 11 ]
hope this helps

Why are all of my results from SVM the same in scikit learn?

I'm trying to calculate probabilities for a multi-class dataset using scikit learn. However, for some reason, I'm getting a the same probabilities for every example. Any idea what's happening? Does this have to do with my model, my use of the library, or something else? Appreciate any help!
svm_model = svm.SVC(probability=True, kernel='rbf',C=1, decision_function_shape='ovr', gamma=0.001,verbose=100)
svm_model.fit(train_X,train_y)
preds= svm_model.predict_proba(test_X)
train_X looks like this
array([[2350, 5550, 2750.0, ..., 23478, 1, 3],
[2500, 5500, 3095.5, ..., 23674, 0, 3],
[3300, 6900, 3600.0, ..., 6529, 0, 3],
...,
[2150, 6175, 2500.0, ..., 11209, 0, 3],
[2095, 5395, 2595.4, ..., 10070, 0, 3],
[1650, 2850, 2000.0, ..., 25463, 1, 3]], dtype=object)
train_y looks like this
0 1
1 2
10 2
100 2
1000 2
10000 2
10001 2
10002 2
10003 2
10004 2
10005 2
10006 2
10007 2
10008 1
10009 1
1001 2
10010 2
test_X looks like this
array([[2190, 3937, 2200.5, ..., 24891, 1, 5],
[2695, 7000, 2850.0, ..., 5491, 1, 4],
[2950, 12000, 4039.5, ..., 22367, 0, 4],
...,
[2850, 5200, 3000.0, ..., 15576, 1, 1],
[3200, 16000, 4100.0, ..., 1320, 0, 3],
[2100, 3750, 2400.0, ..., 6022, 0, 1]], dtype=object)
My results look like
array([[ 0.07819139, 0.22727628, 0.69453233],
[ 0.07819139, 0.22727628, 0.69453233],
[ 0.07819139, 0.22727628, 0.69453233],
...,
[ 0.07819139, 0.22727628, 0.69453233],
[ 0.07819139, 0.22727628, 0.69453233],
[ 0.07819139, 0.22727628, 0.69453233]])
Start with preprocessing!.
It's very important to standardize your data to zero-mean and unit-variance.
The scikit-learn docs say this:
Support Vector Machine algorithms are not scale invariant, so it is highly recommended to scale your data. For example, scale each attribute on the input vector X to [0,1] or [-1,+1], or standardize it to have mean 0 and variance 1. Note that the same scaling must be applied to the test vector to obtain meaningful results. See section Preprocessing data for more details on scaling and normalization
sklearns Section on Preprocessing
sklearns StandardScaler.
The next step after this is parameter-tuning (C, gamma and co.). This is usually done by GridSearch. But i usually expect people to try a simple LinearSVM first before trying the Kernel-SVM (less hyper-parameters, less computation-time, better generalization for non-optimal parameter-chosings).

Automate creation of large symbolic matrix

Can this matrix be generated in a less manual way? It's okay for 4 x 4, but I need something larger. Thanks
--> L : matrix([L11,L12,L13,L14],[L21,L22,L23,L24],[L31,L32,L33,L34],[L41,L42,L43,L44]);
(L) matrix(
[L11, L12, L13, L14],
[L21, L22, L23, L24],
[L31, L32, L33, L34],
[L41, L42, L43, L44]
)
Answer to the question and note the noun form for L in the concat function ('L)
L:genmatrix(lambda([i,j], concat('L,i,j)), 3, 3);
(L) matrix(
[L11, L12, L13],
[L21, L22, L23],
[L31, L32, L33]
)
For a diagonal matrix
R:genmatrix(lambda([i,j], if i=j then concat('R,i) else 0), 3, 3);
(R) matrix(
[R1, 0, 0],
[0, R2, 0],
[0, 0, R3]
)

Assign Taylor expansion to function

When I use Maxima to calculate the Taylor series:
f(x,y) := taylor((x+y)^3, [x, y], [2, 3], 2);
f(2,3); /* error: wrong number of arguments */
Basically I want to define a function as a expansion of (x+y)^3, which takes in x,y as parameter. How can I achieve this?
Try
(%i1) f(x,y) := ''(ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2))) $
(%i2) f(2, 3);
(%o2) 125
or
(%i1) define(f(x, y), ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2)))$
(%i2) f(2, 3);
(%o2) 125

Resources