[英]matlab sum function to python converstion
I am trying to convert this matlab code to python:我正在尝试将此 matlab 代码转换为 python:
#T2 = (sum((log(X(1:m,:)))'));
Here is my code in python:这是我在python中的代码:
T2 = sum(np.log(X[0:int(m),:]).T)
where m = 103
and X
is a matrix:其中m = 103
和X
是一个矩阵:
f1 = np.float64(135)
f2 = np.float64(351)
X = np.float64(p[:, int(f1):int(f2)])
and p
is dictionary (loaded data) p
是字典(加载的数据)
The problem is python gives me the exact same value with same dimension ( 216x103
) like matlab before applying the sum function on ( np.log(X[0:int(m), :]).T
).问题是在( np.log(X[0:int(m), :]).T
上应用 sum 函数之前,python 给了我与 matlab 相同尺寸( 216x103
)完全相同的值。 However.然而。 after applying the sum function it gives me the correct value but wrong dimension ( 103x1
).应用 sum 函数后,它给了我正确的值但尺寸错误( 103x1
)。 The correct dimension is ( 1x103
).正确的尺寸是 ( 1x103
)。 I have tried using transpose after getting the sum but it doesnt work.我在得到总和后尝试使用转置,但它不起作用。 Any suggestions how to get my desired dimension?任何建议如何获得我想要的尺寸?
Lets make a demo 2d array:让我们制作一个演示二维数组:
In [19]: x = np.arange(12).reshape(3,4)
In [20]: x
Out[20]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
And apply the base Python sum
function:并应用基本的 Python sum
函数:
In [21]: sum(x)
Out[21]: array([12, 15, 18, 21])
The result is a (4,) shape array (not 4x1).结果是一个 (4,) 形状数组(不是 4x1)。 Print sum(x).shape
if you don't believe me.如果您不相信我,请打印sum(x).shape
。
The numpy.sum
function adds all terms if no axis is given:如果没有给出轴, numpy.sum
函数会添加所有项:
In [22]: np.sum(x)
Out[22]: 66
or with axis:或带轴:
In [23]: np.sum(x, axis=0)
Out[23]: array([12, 15, 18, 21])
In [24]: np.sum(x, axis=1)
Out[24]: array([ 6, 22, 38])
The Python sum
treats x
as a list of arrays, and adds them together Python sum
将x
视为数组列表,并将它们相加
In [25]: list(x)
Out[25]: [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8, 9, 10, 11])]
In [28]: x[0]+x[1]+x[2]
Out[28]: array([12, 15, 18, 21])
Transpose, without parameter, switch axes.转置,不带参数,切换轴。 It does not add any dimensions:它不添加任何维度:
In [29]: x.T # (4,3) shape
Out[29]:
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
In [30]: sum(x).T
Out[30]: array([12, 15, 18, 21]) # still (4,) shape
>> x=reshape(0:11,4,3)'
x =
0 1 2 3
4 5 6 7
8 9 10 11
>> sum(x)
ans =
12 15 18 21
>> sum(x,1)
ans =
12 15 18 21
>> sum(x,2)
ans =
6
22
38
A matrix in MATLAB consists of m
rows and n
columns, but a matrix in NumPy is an array of arrays. MATLAB 中的矩阵由m
行和n
列组成,但 NumPy 中的矩阵是数组数组。 Each subarray is a flat vector having 1 dimension equal to the number of its elements n
.每个子数组是一个平面向量,其一维等于其元素的数量n
。 MATLAB doesn't have flat vectors at all, a row is 1xn
matrix, a column is mx1
matrix, and a scalar is 1x1
matrix. MATLAB 根本没有平面向量,一行是1xn
矩阵,一列是mx1
矩阵,一个标量是1x1
矩阵。
So, back to the question, when you write T2 = sum(np.log(X[0:int(m),:]).T)
in Python, it's neither 103x1
nor 1x103
, it's a flat 103
vector.所以,回到这个问题,当你在 Python 中编写T2 = sum(np.log(X[0:int(m),:]).T)
时,它既不是103x1
也不是1x103
,它是一个平坦的103
向量。 If you specifically want a 1x103
matrix like MATLAB, just reshape(1,-1)
and you don't have to transpose since you can sum over the second axis.如果您特别想要一个像 MATLAB 这样的1x103
矩阵,只需reshape(1,-1)
并且您不必转置,因为您可以在第二个轴上求和。
import numpy as np
X = np.random.rand(216,103)
m = 103
T2 = np.sum(np.log(X[:m]), axis=1).reshape(1,-1)
T2.shape
# (1, 103)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.