Python Forum
From Matlab to Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: From Matlab to Python (/thread-30592.html)



From Matlab to Python - erbab - Oct-27-2020

Hi all!

I am working on a sound classification project. I have written codes in Matlab, but I need to convert it into Python to use with Raspberry Pi. However, I am not very experienced in Python, so I could not convert some parts of the code.

Here is the Matlab version of the code;

"
clc
close all

load output.mat
d=2*44100;
m=1;
for i=2:41
C1=output{i,1};
r= floor(size(C1,1)/d);
for j=1:r
C1_Data(m,:)=C1(1+((j-1)*d):(d+((j-1)*d)));
m=m+1;
end
"

Here is the Python version which is created by me. However, I could not avoid errors.

"

output = np.concatenate((a1,a2,a3,a4,a5,a6,a7,a8,a9))

d=244100
m=1

C1_Data = pd.DataFrame(reweightTarget, columns = ('Sound Class'))

for i in range(0,43):
C1 = output[i][0]
r = math.floor(C1.size/d)
for j in range (1,r):
s=C1[1+((j-1)d):(d+((j-1)*d))]
C1_Data.loc[m]=[s]
m +=1 "
The error

"index(...) must be called with a collection of some kind 'Sound Class' was passed"


If you can help me solve the problem, I will be grateful.


RE: From Matlab to Python - jefsummers - Oct-27-2020

First, please post code using the Python code marker on the editor toolbar. Blue and yellow python symbol. Otherwise indentation is lost and there may be indentation problems.

The error you get is that the columns = part of your dataframe definition has a single item, where a collection (list, for example) is necessary. If you put brackets [] around your 'Sound Class' that error will go away.

Other issues -
Minor point - range(0,43) the 0 is not needed
Single letter variable names make the code less readable and maintainable. Python style suggests variable names that describe the variable.