データにラベルを付ける
データフレーム df があり、列は endword: string、features: vector、outvec: vector です。endword が "him" に等しい行を選択し、整数値 1 を持つ列 label を追加してください。次に、union 演算を使って、endword が him に等しくない同数の行を追加し、これらの追加行には label = 0 を持たせます。
補足として、SQL での「等しくない」比較は <> を使います。
この演習はコースの一部です
Pythonで学ぶ Spark SQL 入門
演習の手順
lit関数をインポートします。- endword が 'him' の行を選び、値 1 の整数列
labelを追加します。 - endword が 'him' ではない行を選び、値 0 の整数列
labelを追加します。 - 正例の数と同じ数の負例を用いて、これら 2 つの集合を union します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Import the lit function
from pyspark.____ import lit
# Select the rows where endword is 'him' and label 1
df_pos = df.where("____ = 'him'")\
.withColumn('label', lit(____))
# Select the rows where endword is not 'him' and label 0
df_neg = df.where("endword <> '____'")\
.withColumn('label', ____(0))
# Union pos and neg in equal number
df_examples = df_pos.____(df_neg.limit(df_pos.count()))
print("Number of examples: ", df_examples.count())
df_examples.where("endword <> 'him'").sample(False, .1, 42).show(5)