Understanding Data Structure with NMF
Hello. I am Ichikawa, an engineer at Dentsu Digital.
In my daily work, I am involved in data analysis and machine learning projects, and in that context, I have had the opportunity to work with "NMF (Non-negative Matrix Factorization)." NMF is a well-known algorithm that has long been used for recommendations and topic extraction, but I suspect many people have heard the name without fully understanding how it works. Therefore, I would like to summarize NMF, from its basic concepts to actual use cases, as a way to organize my own thoughts as well. This article is written based on information as of February 2026.
(This article contains promotional content for Dentsu Digital Inc.)
Table of Contents
1. What is NMF
2. Advantages of using NMF
3. Trying out data extraction with NMF
4. Summary
1. What is NMF
NMF (Non-negative Matrix Factorization) is a method that approximates an original non-negative matrix V as the product of two non-negative matrices, W (feature matrix) and H (weight matrix). Assuming we have an m x n non-negative matrix V, NMF decomposes it into the product of non-negative matrices W (m x r) and H (r x n).

In this way, the original data can be decomposed into meaningful components and expressed as a sum.
2. Advantages of using NMF
Compared to other feature decomposition methods, NMF, which is expressed only with non-negative values, is said to have high interpretability for extracted topics. For example, suppose you want to analyze what kind of opinions are included in reviews for a certain product. Reviews contain multiple topics, such as those mentioning convenience or those regarding the product's color or design. Let's apply the results of this review data to the formula mentioned earlier. For example, if we define the review data as a "document x word" matrix V, NMF decomposes it as follows:

