Tạo dữ liệu đặc trưng cho phân loại

Nhập môn Spark SQL bằng Python

Mark Plutowski

Data Scientist

Biến đổi một mảng đặc dày

from pyspark.sql.functions import udf
from pyspark.sql.types import IntegerType
bad_udf = udf(lambda x:
              x.indices[0] 
              if (x and hasattr(x, "toArray") and x.numNonzeros()) 
              else 0,
              IntegerType())
Nhập môn Spark SQL bằng Python

Biến đổi một mảng đặc dày

try:
    df.select(bad_udf('outvec').alias('label')).first()
except Exception as e:
    print(e.__class__)
    print(e.errmsg)
<class 'py4j.protocol.Py4JJavaError'>
Đã xảy ra lỗi khi gọi o90.collectToPython.
Nhập môn Spark SQL bằng Python

Phải ép kiểu đúng cho kiểu trả về của UDF

first_udf = udf(lambda x:
               int(x.indices[0]) 
               if (x and hasattr(x, "toArray") and x.numNonzeros())
               else 0,
               IntegerType())
Nhập môn Spark SQL bằng Python

UDF hoạt động

+-------+--------------------+-----+--------------------+-------------------+
|endword|                 doc|count|            features|             outvec|
+-------+--------------------+-----+--------------------+-------------------+
|     it|[please, do, not,...| 1149|(12847,[15,47,502...|  (12847,[7],[1.0])|
| holmes|[start, of, the, ...|  107|(12847,[0,3,183,1...|(12847,[145],[1.0])|
|      i|[the, adventures,...|  103|(12847,[0,3,35,14...| (12847,[11],[1.0])|
+-------+--------------------+-----+--------------------+-------------------+
df.withColumn('label', k_udf('outvec')).drop('outvec').show(3)
+-------+--------------------+-----+--------------------+-----+
|endword|                 doc|count|            features|label|
+-------+--------------------+-----+--------------------+-----+
|     it|[please, do, not,...| 1149|(12847,[15,47,502...|    7|
| holmes|[start, of, the, ...|  107|(12847,[0,3,183,1...|  145|
|      i|[the, adventures,...|  103|(12847,[0,3,35,14...|   11|
+-------+--------------------+-----+--------------------+-----+
Nhập môn Spark SQL bằng Python

CountVectorizer

  • ETS: Extract Transform Select
  • CountVectorizer là bộ Trích xuất đặc trưng
  • Đầu vào là mảng chuỗi
  • Đầu ra là vector
Nhập môn Spark SQL bằng Python

Huấn luyện CountVectorizer

from pyspark.ml.feature import CountVectorizer

cv = CountVectorizer(inputCol='words',            
                     outputCol="features")
model = cv.fit(df)
result = model.transform(df)
print(result)
DataFrame[words: array<string>, features: vector]

# Mảng chuỗi đặc dày bên trái, vector số nguyên đặc dày bên phải
+-------------------------+--------------------------------------+
|words                    |features                              |
+-------------------------+--------------------------------------+
|[Hello, world]           |(10,[7,9],[1.0,1.0])                  |
|[How, are, you?]         |(10,[1,3,4],[1.0,1.0,1.0])            |
|[I, am, fine, thank, you]|(10,[0,2,5,6,8],[1.0,1.0,1.0,1.0,1.0])|
+-------------------------+--------------------------------------+

Nhập môn Spark SQL bằng Python

Ayo berlatih!

Nhập môn Spark SQL bằng Python

Preparing Video For Download...