通过numpy向量化对某些pyspark数据帧列进行NLP分析

如何解决通过numpy向量化对某些pyspark数据帧列进行NLP分析

我想对pyspark数据帧中的字符串列进行一些NLP分析。

df:

 year month u_id rating_score p_id review
 2010 09    tvwe  1           p_5  I do not like it because its size is not for me.  
 2011 11    frsa  1           p_7  I am allergic to the peanut elements.  
 2015 5     ybfd  1           p_2  It is a repeated one,please no more.
 2016 7     tbfb  2           p_2  It is not good for my oil hair.
 

每个p_id代表一个项目。 每个u_id可能对每个项目都有一些评论评论可能是几个字,一个句子或一个段落,甚至是表情符号。

我想找到物品被评为低或高的根本原因。 例如,有多少“ u_id”抱怨物品的尺寸,化学元素过敏或其他与物品功能有关的问题。

How to iterate over rows in a DataFrame in Pandas,我了解到将数据帧转换为numpy数组,然后使用矢量化进行NLP分析,效率更高。

我正在尝试使用SparkNLP为年,月,u_id,p_id的每个注释提取形容词和名词短语。

我不确定如何应用numpy向量化以非常有效地做到这一点。

我的py3代码

from sparknlp.pretrained import PretrainedPipeline
df = spark.sql('select year,month,u_id,p_id,comment from MY_DF where rating_score = 1 and isnull(comment) = false')
import numpy as np

trainseries = df['comment'].apply(lambda x : np.array(x.toArray())).as_matrix().reshape(-1,1)

text = np.apply_along_axis(lambda x : x[0],1,trainseries) # TypeError: 'Column' object is not callable

pipeline_dl = PretrainedPipeline('explain_document_dl',lang='en') # 
result = pipeline_dl.fullAnnotate(text)

代码不起作用。 我还需要在向量化中保留其他列(例如,年,月,u_id,p_id),并确保NLP分析结果可以与年,月,u_id,p_id保持一致。

我不喜欢这样 How to convert a pyspark dataframe column to numpy array,因为collect()太慢。

谢谢

解决方法

IIUC,您不需要Numpy(Spark内部处理矢量化),只需执行transform,然后从结果数据框中选择并过滤适当的信息即可:

from sparknlp.pretrained import PretrainedPipeline

df = spark.sql('select year,month,u_id,p_id,comment from MY_DF where rating_score = 1 and isnull(comment) = false')

df1 = df.withColumnRenamed('comment','text')

pipeline_dl = PretrainedPipeline('explain_document_dl',lang='en')

result = pipeline_dl.transform(df1)

df_new = result.selectExpr(
  *df1.columns,'transform(filter(pos,p -> p.result rlike "^(?:NN|JJ)"),x -> x.metadata.word) as words'
)

输出:

df_new.show(10,0)
+-----+-----+----+------------+----+------------------------------------------------+----------------------------+
|years|month|u_id|rating_score|p_id|text                                            |words                       |
+-----+-----+----+------------+----+------------------------------------------------+----------------------------+
|2010 |09   |tvwe|1           |p_5 |I do not like it because its size is not for me.|[size]                      |
|2011 |11   |frsa|1           |p_7 |I am allergic to the peanut elements.           |[allergic,peanut,elements]|
|2015 |5    |ybfd|1           |p_2 |It is a repeated one,please no more.           |[more]                      |
|2016 |7    |tbfb|2           |p_2 |It is not good for my oil hair.                 |[good,oil,hair]           |
+-----+-----+----+------------+----+------------------------------------------------+----------------------------+

注意:

(1)result = pipeline.fullAnnotate(df,'comment')是将comment重命名为text然后执行pipeline.transform(df1)的快捷方式。 fullAnnotate的第一个参数可以是DataFrame,List或String。

(2)https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html中的POS标签列表

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?