I am working on creating a matrix of features from a database of signals.
I want to calculate some features in order to end up with a matrix. Each row corresponding to each signal, and 4 columns corresponding to each assessed feature.
I have searched and I can't understand how to properly insert or add a new row with the features for each signal, for every for loop while I assess the features.
This is the code I'm following on:
The.mat file is attached to this link HERE
import numpy as np
import scipy.io as sio
from scipy import stats
mat=sio.loadmat('signal_1.mat')
size=mat['signal_1']
a,b=size.shape
calc=[]
for i in range(a):
signal=mat['signal_1'][i][0]
def function(signal):
x = signal
mu=np.mean(x)
mini=np.min(x)
maxi=np.max(x)
ran=maxi-mini
values = np.column_stack((mu,mini,maxi,ran))
return values
calc.append(function(signal))
Which creates a list as follows:
That is inconvenient because I need to have an array with the shape (n,4), being n= a (number of signals).
This is is the desired result:
To sum up,
-How can I create the calc list as a float64 array with size (n,5)?
-How can I replace this line calc.append(function(signal))
to add each row to the array of the assessed features corresponding to each for loop?
-or what is the most efficient way to properly add each row?
*
*
*
*
*
PD: if I try this conversion calc=np.array(calc)
,it doesn't work and gives me a very weird float64 array with size (9,1,4)
Just create an empty array features_mat
and fill it with your features by iterating on all your signals:
import numpy as np
import scipy.io as sio
mat = sio.loadmat('signal_1.mat')
# number of signals in .mat file
n = mat['signal_1'].shape[0]
# get the signals
signals = mat['signal_1'][:,0]
def get_features(signal):
mu = np.mean(signal)
mini = np.min(signal)
maxi = np.max(signal)
ran = maxi-mini
return np.array([mu,mini,maxi,ran])
# pre-allocate memory without initializing it
features_mat = np.empty((n,4))
for i, signal in enumerate(signals):
features_mat[i,:] = get_features(signal)
>>> np.array([[ 4.07850385e+00, -2.10251071e-01, 7.06541344e+00, 7.27566451e+00],
[ 8.31759999e-02, -2.61125020e-03, 1.50838105e-01, 1.53449355e-01],
[-5.55470935e+00, -5.81185396e+00, -5.17208787e+00, 6.39766089e-01],
[-1.36478103e+01, -1.46263278e+02, 1.46379425e+02, 2.92642704e+02],
[ 3.22094459e+00, 1.00760787e+00, 5.55007608e+00, 4.54246820e+00],
[ 4.36753757e+01, 3.57114093e+01, 4.93010863e+01, 1.35896770e+01],
[ 1.71242787e+00, -2.25392323e-01, 3.59933423e+00, 3.82472655e+00],
[-1.73530851e+00, -2.00324815e+00, -1.35313746e+00, 6.50110688e-01],
[-5.83099184e+00, -6.98125270e+00, -4.75522063e+00, 2.22603207e+00]])
Output has desired shape and seems to contain the features you're looking for. Tell me if this works.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.