clear
close all
clc

%% %%%%%%%% 20 newsgroups data %%%%%%%%%%%%%

load news20

whos

[N,D] = size(A);

% single document analysis

i = 4;
%i = randsample(N,1); % document index

doc_i = A(i,:) % ith document as a sparse vector

counts_i = doc_i(doc_i>0)
 
keywords(1:10) % first 10 words in the vocabulary

words_i = keywords(doc_i>0) % words in the ith document

counts_i = counts_i';
tbl = table(words_i, counts_i)

figure
wordcloud(tbl,'words_i','counts_i');
title('Word Cloud')

%% document length analysis
word_counts = full(sum(A,2)); % total word count in each doc
max(word_counts)
mean(word_counts)
median(word_counts)

word_counts_distinct = full(sum(A>0,2)); % # distinct words in each doc
max(word_counts_distinct)
mean(word_counts_distinct)
median(word_counts_distinct)

figure; 
subplot(1,2,1)
histogram(word_counts, ...
    'Normalization', 'count')
set(gca, 'fontsize', 16)
grid on
title('20 newsgroups data', ...
    'fontsize', 20)
xlabel('Total word counts (per doc)')
ylabel('count')

subplot(1,2,2)
histogram(word_counts_distinct, ...
    'Normalization', 'count')
set(gca, 'fontsize', 16)
grid on
title('20 newsgroups data', ...
    'fontsize', 20)
xlabel('# distinct words per doc')
ylabel('count')

%% word frequency analysis

word_freq = sum(A,1); % total frequency of each word
max(word_freq)
mean(word_freq)

word_freq_doc = sum(A>0,1); % # distinct documents containing each word
max(word_freq_doc)
mean(word_freq_doc)

figure; 
subplot(1,2,1)
histogram(log10(word_freq), ...
    'Normalization', 'count')
set(gca, 'fontsize', 16)
grid on
title('20 newsgroups data', ...
    'fontsize', 20)
xlabel('word frequency')
ylabel('frequency')

subplot(1,2,2)
histogram(log10(word_freq_doc), ...
    'Normalization', 'count')
set(gca, 'fontsize', 16)
grid on
title('20 newsgroups data', ...
    'fontsize', 20)
xlabel('# distinct documents containing each word')
ylabel('frequency')

%% most frequent words

figure; 
subplot(1,2,1)
wordcloud(keywords, word_freq)

[maxk_freq, maxk_inds] = maxk(word_freq,30);

subplot(1,2,2)
bar(maxk_freq, 0.6, 'b')
set(gca, 'fontsize', 16)
xlabel('words', 'fontsize', 18)
xticks(1:30)
xticklabels(keywords(maxk_inds))
xtickangle(60)
ylabel('counts')
grid on
title('Most frequent words in 20 Newsgroups', 'fontsize', 20)

%% class sizes
cls_sz = countcats(categorical(labels));

figure
bar(cls_sz) % bar graph displaying class sizes
set(gca, 'fontsize', 16)
grid on
title('Sizes of the 20 newsgroups', ...
    'fontsize', 20)
xlabel('classes','fontsize', 18)
xticks(1:20)
xticklabels(newsgroups)
xtickangle(45)
ylabel('counts','fontsize', 18)

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%

% single natural image (grayscale)

A = imread('baboon.png');

figure
imshow(A) % full image
axis on 

figure
imshow(A(1:256,1:256)) % top quarter
axis on

A_sub = A(1:4:end, 1:4:end); % low-resolution
figure; 
imshow(A_sub)
axis on

%% single matrix as an image

% create a block diagonal matrix

A = zeros(100,100);
A(1:50,1:50) = rand(50);
A(51:100,51:100) = rand(50);

figure; 
imagesc(A) % view the matrix as an image
colorbar

%% collections of images - MNIST handwritten digits

clear
clc
close all

load mnist

numClasses = 10;
D = size(A,2);

% A is 70,000x784
% each row of A is an image of 28x28
% y has the labels (digits in image)

% one image (index=4)
example1 = reshape(A(4,:),28,[]);
figure; imagesc(example1)
colormap gray

% display first 10 images
figure; 
imagesc(reshape(A(1:10,:)',28,[])); 
colormap gray
%axis equal

% storing all images as a 3D tensor
A3 = reshape(A',28,28,[]);

figure; 
imagesc(A3(:,:,4))
colormap gray
axis equal

% class sizes

% cls_sz = zeros(1,numClasses);
% for i = 1:numClasses
%     cls_sz(i) = sum(y==i);
% end

cls_sz = countcats(categorical(y));

figure
bar(cls_sz) % bar graph displaying class sizes
set(gca, 'fontsize', 16)
grid on
title('MNIST handwritten digits', ...
    'fontsize', 20)
xlabel('classes (digits)')
xticklabels([1:9 0])
ylabel('counts')

% average digits
centers = zeros(numClasses,D);
for i = 1:numClasses
    centers(i,:) = mean(A(y==i,:),1);
end

figure; 
imagesc(reshape(centers',28,[])); 
colormap gray
axis equal
title('The average writer', 'fontsize', 18)


