I have to read multiple data from csv files, and when I want to invert matrix from csv data, I get this:
numpy.linalg.linalg.LinAlgError: singular matrix
and the process get stuck on this section :
J = np.mat([dtdx,dtdy,dtdz]).transpose()
dd = np.mat(ttcal-tt)
dm = (scipy.linalg.inv(J.transpose()*J))*((J.transpose())*(dd.transpose()))
and data from "J" like this :
[[-6.81477651e-03 -7.90320450e-03 6.50533437e-05]
[-6.71080644e-03 -6.00135428e-03 6.50533437e-05]]
and data from "dd" like this :
[[0.00621772 0.00537531]]
i has check this data and i find this :
tes = J.transpose()*J
and the result like this :
[[ 9.14761019e-05 9.41324993e-05 -8.79884397e-07]
[ 9.41324993e-05 9.84768945e-05 -9.04538042e-07]
[-8.79884397e-07 -9.04538042e-07 8.46387506e-09]]
I need to invert this matrix but this data is singular matrix. I have to try on matlab r2017b and running well.
I need to solve this problem on python.
Have you tried using pseudo-inverse numpy.linalg.pinv instead? It's supposed to deal with these situations.
B = np.linalg.pinv(a)
But I would suggest to check that you really calculated correctly your matrix and a singular matrix is supposed to appear.
If you are sure the calculations you made are correct and it is what you wanted, then you can go for a psuedo inverse for the singular matrix that you are having.
It can be done in python like this..
mat = np.array([[ 9.14761019e-05, 9.41324993e-05, -8.79884397e-07],
[ 9.41324993e-05, 9.84768945e-05, -9.04538042e-07],
[-8.79884397e-07, -9.04538042e-07, 8.46387506e-09]])
p_inv = np.linalg.pinv(mat)
print(p_inv)
# output
array([[-1.00783988e+13, 5.50963966e+11, -9.88844703e+14],
[ 5.50963966e+11, -3.01194390e+10, 5.40580308e+13],
[-9.88844703e+14, 5.40580308e+13, -9.70207468e+16]])
Related
I have a list of n matrices where n = 5:
[matrix([[3.62425112, 0.00953506],
[0.00953506, 1.05054417]]), matrix([[4.15808905e+00, 9.27845937e-04],
[9.27845937e-04, 9.88509628e-01]]), matrix([[3.90560856, 0.0504297 ],
[0.0504297 , 0.92587046]]), matrix([[ 3.87347073, -0.12430547],
[-0.12430547, 1.09071475]]), matrix([[ 3.87697392, -0.00475038],
[-0.00475038, 1.01439917]])]
I want to do element-wise addition of these matrices:
I am trying this:
np.add(S_list[0], S_list[1], S_list[2], S_list[3], S_list[4])
It works but I don't want to fix n = 5
Can anyone please help? Thank you.
by the documentation, np.add should add only two matrices.
However np.add.reduce(S_list) or just sum(S_list) will give you what you want.
You could just use Python's built-in function sum
sum(S_list)
Output:
[[19.43839338 -0.06816324]
[-0.06816324 5.07003818]]
Are you sure that np.add(S_list[0], S_list[1], S_list[2], S_list[3], S_list[4]) works ? Because np.add() takes as input arguments two arrays . Anyway , the following code does the work if you want to use np.add():
sum = np.add(S_list[0],S_list[1])
for i in range(len(S_list) - 2):
sum = np.add(sum,S_list[i+2])
print(sum)
I hope I can be as clear as possible.
I have an excel file with 400 subjects for a study and for each one of them I have their age, their sex and 40 more columns of biological variables.
Es: CODE0001; (age)20; M\F; Biovalue1; BioValue 2 ..... Biovalue 40.
My goal is to analyze these data with the 1-way Anova because I think it's the best option I have. I'm trying do it (even using this guide https://www.marsja.se/four-ways-to-conduct-one-way-anovas-using-python/ ) but there's always a problem with the code.
So: how can I set up my data in order to be able to use the code for example from that website?
I've already done Dataset.mean() and Dataset.std() for all the data, but I can't use for example the value "Mean Age" because it seems like Jupyter only reads it as a string and not a value.
I'm in a deep state of confusion, so all kind of help will be super appreciated!!!
Thank you in advance
I'm sorry but I didn't understand. I'm relatively new to python so maybe i couldn't explain myself properly.
I need to do an Anova analysis:
First I did this:
AnalisiISAD.mean()
2) Then I made a list from that:
MeanList = [......]
3) Then i proceded with the anova script
AnalisiI.boxplot('MeanList', by='AgeT0', figsize=(12,8))
ctrl = Analisi['MeanList'][Analisi == 'ctrl']
grps = pd.unique(Analisi.group.values)
d_data = {grp:Analisi['MeanList'][Analisi.group ==grp] for grp in grps}
k = len(pd.unique(Analisi.group))
N = len(Analisi.values)
n = Analisi.groupby('AgeT0').size()[0]
but this error occurs: KeyError: 'Column not found: MeanList'
Does this mean I have to create a new column in the excel file? How do I do that?
When using df.mean() or df.std(), try changing the data to pd.Series first and run it.
I would like to invert a bunch of tensors in a list using cholesky decomposition in tensorflow 2, but the resulting code is quite ugly. is there any elegant / more pythonic way to do something like this :
iMps = []
for Mp in Mps :
cholMp = tf.linalg.cholesky(Mp)
icholMp = tf.linalg.inv(cholMp)
iMp = tf.tensordot(tf.transpose(icholMp),icholMp)
iMps.append(iMp)
is it possible to replace for loop with other stuff ?, Mps is list of tensors with different size (can i represent it as something else?). is there any way to make it more elegant ?
You can achieve this using python Map function.
I have modified your code to create Map function like below.
def inverse_tensors(Mp):
cholMp = tf.linalg.cholesky(Mp)
icholMp = tf.linalg.inv(cholMp)
iMp = tf.tensordot(tf.transpose(icholMp),icholMp,axes=0)
return iMp
iMps = list(map(inverse_tensors,list_tensors))
Hope this answers your question, Happy Learning!
I need to convert a piece of MATLAB code to Python and I'm bad at both. The code in MATLAB uses fft and fftshift. I tried to use NumPy in Python. The code runs but when I compare the outcome they are not matching. I appreciate your help.
Here is the MATLAB code:
h(1,1:Modes_number) = -1i*S;
hfft = fft(h);
hft0 = fftshift(hfft);
and here is the Python code which I wrote:
h = np.zeros((1,self.cfg.Modes_number+1),dtype=complex)
for i in range(0, self.cfg.Modes_number+1):
h[0,i] = -1j*S;
hfft = np.fft.fft(h)
hft0 = np.fft.fftshift(hfft)
Here is the values for S and Modes_number:
S = 12.5022214424;
Modes_number = 200;
Here is also an example of the results I get in MATLAB and Python:
MATLAB:
hfft(1,1)
ans =
1.1857e-13 - 2.5129e+03i
Python:
hfft[0]
0. -2.52544873e+03j
Cheers.
The error in your Python code is that you define h to be of size Modes_number+1, which is one more than the size in the MATLAB code. The first value in hfft is the sum of all input values. In MATLAB this is -1j*S*200 = -2500.4j, and in your Python code this is -1j*S*201 = -2512.9j. These are the values that you are seeing.
This bit of Python code produces the same as your bit of MATLAB code, up to numerical precision (I see some values like -1.68388521e-15 +6.55829989e-15j in Python, which are forced to 0 by MATLAB's algorithms). I am creating h as a one-dimensional vector, rather than a 2D array with one dimension of size 1.
import numpy as np
S = 12.5022214424
Modes_number = 200
h = np.zeros(Modes_number,dtype=complex)
for i in range(0,Modes_number):
h[i] = -1j*S;
hfft = np.fft.fft(h)
hft0 = np.fft.fftshift(hfft)
Python:
>>> hfft[0]
-2500.4442884800001j
MATLAB:
>> hfft(1)
ans =
0.000000000000000e+00 - 2.500444288480000e+03i`
My source data is in a TSV file, it is a 20X20 matrix (ehttp://www.bio.davidson.edu/genomics/2008/Simpson/BLOSUM62.png)
Here's what I'm trying to accomplish:
I need to read the matrix in this source file and then I have to make a function (called distance) that calculate the intersection between two index. For example if I have [A][D], the function returns the value of the intersection between these two letters. This is the code I tried:
Blosum62 = open('BLOSUM62.tsv','r')
print(Blosum62)
def distance(x,y):
a = Blosum62.index(x)
b = Blosum62.index(y)
dist = Blosum62Matrix[a][b]
return dist
but it doesn't work, I think there is a problem either in the way I used to open/print my file and in the function...thanks for helping!