

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Neptune ML のリンク予測モデルを使用した Gremlin リンク予測クエリ
<a name="machine-learning-gremlin-link-prediction-queries"></a>

リンク予測モデルでは、次のような問題が解けます。
+ **先頭ノード予測**: 頂点とエッジタイプを指定すると、その頂点からリンクする可能性が高い頂点はどれか?
+ **末尾ノード予測**: 頂点とエッジラベルを指定すると、その頂点へとリンクする可能性が高い頂点はどれか?

**注記**  
エッジ予測は Neptune ML ではまだサポートされていません。

以下の例では、エッジ `Rated` でリンクされている `User` および `Movie` 頂点を持つ単純なグラフを考えてみましょう。

以下は、映画 `"movie_1"`、`"movie_2"` および `"movie_3"` を評価する可能性が最も高い上位 5 人のユーザーを予測するために使用されるサンプルヘッドノード予測クエリです。

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .with("Neptune#ml.limit", 5)
 .V("movie_1", "movie_2", "movie_3")
 .in("rated").with("Neptune#ml.prediction").hasLabel("user")
```

これは、ユーザー `"user_1"` が評価する可能性が高い上位 5 本の映画を予測するために使用する、テールノード予測の類似のものです。

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out("rated").with("Neptune#ml.prediction").hasLabel("movie")
```

エッジラベルと予測頂点ラベルの両方が必要です。いずれかが省略されると、例外がスローされます。たとえば、頂点ラベルが予測されていない次のクエリでは、例外がスローされます。

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out("rated").with("Neptune#ml.prediction")
```

同様に、エッジラベルのない次のクエリでは、例外がスローされます。

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out().with("Neptune#ml.prediction").hasLabel("movie")
```

これらの例外が返される具体的なエラーメッセージについては、[Neptune ML 例外の一覧](machine-learning-gremlin-exceptions.md)を参照してください。

## その他のリンク予測クエリ
<a name="machine-learning-gremlin-other-link-prediction-queries"></a>

`select()` ステップと `as(` ステップを併用して、予測頂点を入力頂点とともに出力しすることができます。

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("movie_1").as("source")
 .in("rated").with("Neptune#ml.prediction").hasLabel("user").as("target")
 .select("source","target")

g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1").as("source")
 .out("rated").with("Neptune#ml.prediction").hasLabel("movie").as("target")
 .select("source","target")
```

次のような無制限クエリを作成できます。

```
g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("user_1")
 .out("rated").with("Neptune#ml.prediction").hasLabel("movie")

g.with("Neptune#ml.endpoint","node-prediction-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V("movie_1")
 .in("rated").with("Neptune#ml.prediction").hasLabel("user")
```

## リンク予測クエリでの帰納的推論の使用
<a name="machine-learning-gremlin-link-predict-inductive"></a>

Jupyter ノートブックの既存のグラフに、次のように新しいノードを追加するとします。

```
%%gremlin
g.addV('label1').property(id,'101').as('newV1')
 .addV('label2').property(id,'102').as('newV2')
 .V('1').as('oldV1')
 .V('2').as('oldV2')
 .addE('eLabel1').from('newV1').to('oldV1')
 .addE('eLabel2').from('oldV2').to('newV2')
```

その場合、帰納的推論クエリを使用して、新しいノードを考慮に入れたヘッドノードを予測できます。

```
%%gremlin
g.with("Neptune#ml.endpoint", "lp-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').out("eLabel1")
 .with("Neptune#ml.prediction")
 .with("Neptune#ml.inductiveInference")
 .hasLabel("label2")
```

結果:

```
==>V[2]
```

同様に、帰納的推論クエリを使用して、新しいノードを考慮に入れたテールノードを予測できます。

```
%%gremlin
g.with("Neptune#ml.endpoint", "lp-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('102').in("eLabel2")
 .with("Neptune#ml.prediction")
 .with("Neptune#ml.inductiveInference")
 .hasLabel("label1")
```

結果:

```
==>V[1]
```