Here, V is a matrix representing the frequency of word occurrences in each review, W is a "document x topic" matrix representing how much of each topic each review contains, and H is a "topic x word" matrix representing which words are strongly associated with each topic. In other words, by looking at each row of H, you can grasp the meaning of topics such as "word groups related to convenience" or "word groups related to design," and by looking at each row of W, you can understand which combination of topics each review is composed of.
In NMF, each review is expressed as an "additive combination" of these multiple topics. In other words, a certain review is expressed in a form like "0.6 for the convenience topic and 0.4 for the design topic." Since it does not include negative contributions and can explain documents through the weighting of topics, it is considered to have high interpretability, making it easy to intuitively understand the meaning of each topic and its relationship to the documents.
Similar to NMF, Singular Value Decomposition (SVD) is a dimensionality reduction method that performs low-rank approximation of a document-word matrix. SVD is a representative method widely used in text analysis, such as in Latent Semantic Analysis (LSA), and it compresses information by projecting the matrix into a low-dimensional space based on orthogonal bases. This method has the theoretically clear property of minimizing reconstruction error and allows for efficient dimensionality reduction. Since SVD includes positive and negative values in the decomposed matrices, "words that contribute positively" and "words that contribute negatively" to a certain topic can appear simultaneously. Therefore, when interpreting it, one must understand it as an "increase or decrease in a certain direction" rather than "adding up certain concepts." In contrast, while NMF is also a dimensionality reduction method that performs low-rank approximation, it is characterized by performing decomposition under non-negative constraints. As a result, each topic is expressed as a non-negative weighted set of words, and each document is also expressed as an additive combination of topics. Therefore, while SVD may have an advantage in terms of reconstruction accuracy, NMF has the advantage of making it easier to grasp the content of topics and their correspondence with documents in terms of interpretability and application to use cases.
3. Trying out data extraction with NMF
I have briefly explained NMF so far. Next, I will apply NMF to actual data. This time, I performed a simple analysis using NMF on 121 articles previously posted on the "Dentsu Digital Tech Blog," excluding the titles, to see what topics have been discussed in the past. First, I performed data preprocessing on the 121 pieces of text data. For simplification, I only analyzed Japanese nouns this time.
# -------------------------
# 1. データ読み込み
# -------------------------
with open(”dd_techblog_title_body.json”, ”r”, encoding="utf-8") as f:
data = json.load(f)
df = pd.DataFrame(data)
df = df[df["body"].notna()].copy()
# -------------------------
# 2. 前処理: HTML/コード/英数字などを削除し、日本語のみ抽出
# -------------------------
df = pd.DataFrame(data)
df = df[df["body"].notna()].copy()
# -------------------------
# 2. 前処理: HTML/コード/英数字などを削除し、日本語のみ抽出
# -------------------------
RE_TAG = re.compile(r"<[^>]+>")
RE_CODE_FENCE = re.compile(r"```.*?```", re.DOTALL)
RE_INLINE_CODE = re.compile(r"`[^`]+`")
RE_URL = re.compile(r"https?://\S+")
RE_ENTITY = re.compile(r"&[a-zA-Z]+;")
RE_NON_JA = re.compile(r"[^ぁ-んァ-ン一-龥ー々\s]") # 日本語以外を空白へ(記号・英数など)
def clean_text(text: str) -> str:
text = str(text)
text = RE_CODE_FENCE.sub(" ", text) # ``` ``` のコードブロック削除
text = RE_INLINE_CODE.sub(" ", text) # `inline code` 削除
text = RE_TAG.sub(" ", text) # HTMLタグ削除
text = RE_URL.sub(" ", text) # URL削除
text = RE_ENTITY.sub(" ", text) # 等
text = RE_NON_JA.sub(" ", text) # 日本語以外を除去
text = re.sub(r"\s+", " ", text).strip()
return text
df["body_clean"] = df["body"].apply(clean_text)
# -------------------------
# 3. 形態素解析(名詞中心 + ストップワード)
# -------------------------
tokenizer = Tokenizer()
custom_stopwords = set([ "する","なる","ある","いる","みる","使う","できる", "思う",
"考える","まとめ","方法","今回","これ","それ", "ため","よう","もの","こと",
"ところ","とき","場合", "さん","など","そして","また","一方", ])
def tokenize_ja_nouns(text: str) -> str:
tokens = []
for t in tokenizer.tokenize(text):
pos = t.part_of_speech.split(",")[0]
base = t.base_form # 名詞だけ
if pos != "名詞":
continue # 1文字やストップワード除外
if len(base) <= 1:
continue
if base in custom_stopwords:
continue
tokens.append(base)
return " ".join(tokens)
df["body_tokenized"] = df["body_clean"].apply(tokenize_ja_nouns)
df = df[df["body_tokenized"].str.len() > 0].copy() Through preprocessing, I was able to organize each article as a set of nouns. However, if I apply NMF using simple word counts as they are, words that appear commonly in many articles, such as "data" or "analysis," might have a strong influence, potentially making the meaning of the topics ambiguous.
Therefore, in this blog, I will vectorize the documents using TF-IDF (Term Frequency–Inverse Document Frequency) to emphasize words that are characteristic of each article. By using TF-IDF, I can suppress the influence of general words that appear frequently throughout the entire set while more strongly reflecting words that are characteristic of specific groups of articles. This aims to improve the interpretability of topic extraction by NMF.
# -------------------------
# 4. TF-IDF
# -------------------------
vectorizer = TfidfVectorizer(
max_df=0.90,
min_df=3,
max_features=8000,
ngram_range=(1, 2),
)
X = vectorizer.fit_transform(df["body_tokenized"]) Next, I apply NMF to the text data collected via TF-IDF. There is no clear correct answer for determining the number of topics (K) in NMF, and it is common to decide based on the transition of reconstruction error and the interpretability of the topics. Reconstruction error is an index representing the magnitude of the difference between the original document-word matrix V and the product WH of the W and H obtained through decomposition; the smaller the value, the better the original data is approximated. In practice, it is common to try several values of K and select one while checking the reduction in reconstruction error and the validity of the meaning of the obtained topics. This time, I will try applying NMF targeting 6 topics.
# -------------------------
# 5. NMF
# -------------------------
n_topics = 6
nmf = NMF(n_components=n_topics, random_state=42)
W = nmf.fit_transform(X)
H = nmf.components_ Finally, let's look at each topic decomposed by features.
# -------------------------
# 6. トピック表示
# -------------------------
def print_topics(H, feature_names, n_top_words=12):
for topic_idx, topic in enumerate(H):
top_words = [feature_names[i] for i in topic.argsort()[:-n_top_words - 1:-1]]
print(f"Topic {topic_idx}: {' | '.join(top_words)}")
print("=== トピック ===")
print_topics(H, vectorizer.get_feature_names_out()) The following are the word groups for each extracted topic.
=== Topics ===
【Topic 0】
Data | Analysis | Tags | Measurement | Migration | Advertising | Settings | Search | Events | Users | Datasets | Pages
【Topic 1】
Models | Prediction | Learning | Data | Features | Machine Learning | Machine | Output | Purchasing | Model Training | Accuracy | Curves
【Topic 2】
Internship | Development | Scrum | Work | Team | Participation | Product | Training | Engineer | Challenges | Technology | Time
【Topic 3】
Files | Execution | Tasks | Settings | Below | Usage | Definition | Code | Account | Necessity | Creation | Functions
【Topic 4】
Causal | Estimation | Variables | Observation | Effects | Intervention | Models | Coefficients | Causal Discovery | Discovery | Causal Effects | Approaches
【Topic 5】
Testing | Hypotheses | Significance | Hypothesis Testing | Sample Size | Tests | Distribution | Effects | Statistics | Significance Level | Level | Rejection
Looking at the words included in the decomposed topics, each topic is roughly divided into
【Topic 0】
Measurement, tag design, and analysis operations (advertising/events/datasets/pages)
【Topic 1】
Predictive models and machine learning (prediction/learning/features/accuracy/purchasing)
【Topic 2】
Team development and internship experience (internship/scrum/training/team/work)
【Topic 3】
Implementation procedures, settings, and task execution (files/execution/settings/code/functions/account)
【Topic 4】
Causal inference and effect estimation (causal/estimation/intervention/effects/variables/coefficients)
【Topic 5】
Hypothesis testing and statistical judgment (testing/hypotheses/significance/distribution/sample size/rejection)
It seems they are divided into the above. Looking at about three representative articles for each topic, they were as follows.
【Topic 0】
- Securely sharing data using BigQuery Data Clean Rooms
- Analysis connecting GA4 and Google Search Console data
- Universal Analytics will end in the summer of 2023
【Topic 1】
- Executing SageMaker Autopilot using the SDK
- Technology to understand AI: Principles and implementation of SHAP
- Comparing Amazon SageMaker Canvas and DataRobot
【Topic 2】
- Participation record of Dentsu Digital work-style internship (February 2021 Engineer Course, Shibuya)
- Participating in the Dentsu Digital internship (December 2020 Engineer Course, Nakayama)
- Dentsu Digital internship participation record (December 2020 Engineer Course, Katase)
【Topic 3】
- How to execute ADH and BigQuery queries with Airflow
- Implementing API call permission checks using Go Protocol Buffer Message API V2 Reflection and gRPC Server-side Interceptor
- DevOps in-house tools created in 2020
【Topic 4】
- Running the statistical causal discovery method BMLiNGAM on Google Colab
- Running the statistical causal discovery method LiNGAM on Google Colab
- Introduction to the causal discovery method VAR-LiNGAM considering time-series properties
【Topic 5】
- A/A testing method using non-inferiority testing
- The problem of multiple testing in statistical hypothesis testing and how to deal with it
- The concept of effect size in statistical hypothesis testing and calculation of required sample size
Even looking at the article titles, it seems the topics can be generally classified.
4. Summary
This time, I provided a simple explanation of the mechanism of NMF and performed feature decomposition using real data. Although NMF is not flashy, I feel it is a very easy-to-handle method due to its high interpretability.
Thank you for reading.
