Fancy-yousa's picture
Upload 78 files
b5567db verified
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