- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 pyspark 的新手。我有如下所示的 Pandas 代码。
bindt = df[df[var].notnull()][var].quantile([0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]).unique()
df['{0}_quartile'.format(var)] = pd.cut(df[var], bindt, labels=False, include_lowest=True )
我在 pyspark 2.x 中找到了“approxQuantile”,但在 pyspark 1.6.0 中没有找到任何此类
我的示例输入:
df.show()
+-----------+----------+---------------+--------------+------------------------+
| id | col_1 |col_2 |col_3 |col_4 |
+-----------+----------+---------------+--------------+------------------------+
|1.10919E+16|3988487.35| -236751.43| -362208.07| 0.660000|
|1.10919E+16|3988487.35| -236751.43| -362208.07| 0.900000|
|1.10919E+16|3988487.35| -236751.43| -362208.07| 0.660000|
|1.10919E+16| 36718.55| null| null| 0.860000|
|1.10919E+16| 36718.55| null| null| 0.780000|
|1.10919E+16| 36718.55| null| null| 0.660000|
|1.10919E+16| 36718.55| null| null| 0.900000|
|1.10919E+16| 36718.55| null| null| 0.660000|
df.collect()
[Row(id=u'1.11312E+16', col_1=Decimal('367364.44'), col_2=Decimal('-401715.23'), col_3=Decimal('-1649917.53'), col_4=Decimal('0.080000')),
Row(id=u'1.11312E+16', col_1=Decimal('367364.44'), col_2=Decimal('-401715.23'), col_3=Decimal('-1649917.53'), col_4=Decimal('0.780000')),
Row(id=u'1.11312E+16', col_1=Decimal('367364.44'), col_2=Decimal('-401715.23'), col_3=Decimal('-1649917.53'), col_4=Decimal('0.780000')),
Row(id=u'1.11312E+16', col_1=Decimal('367364.44'), col_2=Decimal('-401715.23'), col_3=Decimal('-1649917.53'), col_4=Decimal('0.860000')),
Row(id=u'1.11312E+16', col_1=Decimal('367364.44'), col_2=Decimal('-401715.23'), col_3=Decimal('-1649917.53'), col_4=Decimal('0.330000'))]
我必须为所有输入列循环上述逻辑。
for var in df.columns:
bindt = df[df[var].notnull()][var].quantile([0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]).unique()
df['{0}_quartile'.format(var)] = pd.cut(df[var], bindt, labels=False, include_lowest=True )
谁能建议如何在 pyspark 1.6 dataframe 中重写上述代码。
提前致谢
最佳答案
如果您使用的是 pyspark 2.x,则可以使用 QuantileDiscretizer来自使用 approxQuantile() 的 ml lib和 Bucketizer在幕后。
但是,由于您使用的是 pyspark 1.6.x,因此您需要:
您可以通过两种方式找到分位数值:
通过计算 percent_rank() 来计算列的百分位数并提取百分位值接近所需分位数的列值
关注the methods in this answer其中解释了如何使用 pyspark < 2.0.0
这是我的近似分位数值的示例实现:
from pyspark.sql import functions as F
from pyspark.sql import Window
def compute_quantiles(df, col, quantiles):
quantiles = sorted(quantiles)
# 1. compute percentile
df = df.withColumn("percentile", F.percent_rank().over(Window.orderBy(col)))
# 2. categorize quantile based on the desired quantile and compute errors
df = df.withColumn("percentile_cat1", F.lit(-1.0))
df = df.withColumn("percentile_err1", F.lit(-1.0))
df = df.withColumn("percentile_cat2", F.lit(-1.0))
df = df.withColumn("percentile_err2", F.lit(-1.0))
# check percentile with the lower boundaries
for idx in range(0, len(quantiles)-1):
q = quantiles[idx]
df = df.withColumn("percentile_cat1", F\
.when( (F.col("percentile_cat1") == -1.0) &
(F.col("percentile") <= q), q)\
.otherwise(F.col("percentile_cat1")))
df = df.withColumn("percentile_err1", F\
.when( (F.col("percentile_err1") == -1.0) &
(F.col("percentile") <= q),
F.pow(F.col("percentile") - q, 2))\
.otherwise(F.col("percentile_err1")))
# assign the remaining -1 values in the error to the largest squared error of 1
df = df.withColumn("percentile_err1", F\
.when(F.col("percentile_err1") == -1.0, 1)\
.otherwise(F.col("percentile_err1")))
# check percentile with the upper boundaries
for idx in range(1, len(quantiles)):
q = quantiles[idx]
df = df.withColumn("percentile_cat2", F\
.when((F.col("percentile_cat2") == -1.0) &
(F.col("percentile") <= q), q)\
.otherwise(F.col("percentile_cat2")))
df = df.withColumn("percentile_err2",F\
.when((F.col("percentile_err2") == -1.0) &
(F.col("percentile") <= q),
F.pow(F.col("percentile") - q, 2))\
.otherwise(F.col("percentile_err2")))
# assign the remaining -1 values in the error to the largest squared error of 1
df = df.withColumn("percentile_err2", F\
.when(F.col("percentile_err2") == -1.0, 1)\
.otherwise(F.col("percentile_err2")))
# select the nearest quantile to the percentile
df = df.withColumn("percentile_cat", F\
.when(F.col("percentile_err1") < F.col("percentile_err2"),
F.col("percentile_cat1"))\
.otherwise(F.col("percentile_cat2")))
df = df.withColumn("percentile_err", F\
.when(F.col("percentile_err1") < F.col("percentile_err2"),
F.col("percentile_err1"))\
.otherwise(F.col("percentile_err2")))
# 3. approximate quantile values by choosing the value with the lowest error at each percentile category
df = df.withColumn("approx_quantile", F\
.first(col).over(Window\
.partitionBy("percentile_cat")\
.orderBy(F.asc("percentile_err"))))
return df
def extract_quantiles(df):
df_quantiles = df.select("percentile_cat", "approx_quantile").distinct()
rows = df_quantiles.collect()
quantile_values = [ row.approx_quantile for row in rows ]
return quantile_values
我想从上面实现的是计算列中每一行的百分位数,并将其分类到最接近的分位数。可以通过选择与百分位数具有最低差异(平方误差)的最低分位数类别来将百分位数分类为最接近的分位数。
<强>1。计算百分位数首先,我使用 percent_rank() 计算列的百分位数, 一个 Window function在 pyspark 中。您可以将 Window 视为数据的分区规范。由于percent_rank()
是Window函数,所以需要传入Window。
最接近百分位数的分位数类别可以低于、等于或高于。因此,我需要两次计算误差:第一次将百分位数与分位数下限进行比较,第二次将其与分位数上限进行比较。请注意,≤ 运算符用于检查百分位数是否小于或等于 边界。在知道百分位数的直接上下分位数边界后,我们可以通过选择低于或等于或高于或等于误差最小的分位数类别,将百分位数分配给最近的分位数类别。
<强>3。近似分位数一旦我们知道每个百分位数的所有最接近的分位数类别,我们就可以近似分位数值:它是每个分位数类别中误差最低的值。可以使用 first()
计算此近似分位数值使用 Window 在每个类别分区上运行函数.接下来,要提取分位数值,我们只需从数据框中选择唯一的 percentileCategory-approxQuantileValue 对。
在使用 desired_quantiles = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
测试我的数据(~10000 行)后,我发现我的示例实现非常接近 approxQuantile
结果。当我减少提供给 approxQuantile
的误差时,两个结果值变得更接近。
使用 extract_quantiles(compute_quantile(df, col, quantiles))
:
使用 approxQuantile
:
找到分位数后,您可以使用 pyspark 的 Bucketizer 根据分位数对值进行分桶。 Bucketizer 在两个 pyspark 1.6.x 中都可用 [1] [2]和 2.x [3] [4]
这是一个如何执行分桶的示例:
from pyspark.ml.feature import Bucketizer
bucketedData = df
desired_quantiles = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] # must be sorted
for col in df.columns:
quantile_values = extract_quantiles(compute_quantiles(df, col, desired_quantiles))
splits = [ boundary_values ] # replace this with quantile_values
bucketizer = Bucketizer()\
.setInputCol(col)\
.setOutputCol("{}_quantile".format(col))\
.setSplits(splits)
bucketedData = bucketizer.transform(bucketedData)
您可以将 value_boundaries
替换为您在第 1 步中找到的分位数值或您想要的任何桶分割范围。当您使用 bucketizer 时,整个列值范围必须包含在拆分中。否则,指定拆分之外的值将被视为错误。如果您不确定该值,则必须显式提供无限值,例如 -float("inf")
、float("inf")
以涵盖所有 float 值数据的边界。
关于python - pyspark 1.6 中 Pandas 分位数和切割的替代方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803107/
Python 中是否有任何预制的优化工具/库来切割/切片值“小于”某物的列表? 问题来了:假设我有一个列表: a=[1,3,5,7,9] 我想删除所有 2 的项目,将会有很多迭代)。 我也可以使用二进
我创建了一个 fiddle 来更好地展示我想问的问题。 angle crop of pseudo element 这是一张显示所需结果的图片: li.active::after { conte
我有一个字符串如下: 2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576 我想提取
RESTEasy 看起来会忽略尾部斜杠,因此我无法编写带有和不带有尾部斜杠的两个不同的 Web 服务,此示例显示: @Path("foo") public class TestClass {
mov rax,QWORD PTR [rbp-0x10] mov eax,DWORD PTR [rax] add eax,0x1 mov DWORD PTR [rbp-0x14], eax 下
我有以下 xml: External Vendor File External vendor file for so
我在 Python 中工作,并在 Pandas 中使用剪切功能。我想让我的 pd.cut 中的 bins 基于用户定义的逗号分隔整数,具有预定义的上限和下限。换句话说,我想将 bins 预定义为 [0
我正在尝试嵌入 ShareThis将代码写入使用 Bootstrap 创建的网站。 问题是由于某种原因,两个按钮的底部都被切断了,而且我似乎无法使用 Bootstrap 列偏移将按钮居中,因为使用这种
我正在尝试模拟无线传感器节点网络,以研究网络的稳健性。我面临以下问题: 我有一个具有一些边缘容量的节点网络。这相当于算法中的网络流问题。有一个源节点(检测某些事件)和一个接收节点(我的基站)。现在,我
我有一个 UIImageView,带有特定的图像。我还有一个形状奇怪的 UIBezierPath。我想将图像剪切成该形状并返回该形状的新图像。 形式为: func getCut(bezier:UIBe
我有一个大型的全局 .nc 文件数据集,我正试图将它们裁剪到一个较小的区域。我将此区域存储为 .shp 文件。 我曾尝试使用 Qgis 中的 gdal,但需要通过转换每个变量来做到这一点,我必须为所有
我正在使用以下命令来剪切 FLAC 文件:ffmpeg -i input.flac -ss 10s -t 10s -c copy output.flacoutput.flac包含正确的音频持续时间。但
我希望为以下组件制作足迹: AG EMCO HV power supply 现在,这个组件可以通过电路板安装,虽然它不是一个很高的组件,但我希望探索这个选项。我想一个附带问题是:PCB 制造商有多喜欢
我目前正在用 C++ 编写一个小程序(在 Mac 上,其中包含一些 C 代码)并且需要从套接字中检索二进制数据(效果很好),但我还需要切断 HTTP标题。 基本上,我连接到服务器,发送 HTTP GE
我想制作一个Python程序,其中在文本文件中给出DNA序列。它有超过9000个字符。我必须将序列剪成 3 个字符所以我们的框架从1到3读取,然后4到6,然后7到9,这被称为密码子。 例如,序列是 A
我正在开发一个迷你游戏,如果我将 DIN-Ax 作为输入,我需要找出可以在 DIN-A0 纸上画多少行。例如: ;car horizontal and cdr vertical lines (defi
我在下面有这个名为 monitor.log 的报告文件 switch#sh mac address-table int g1/0/1 Mac Address Table -----
我的设备上有 MPEG-TS 文件。我想从设备上的文件开始处截断一个相当准确的时间。 使用 FFmpegWrapper作为基地,我希望能够实现这一目标。 不过,我对 ffmpeg 的 C API 有点
我导出了两个字段:name和header从数据库中使用: SELECT name, header INTO OUTFILE '/var/lib/mysql-files/myfile.txt' FIEL
这个问题在这里已经有了答案: How do you parse and process HTML/XML in PHP? (31 个答案) 关闭 9 年前。 我尝试转换以下字符串 st
我是一名优秀的程序员,十分优秀!