Update app.py
Browse files
app.py
CHANGED
@@ -412,54 +412,56 @@ that can be as accurate as many blackbox models. The MIT licensed source code
|
|
412 |
can be downloaded from github.com/microsoft/interpret.
|
413 |
|
414 |
''', height=200)
|
|
|
|
|
415 |
#Get user input for the number of dictionary components
|
416 |
n_components = st.slider('Number of dictionary components', 1, 20, 10)
|
417 |
if st.button('Analyze'):
|
418 |
-
# Perform text preprocessing
|
419 |
-
vectorizer = CountVectorizer(stop_words='english')
|
420 |
-
X = vectorizer.fit_transform([text_input])
|
421 |
-
|
422 |
-
# Perform dictionary learning
|
423 |
-
dl = DictionaryLearning(n_components=n_components, transform_algorithm='lasso_lars', random_state=0)
|
424 |
-
X_transformed = dl.fit_transform(X)
|
425 |
-
dictionary = dl.components_
|
426 |
-
|
427 |
-
# Get the feature names (terms)
|
428 |
-
feature_names = vectorizer.get_feature_names_out()
|
429 |
-
|
430 |
-
# Create a DataFrame with dictionary components and their corresponding terms
|
431 |
-
df_components = pd.DataFrame(dictionary, columns=feature_names)
|
432 |
-
df_components['Component'] = ['Component ' + str(i+1) for i in range(n_components)]
|
433 |
-
df_components = df_components.set_index('Component')
|
434 |
-
|
435 |
-
# Display the DataFrame
|
436 |
-
st.markdown("### Dictionary Components")
|
437 |
-
st.dataframe(df_components)
|
438 |
-
|
439 |
-
# Create a graph of terms and their connections
|
440 |
-
G = nx.Graph()
|
441 |
-
|
442 |
-
# Add nodes to the graph
|
443 |
-
for term in feature_names:
|
444 |
-
|
445 |
-
|
446 |
-
# Add edges to the graph based on co-occurrence in dictionary components
|
447 |
-
for i in range(n_components):
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
# Plot the graph
|
455 |
-
fig, ax = plt.subplots(figsize=(8, 8))
|
456 |
-
pos = nx.spring_layout(G, k=0.3)
|
457 |
-
nx.draw_networkx_nodes(G, pos, node_size=100, node_color='lightblue', alpha=0.8)
|
458 |
-
nx.draw_networkx_edges(G, pos, edge_color='gray', alpha=0.5)
|
459 |
-
nx.draw_networkx_labels(G, pos, font_size=8)
|
460 |
-
ax.axis('off')
|
461 |
-
st.pyplot(fig)
|
462 |
-
|
463 |
|
464 |
|
465 |
|
|
|
412 |
can be downloaded from github.com/microsoft/interpret.
|
413 |
|
414 |
''', height=200)
|
415 |
+
|
416 |
+
|
417 |
#Get user input for the number of dictionary components
|
418 |
n_components = st.slider('Number of dictionary components', 1, 20, 10)
|
419 |
if st.button('Analyze'):
|
420 |
+
# Perform text preprocessing
|
421 |
+
vectorizer = CountVectorizer(stop_words='english')
|
422 |
+
X = vectorizer.fit_transform([text_input])
|
423 |
+
|
424 |
+
# Perform dictionary learning
|
425 |
+
dl = DictionaryLearning(n_components=n_components, transform_algorithm='lasso_lars', random_state=0)
|
426 |
+
X_transformed = dl.fit_transform(X)
|
427 |
+
dictionary = dl.components_
|
428 |
+
|
429 |
+
# Get the feature names (terms)
|
430 |
+
feature_names = vectorizer.get_feature_names_out()
|
431 |
+
|
432 |
+
# Create a DataFrame with dictionary components and their corresponding terms
|
433 |
+
df_components = pd.DataFrame(dictionary, columns=feature_names)
|
434 |
+
df_components['Component'] = ['Component ' + str(i+1) for i in range(n_components)]
|
435 |
+
df_components = df_components.set_index('Component')
|
436 |
+
|
437 |
+
# Display the DataFrame
|
438 |
+
st.markdown("### Dictionary Components")
|
439 |
+
st.dataframe(df_components)
|
440 |
+
|
441 |
+
# Create a graph of terms and their connections
|
442 |
+
G = nx.Graph()
|
443 |
+
|
444 |
+
# Add nodes to the graph
|
445 |
+
for term in feature_names:
|
446 |
+
G.add_node(term)
|
447 |
+
|
448 |
+
# Add edges to the graph based on co-occurrence in dictionary components
|
449 |
+
for i in range(n_components):
|
450 |
+
terms = df_components.columns[df_components.iloc[i] > 0]
|
451 |
+
for term1 in terms:
|
452 |
+
for term2 in terms:
|
453 |
+
if term1 != term2:
|
454 |
+
G.add_edge(term1, term2)
|
455 |
+
|
456 |
+
# Plot the graph
|
457 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
458 |
+
pos = nx.spring_layout(G, k=0.3)
|
459 |
+
nx.draw_networkx_nodes(G, pos, node_size=100, node_color='lightblue', alpha=0.8)
|
460 |
+
nx.draw_networkx_edges(G, pos, edge_color='gray', alpha=0.5)
|
461 |
+
nx.draw_networkx_labels(G, pos, font_size=8)
|
462 |
+
ax.axis('off')
|
463 |
+
st.pyplot(fig)
|
464 |
+
|
465 |
|
466 |
|
467 |
|