手動による類似度測定
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
前述のように、k 平均法では、最も近いセントロイドにポイントが割り当てられます。「最も近い」とはどういう意味ですか?
特徴データに K 平均法を適用するには、すべての特徴データを 1 つの数値に結合する類似性指標を定義する必要があります。これは手動の類似性指標と呼ばれます。
靴のデータセットについて考えてみましょう。そのデータセットに靴のサイズのみが特徴として含まれている場合は、2 つの靴の類似性をサイズの差異で定義できます。サイズの差が小さいほど、靴の類似性が高くなります。
その靴のデータセットにサイズと価格の 2 つの数値特徴がある場合、それらを組み合わせて類似性を示す単一の数値にすることができます。まず、両方の特徴量を比較できるようにデータをスケーリングします。
- サイズ: 靴のサイズはガウス分布を形成していると考えられます。確認します。
次に、データを正規化します。
- 価格(p): データはポアソン分布である可能性があります。確認します。十分なデータがある場合は、データを分位に変換し、 \([0,1]\)にスケーリングします。
次に、二乗平均平方根誤差(RMSE)を計算して、2 つの特徴を組み合わせます。この類似性の大まかな測定値は\(\sqrt{\frac{(s_i - s_j)^2+(p_i - p_j)^2}{2}}\)で表されます。
簡単な例として、米国サイズが 8 と 11、価格が 120 と 150 の 2 足の靴の類似度を計算します。分布を把握するのに十分なデータがないため、正規化や分位を使用せずにデータをスケーリングします。
アクション | メソッド |
サイズをスケーリングします。 |
最大の靴のサイズが 20 だとします。8 と 11 を最大サイズ 20 で割ると、0.4 と 0.55 になります。 |
価格を調整します。 |
120 と 150 を最大価格 150 で割ると、0.8 と 1 になります。 |
サイズの違いを確認します。 |
\(0.55 - 0.4 = 0.15\) |
価格の差額を確認します。 |
\(1 - 0.8 = 0.2\) |
RMSE を計算します。 |
\(\sqrt{\frac{0.2^2+0.15^2}{2}} = 0.17\) |
直感的には、特徴データが類似しているほど、類似度測定値は増加します。代わりに、類似性測定値(RMSE)が実際に減少します。類似度測定値を 1 から減算して、直感に沿うようにします。
\[\text{Similarity} = 1 - 0.17 = 0.83\]
一般に、データを準備するで説明されているように数値データを準備し、ユークリッド距離を使用してデータを結合できます。
そのデータセットに靴のサイズと靴の色の両方が含まれている場合はどうなりますか?色はカテゴリデータです。これは、ML 集中講座のカテゴリデータの操作で説明しています。カテゴリデータは、数値のサイズデータと組み合わせるのが難しいです。Mobility Print を実行できるシステムは次のとおりです。
- 単一値(単価)(車の色(「白」または「青」のいずれか)など)
- 複数値(多価): 映画のジャンルなど(映画は「アクション」と「コメディ」の両方である場合もあれば、「アクション」のみである場合もあります)
単価データが一致する場合(2 足の青い靴の場合など)は、例間の類似度は 1 です。それ以外の場合は、類似度は 0 です。
映画のジャンルなどの多価データは扱いにくいものです。映画のジャンルが固定されている場合は、共通値の比率(ジャカード類似度)を使用して類似度を計算できます。ジャッカード類似性の計算例:
- [“comedy”,”action”] and [“comedy”,”action”] = 1
- [“comedy”,”action”] と [“action”] = ½
- [“comedy”,”action”] and [“action”, "drama"] = ⅓
- [“comedy”,”action”] and [“non-fiction”,”biographical”] = 0
カテゴリデータの手動類似性測定には、ジャカード類似性以外にも方法があります。他の 2 つの例:
- 郵便番号は、ユークリッド距離を計算する前に緯度と経度に変換できます。
- 色は数値の RGB 値に変換でき、値の差はユークリッド距離に結合されます。
詳細については、カテゴリデータの操作をご覧ください。
一般的に、手動の類似性測定は実際の類似性に直接対応している必要があります。選択した指標がこの条件を満たしていない場合、その指標はエンコードする情報をエンコードしていません。
類似度を計算する前に、データを慎重に前処理します。このページの例は簡素化されています。ほとんどの実際のデータセットは大きく複雑です。前述のように、数値データを処理する場合は、デフォルトで分位を選択することをおすすめします。
データの複雑さが増すにつれて、類似性測定を手動で作成することが難しくなります。その場合は、教師あり類似性測定に切り替えます。教師あり機械学習モデルが類似性を計算します。これについては後で詳しく説明します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-02-25 UTC。
[null,null,["最終更新日 2025-02-25 UTC。"],[[["\u003cp\u003eK-means clustering assigns data points to the nearest centroid based on a similarity measure.\u003c/p\u003e\n"],["\u003cp\u003eA manual similarity measure combines all feature data into a single numeric value for comparison.\u003c/p\u003e\n"],["\u003cp\u003eNumerical features can be scaled and combined using RMSE, while categorical features can use Jaccard similarity or other methods.\u003c/p\u003e\n"],["\u003cp\u003eA good similarity measure accurately reflects real-world similarity and often requires careful data preprocessing.\u003c/p\u003e\n"],["\u003cp\u003eFor complex data, a supervised similarity measure using a machine learning model may be more suitable.\u003c/p\u003e\n"]]],[],null,["# Manual similarity measure\n\nAs just shown, k-means assigns points to their closest centroid. But what does\n\"closest\" mean?\n\nTo apply k-means to feature data, you will need to define a measure of\nsimilarity that combines all the feature data into a single numeric value,\ncalled a **manual similarity measure**.\n\nConsider a shoe dataset. If that dataset has shoe size as its only feature,\nyou can define the similarity of two shoes in terms of the difference between\ntheir sizes. The smaller the numerical difference between sizes, the greater the\nsimilarity between shoes.\n\nIf that shoe dataset had two numeric features, size and price, you can combine\nthem into a single number representing similarity. First scale the data so\nboth features are comparable:\n\n- Size (s): Shoe size probably forms a Gaussian distribution. Confirm this. Then normalize the data.\n- Price (p): The data is probably a Poisson distribution. Confirm this. If you have enough data, convert the data to quantiles and scale to \\\\(\\[0,1\\]\\\\).\n\nNext, combine the two features by calculating the\n[root mean squared error](/machine-learning/glossary#RMSE) (RMSE).\nThis rough measure of similarity is given by\n\\\\(\\\\sqrt{\\\\frac{(s_i - s_j)\\^2+(p_i - p_j)\\^2}{2}}\\\\).\n\nFor a simple example, calculate similarity for two shoes with US sizes\n8 and 11, and prices 120 and 150. Since we don't have enough data to understand\nthe distribution, we'll scale the data without normalizing or using\nquantiles.\n\n| Action | Method |\n|-------------------------------|--------------------------------------------------------------------------------------------------------|\n| Scale the size. | Assume a maximum possible shoe size of 20. Divide 8 and 11 by the maximum size 20 to get 0.4 and 0.55. |\n| Scale the price. | Divide 120 and 150 by the maximum price 150 to get 0.8 and 1. |\n| Find the difference in size. | \\\\(0.55 - 0.4 = 0.15\\\\) |\n| Find the difference in price. | \\\\(1 - 0.8 = 0.2\\\\) |\n| Calculate the RMSE. | \\\\(\\\\sqrt{\\\\frac{0.2\\^2+0.15\\^2}{2}} = 0.17\\\\) |\n\nIntuitively, your similarity measure should increase when feature data is more\nsimilar. Instead, your similarity measure (RMSE) actually decreases. Make your\nsimilarity measure follow your intuition by subtracting it from 1.\n\n\\\\\\[\\\\text{Similarity} = 1 - 0.17 = 0.83\\\\\\]\n\nIn general, you can prepare numerical data as described in\n[Prepare data](/machine-learning/clustering/prepare-data), then combine the\ndata by using Euclidean distance.\n\nWhat if that dataset included both shoe size and shoe color? Color is\n[categorical data](/machine-learning/glossary#categorical_data),\ndiscussed in Machine Learning Crash Course in\n[Working with categorical data](/machine-learning/crash-course/categorical-data).\nCategorical data is harder to combine with the numerical size data. It can be:\n\n- Single-valued (univalent), such as a car's color (\"white\" or \"blue\" but never both)\n- Multi-valued (multivalent), such as a movie's genre (a movie can be both \"action\" and \"comedy,\" or only \"action\")\n\nIf univalent data matches, for example in the case of two pairs of blue shoes,\nthe similarity between the examples is 1. Otherwise, similarity is 0.\n\nMultivalent data, like movie genres, is harder to work with. If there are a\nfixed set of movie genres, similarity can be calculated using the ratio of\ncommon values, called\n[**Jaccard similarity**](https://wikipedia.org/wiki/Jaccard_index). Example\ncalculations of Jaccard similarity:\n\n- \\[\"comedy\",\"action\"\\] and \\[\"comedy\",\"action\"\\] = 1\n- \\[\"comedy\",\"action\"\\] and \\[\"action\"\\] = ½\n- \\[\"comedy\",\"action\"\\] and \\[\"action\", \"drama\"\\] = ⅓\n- \\[\"comedy\",\"action\"\\] and \\[\"non-fiction\",\"biographical\"\\] = 0\n\nJaccard similarity is not the only possible manual similarity measure for\ncategorical data. Two other examples:\n\n- **Postal codes** can be converted into latitude and longitude before calculating Euclidean distance between them.\n- **Color** can be converted into numeric RGB values, with differences in values combined into Euclidean distance.\n\nSee [Working with categorical data](/machine-learning/crash-course/categorical-data)\nfor more.\n\nIn general, a manual similarity measure must directly correspond\nto actual similarity. If your chosen metric does not, then it isn't encoding the\ninformation you want it to encode.\n\nPre-process your data carefully before calculating a similarity measure. The\nexamples on this page are simplified. Most real-world datasets are large\nand complex. As previously mentioned, quantiles are a good default choice\nfor processing numeric data.\n\nAs the complexity of data increases, it becomes harder to create a manual\nsimilarity measure. In that situation, switch to a\n**supervised similarity measure**, where a supervised machine\nlearning model calculates similarity. This will be discussed in more detail\nlater.\n\n| **Key terms:**\n|\n| \u003cbr /\u003e\n|\n| - [categorical data](/machine-learning/glossary#categorical_data)\n| - [Jaccard similarity](https://wikipedia.org/wiki/Jaccard_index)\n|\n| \u003cbr /\u003e\n|\n\u003cbr /\u003e"]]