preprocessing data for PCA
3 ビュー (過去 30 日間)
古いコメントを表示
Hi, I have a table contains categorial data as the first column and numerical data as the rest of columns. I need to fill the missing values and standardize the numerical data. Here is what I did:
PrepData = fillmissing(data,'spline','DataVariables',@isnumeric);
func = @zscore;
PrepData = varfun(func,PrepData{:,2:end});
but I receive this error:
Undefined function 'varfun' for input arguments of type 'function_handle'.
How can I fix this?
Thank you
0 件のコメント
回答 (1 件)
Shantanu Dixit
2025 年 5 月 29 日
編集済み: Shantanu Dixit
2025 年 5 月 29 日
Hi Mahmoud,
If I understood the query correctly, you're trying to preprocess a table where the first column is categorical and the remaining columns are numerical. The error occurs because 'varfun' expects a table as input: https://www.mathworks.com/help/matlab/ref/table.varfun.html#btyj4vl-1-A , but 'PrepData{:,2:end}' extracts a matrix (numeric array).
To fill the missing values in the numerical columns (using 'spline' interpolation) and correspondingly standardize the numerical data (using 'zscore'), you can refer to a below example script:
% Sample data
categories = {'Type1'; 'Type2'; 'Type1'; 'Type2'; 'Type1'};
values1 = [1.2; NaN; 3.4; 4.1; 5.6];
values2 = [10.5; 20.1; NaN; 40.3; 50.7];
data = table(categories, values1, values2, ...
'VariableNames', {'Category', 'Feature1', 'Feature2'});
disp(data)
% fill missing values
PrepData = fillmissing(data, 'spline', 'DataVariables', @isnumeric);
% Standardize numerical columns (keeping categorical)
numVars = PrepData.Properties.VariableNames(2:end); % Get numerical column names
% varfun expects table /timetable as second input argument
PrepData(:,numVars) = varfun(@zscore, PrepData(:,numVars));
disp('Preprocessed data:');
disp(PrepData);
Hope this helps!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!