How to fill in missing data?
9 ビュー (過去 30 日間)
古いコメントを表示
Hello everybody,
I have a dataset(txt file) which contains some missing values (represented with 0). I would like to replace all this 0 places with numbers. How can I do?
0 件のコメント
回答 (2 件)
Ayush
2024 年 10 月 7 日
Hi,
One way to replace missing values currently represented as 0 is to use interpolation. Start by identifying the indices of the zeros in your dataset. Then, apply the “interp1” function to perform the interpolation. Refer to the pseudo code below for a better understanding:
% Step 1: Load the data
data = readmatrix('your_dataset.txt');
% Step 2: Interpolate to replace zeros
for col = 1:size(data, 2)
x = 1:size(data, 1); % Indices of the data
y = data(:, col); % Data values
% Find indices of non-zero and zero elements
nonZeroIndices = y ~= 0;
zeroIndices = y == 0;
% Perform interpolation only if there are non-zero elements
if any(nonZeroIndices)
% Interpolate only non-zero elements
yInterpolated = interp1(x(nonZeroIndices), y(nonZeroIndices), x(zeroIndices), 'linear', 'extrap');
% Replace zeros with interpolated values
y(zeroIndices) = yInterpolated;
end
% Update the column in the dataset
data(:, col) = y;
end
% Step 3: Save the modified data back to a file (optional)
writematrix(data, 'modified_dataset.txt');
For more information on using the “interp1” function, please refer to the documentation below:
0 件のコメント
Star Strider
2024 年 10 月 7 日
T1 = array2table(randi([0 9],10,5)) % Original
loc = table2array(T1) == 0 % Logical Matrix Of ‘0’ Locations
T1 = fillmissing(T1, 'linear', MissingLocations=loc, EndValues='nearest') % 'linear' Interpolation Of Missing Values With 'nearest' For End Values
There are several methods to fill (interpolate) the missing values. See the documentation for those and other options.
.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Preprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!