13

mllib.recommendationSpark のライブラリを使用して、プロトタイプのレコメンダー システムを構築したいと考えています。ただし、私が持っているユーザー データの形式は、次のような形式です。

AB123XY45678
CD234WZ12345
EF345OOO1234
GH456XY98765
....

ライブラリを使用する場合mllib.recommendation、クラスの API によるとRating、ユーザー ID は整数でなければなりません (また、連続している必要がありますか?)

実際のユーザー ID と Spark が使用する数値 ID の間で何らかの変換を行う必要があるようです。しかし、どうすればいいですか?

4

4 に答える 4

5

文字列を一意の整数インデックスに変換するには、ユーザー ID に対して StringIndexer を実行する必要があります。継続的である必要はありません。

これは、 https://www.aihello.comのアイテム推奨エンジンに使用されます。

df is (user:String, product,rating)

  val stringindexer = new StringIndexer()
      .setInputCol("user")
      .setOutputCol("userNumber")
  val modelc = stringindexer.fit(df)
  val  df = modelc.transform(df)
于 2017-01-28T00:17:45.733 に答える
2

@Ganesh Krishnan は正しいです。StringIndexer はこの問題を解決します。

from pyspark.ml.feature import OneHotEncoder, StringIndexer
from pyspark.sql import SQLContext
>>> spark = SQLContext(sc)                                                                             
>>> df = spark.createDataFrame(
...     [(0, "a"), (1, "b"), (2, "c"), (3, "a"), (4, "a"), (5, "c")],
...     ["id", "category"])

| id|category|
+---+--------+
|  0|       a|
|  1|       b|
|  2|       c|
|  3|       a|
|  4|       a|
|  5|       c|
+---+--------+
>>> stringIndexer = StringIndexer(inputCol="category", outputCol="categoryIndex")
>>> model = stringIndexer.fit(df)
>>> indexed = model.transform(df)
>>> indexed.show()
+---+--------+-------------+
| id|category|categoryIndex|
+---+--------+-------------+
|  0|       a|          0.0|
|  1|       b|          2.0|
|  2|       c|          1.0|
|  3|       a|          0.0|
|  4|       a|          0.0|
|  5|       c|          1.0|
+---+--------+-------------+

>>> converter = IndexToString(inputCol="categoryIndex", outputCol="originalCategory")
>>> converted = converter.transform(indexed)
>>> converted.show()
+---+--------+-------------+----------------+
| id|category|categoryIndex|originalCategory|
+---+--------+-------------+----------------+
|  0|       a|          0.0|               a|
|  1|       b|          2.0|               b|
|  2|       c|          1.0|               c|
|  3|       a|          0.0|               a|
|  4|       a|          0.0|               a|
|  5|       c|          1.0|               c|
+---+--------+-------------+----------------+

>>> converted.select("id", "originalCategory").show()
+---+----------------+
| id|originalCategory|
+---+----------------+
|  0|               a|
|  1|               b|
|  2|               c|
|  3|               a|
|  4|               a|
|  5|               c|
+---+----------------+
于 2017-05-11T03:08:00.023 に答える