awacke1 commited on
Commit
6827b7d
·
verified ·
1 Parent(s): 831c45c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from sklearn.decomposition import DictionaryLearning
4
+ from sklearn.datasets import load_digits
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Title of the app
8
+ st.title('Dictionary Learning Demo with Streamlit')
9
+
10
+ # Description
11
+ st.write('''
12
+ This application demonstrates the concept of Dictionary Learning using the scikit-learn library.
13
+ Dictionary learning aims to find a sparse representation of the data in the form of a dictionary and a sparse matrix.
14
+ ''')
15
+
16
+ # Load dataset
17
+ digits = load_digits()
18
+ data = digits.data
19
+
20
+ # Display a sample image from the dataset
21
+ st.write("Sample image from the dataset:")
22
+ sample_index = 0
23
+ sample_image = data[sample_index].reshape(8, 8)
24
+ plt.imshow(sample_image, cmap='gray')
25
+ st.pyplot(plt)
26
+
27
+ # Get user input for the number of dictionary components
28
+ n_components = st.slider('Number of dictionary components', 1, 64, 32)
29
+
30
+ # Perform dictionary learning
31
+ dl = DictionaryLearning(n_components=n_components, transform_algorithm='lasso_lars', random_state=0)
32
+ X_transformed = dl.fit_transform(data)
33
+ dictionary = dl.components_
34
+
35
+ # Display the learned dictionary components
36
+ st.write("Learned dictionary components:")
37
+ fig, axes = plt.subplots(4, 8, figsize=(8, 4))
38
+ for i, ax in enumerate(axes.ravel()):
39
+ if i < n_components:
40
+ ax.imshow(dictionary[i].reshape(8, 8), cmap='gray')
41
+ ax.axis('off')
42
+ st.pyplot(fig)
43
+
44
+ # Display sparsity of the transformed data
45
+ sparsity = np.mean(X_transformed == 0)
46
+ st.write(f'Sparsity of the transformed data: {sparsity:.2f}')