File size: 937 Bytes
b5567db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import numpy as np
import scipy.io as sio
def load_mat_as_numeric(path, x_key="X", y_key="Y"):
data = sio.loadmat(path)
X_raw = data[x_key]
y_raw = data[y_key]
# Step 1: flatten MATLAB cell array elements
def clean_cell_array(arr):
cleaned = []
for row in arr:
new_row = []
for elem in row:
# elem is usually array(['46.0'])
if isinstance(elem, np.ndarray):
elem = elem[0] # '46.0'
elem = elem.strip()
new_row.append(elem)
cleaned.append(new_row)
return np.array(cleaned)
X_str = clean_cell_array(X_raw)
y_str = clean_cell_array(y_raw).reshape(-1)
# Step 2: convert X to float
X = X_str.astype(float)
# Step 3: convert y to numeric or keep string
try:
y = y_str.astype(float)
except:
y = y_str.astype(str)
return X, y
|