2016-12-10 7 views
0

私はmovielense ml-100Kデータセットのユーザーデータを持っています。スケーラのRDDで1つのホットエンコーディング

サンプル行がある - 私はRDDとしてデータを読みました

1|24|M|technician|85711 
2|53|F|other|94043 
3|23|M|writer|32067 
4|24|M|technician|43537 
5|33|F|other|15213 

scala> val user_data = sc.textFile("/home/user/Documents/movielense/ml-100k/u.user").map(x=>x.split('|')) 
user_data: org.apache.spark.rdd.RDD[Array[String]] = MapPartitionsRDD[5] at map at <console>:29 

scala> user_data.take(5) 
res0: Array[Array[String]] = Array(Array(1, 24, M, technician, 85711), Array(2, 53, F, other, 94043), Array(3, 23, M, writer, 32067), Array(4, 24, M, technician, 43537), Array(5, 33, F, other, 15213)) 


# encode distinct profession with zipWithIndex - 
scala> val indexed_profession = user_data.map(x=>x(3)).distinct().sortBy[String](x=>x).zipWithIndex() 
indexed_profession: org.apache.spark.rdd.RDD[(String, Long)] = ZippedWithIndexRDD[18] at zipWithIndex at <console>:31 

scala> indexed_profession.collect() 
res1: Array[(String, Long)] = Array((administrator,0), (artist,1), (doctor,2), (educator,3), (engineer,4), (entertainment,5), (executive,6), (healthcare,7), (homemaker,8), (lawyer,9), (librarian,10), (marketing,11), (none,12), (other,13), (programmer,14), (retired,15), (salesman,16), (scientist,17), (student,18), (technician,19), (writer,20)) 

follows-として、私は職業欄に1つのホットエンコーディングをやりたいです。

予想される出力は -

userId Age Gender Occupation Zipcodes technician other writer 
1  24 M  technician 85711  1   0  0 
2  53 F  other  94043  0   1  0 
3  23 M  writer  32067  0   0  1 
4  24 M  technician 43537  1   0  0 
5  33 F  other  15213  0   1  0 

私はScalaでRDDにこれを達成するにはどうすればよいです。 RDDでデータフレームに変換せずに操作を実行したい。

すべてのヘルプ

おかげ

+0

投票の前に、ユーザーに完全な質問を投稿させてください。無意識のうちにインターネットが切断された不完全な質問が投稿されました。 – r4sn4

+0

SparkのデフォルトOneホットエンコーダを使用したくない理由は何ですか?参照:http://stackoverflow.com/questions/31872396/how-to-encode-categorical-features-in-apache-sparkまたはSpark2データフレームAPI:https://spark.apache.org/docs/2.0.2 /ml-features.html#onehotencoder)。 – GPI

+0

なんとかこのスレッドをスキップしました...このアプロ...を試してみましょう – r4sn4

答えて

0

私は道を次でこれをやった -

1)リード・ユーザ・データ -

scala> val user_data = sc.textFile("/home/user/Documents/movielense/ml-100k/u.user").map(x=>x.split('|')) 

2)データ -

の5行を表示
scala> user_data.take(5) 
res0: Array[Array[String]] = Array(Array(1, 24, M, technician, 85711), Array(2, 53, F, other, 94043), Array(3, 23, M, writer, 32067), Array(4, 24, M, technician, 43537), Array(5, 33, F, other, 15213)) 
scala> val encode_user_data = user_data.map{ x => (x(0),x(1),x(2),x(3),x(4),encode(x(3)))} 
- 0 3)ユーザデータにエンコード関数を適用

scala> def encode(x: String) = 
|{ 
| var encodeArray = Array.fill(21)(0) 
| encodeArray(indexed_user.get(x).get.toInt)=1 
| encodeArray 
} 

5)職業のワンホット符号化を行いindexing-

scala> val indexed_profession = user_data.map(x=>x(3)).distinct().sortBy[String](x=>x).zipWithIndex().collectAsMap() 

scala> indexed_profession 
res35: scala.collection.Map[String,Long] = Map(scientist -> 17, writer -> 20, doctor -> 2, healthcare -> 7, administrator -> 0, educator -> 3, homemaker -> 8, none -> 12, artist -> 1, salesman -> 16, executive -> 6, programmer -> 14, engineer -> 4, librarian -> 10, technician -> 19, retired -> 15, entertainment -> 5, marketing -> 11, student -> 18, lawyer -> 9, other -> 13) 

4)を作成するエンコード機能により職業の地図を作成します

6)符号化データを表示する -

scala> encode_user_data.take(6) 
res71: Array[(String, String, String, String, String, Array[Int])] = 

1,24,M,technician,85711,Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)), 
2,53,F,other,94043,Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)), 
3,23,M,writer,32067,Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)), 
4,24,M,technician,43537,Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)), 
5,33,F,other,15213,Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)), 
6,42,M,executive,98101,Array(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))) 
+0

これ以上の解決策はありません。投稿してください – r4sn4

関連する問題