- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以这不是我需要使用的数据集,但它是我正在使用的一个癌症研究项目的大型数据集(约 180 万个数据点)的模板,所以我想是否可以将其用于与较小的一起工作,然后我可以将其调整为我的大的!作为示例,假设我有以下数据集:
import numpy as np
import pandas as pd
df = pd.DataFrame({
'cond': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B','B', 'B', 'B', 'B', 'B','B','B'],
'Array': ['S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','SS','TT'],
'X': [1, 2, 3, 1, 2 , 3, 4, 7.3, 5.1, 3.2, 1.4, 5.5, 9.9, 3.2, 1.1, 3.3, 1.2, 5.4],
'Y': [3.1, 2.2, 2.1, 1.2, 2.4, 1.2, 1.5, 1.33, 1.5, 1.6, 1.4, 1.3, 0.9, 0.78, 1.2, 4.0, 5.0, 6.0],
'Marker': [2.0, 1.2, 1.2, 2.01, 2.55, 2.05, 1.66, 3.2, 3.21, 3.04, 8.01, 9.1, 7.06, 8.1, 7.9, 5.12, 5.23, 5.15],
'Area': [3.0, 2.0, 2.88, 1.33, 2.44, 1.25, 1.53, 1.0, 0.156, 2.0, 2.4, 6.3, 6.9, 9.78, 10.2, 15.0, 16.0, 19.0]
})
print(df)
这会产生如下所示的输出:
cond Array X Y Marker Area
0 A S 1.0 3.10 2.00 3.000
1 A S 2.0 2.20 1.20 2.000
2 A TT 3.0 2.10 1.20 2.880
3 A TT 1.0 1.20 2.01 1.330
4 A S 2.0 2.40 2.55 2.440
5 A S 3.0 1.20 2.05 1.250
6 A TT 4.0 1.50 1.66 1.530
7 A TT 7.3 1.33 3.20 1.000
8 A S 5.1 1.50 3.21 0.156
9 B S 3.2 1.60 3.04 2.000
10 B TT 1.4 1.40 8.01 2.400
11 B TT 5.5 1.30 9.10 6.300
12 B S 9.9 0.90 7.06 6.900
13 B S 3.2 0.78 8.10 9.780
14 B TT 1.1 1.20 7.90 10.200
15 B TT 3.3 4.00 5.12 15.000
16 B SS 1.2 5.00 5.23 16.000
17 B TT 5.4 6.00 5.15 19.000
好吧,现在我需要做的是根据两个标签“cond”和“Array”来分割它们。我使用的是这样做的
g=df.groupby(['cond','Array'])['Marker']
这将其分成 4 个较小的集合,分为 A-S、A-TT、B-S、B-TT 配对。现在我有一个可以使用的自定义函数。这是该函数的一部分,我将解释它是如何工作的:
def num_to_delete(p,alpha,N):
if p==0.950:
if 1-alpha==0.90:
if N<=60:
m=1
if 60<N<80:
m=round(N/20-2)
if 80<=N:
m=2
if 1-alpha==0.95:
if N<=80:
m=1
if 80<N<=100:
m=round(N/20 -3)
if 100<N:
m=2
return m
好吧,它的工作方式是我向它输入我选择的“p”和“alpha”(真正的函数涵盖了更多 p 和 alpha 的情况)。输入到其中的 N 是较小数据集的元素数量(在本例中,对于 A-S 为 5,对于 A-TT 为 4,等等)。所以我想要发生的是,对于每个较小的数据集,吐出一些要删除的点(在本例中,该函数将始终给我们 1,但我尝试使用该函数对此进行编码超大型数据集的应用)。因为它给出的数字是 1,所以我希望它删除该集合中第 1 个最大的数据点,并告诉我剩下的最高点是多少。
作为示例,对于 A-S 耦合,我有 5 个数据点:2.0、1.2、2.55、2.05 和 3.21。由于有 5 个数据点,我的函数告诉我删除其中 1 个,因此忽略 3.21,并告诉我剩下的最高数据点是多少,在本例中为 2.55。我想对每个耦合执行此操作,但在我的真实数据集中,我将具有不同数量的元素,因此该函数会告诉我为每个耦合删除不同的数量。
我的最终目标是拥有一个如下所示的决赛 table :
cond Array NumDeleted p95/a05 p95/a10
0 A S 1.0 2.55 2.55
1 A TT 1.0 2.01 2.01
2 B S 1.0 7.06 7.06
3 B TT 1.0 8.01 8.01
对于较大的数据集,最后两列中的值会有所不同,因为在大型数据集中,要删除的值的数量差异很大,因此剩余的值也会有所不同。我最终需要根据 p95/a05 和 p95/a10 获得的值更改第二个数据集
无论如何,我很抱歉这个解释太长了,但如果有人能提供帮助,那就太棒了!我希望这是一件相当简单的事情,因为我已经坚持这个问题一个多星期了。
最佳答案
编辑:更通用的解决方案
首先,创建一个闭包
来定义您的配置会有所帮助。这是假设您将来会有更多配置:
def create_num_to_delete(p, alpha):
"""Create a num_to_delete function given p and alpha."""
def num_to_delete(N):
if p == 0.950:
if 1 - alpha == 0.90:
if N <= 60:
m = 1
if 60 < N < 80:
m = round(N/20 - 2)
if 80 <= N:
m = 2
if 1-alpha == 0.95:
if N <= 80:
m = 1
if 80 < N <= 100:
m = round(N/20 -3)
if 100 < N:
m = 2
return m
return num_to_delete
然后您可以使用此闭包来定义配置字典:
configurations = {
'p95/a05': create_num_to_delete(0.95, 0.05),
'p95/a10': create_num_to_delete(0.95, 0.10),
}
然后,定义一个汇总数据的函数。此函数应该依赖于您的配置,以便它保持动态。
def summarize(x):
# The syntax on the right-hand side is called list comprehension.
# As you can probably guess, it's essentially a flattened for-loop that
# produces a list. The syntax starting with "for" is your basic for loop
# statement, and the syntax to the left of "for" is an expression that
# that serves as the value of the resulting list for each iteration
# of the loop.
#
# Here, we are looping through the "num_to_delete" functions we defined in
# our `configurations` dictionary. And calling it in our group `x`.
Ns = [num_to_delete(len(x)) for num_to_delete in configurations.values()]
markers = x['Marker'].sort_values(ascending=False)
highest_markers = []
for N in Ns:
if N == len(x):
highest_markers.append(None)
else:
# Since we know that `markers` is already sorted in descending
# order, all we need to get the highest remaining value is to get
# the value in the *complete list* of values offset by the
# the number of values that need to be deleted (this is `N`).
#
# Since sequences are 0-indexed, simply indexing by `N` is enough.
# For example, if `N` is 1, indexing by `N` would give us
# the marker value *indexed by* 1, which is,
# in a 0-sequenced index, simply the second value.
highest_markers.append(markers.iloc[N])
# Returning a list from an applied groupby function translates into
# a DataFrame which the series index as the columns and the series values
# as the row values. Index in this case is just the list of configuration
# names we have in the `configurations` dictionary.
return pd.Series(highest_markers, index=list(configurations.keys()))
最后,将该函数应用
到您的数据集并重置索引。这使 cond
和 Array
保持为列:
grouped = df.groupby(['cond', 'Array'])
grouped.apply(summarize).reset_index()
输出是:
cond Array p95/a05 p95/a10
0 A S 2.55 2.55
1 A TT 2.01 2.01
2 B S 7.06 7.06
3 B SS NaN NaN
4 B TT 8.01 8.01
希望这有帮助。
关于python - 使用 split-apply-combine 通过自定义函数删除一些值并合并剩下的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279548/
我被这种奇怪的事情难住了。 假设我有这个数组: var array = [{ something: 'special' }, 'and', 'a', 'bunch', 'of', 'paramet
假设我们有这样的代码: let fn1 = Function.apply.bind(Math.max, null); fn1([1, 10, 5]); // returns 10 我知道它是 ES6
所以我尝试通过数据绑定(bind)调用我的 viewModel 原型(prototype)上的方法。我通过“单击”将两个不同的元素数据绑定(bind)到同一方法。当我单击第一个按钮(“新游戏”按钮)时
观察以下代码 trait Example { type O def apply(o: O) def f(o: O) = this.apply(o) } 在Scala中编译良好。我希望我可以
我知道 apply f in H 可用于将假设应用于函数,并且我知道 apply f with a b c 可用于提供参数直接应用 f 时,它无法自行推断。 是否可以以某种方式将两者结合使用? 最佳答
这个问题已经有答案了: How to override apply in a case class companion (10 个回答) 已关闭 6 年前。 我正在尝试重载案例类的 apply 方法:
我有一个自定义的Grails 4.x配置文件。我想为我的应用程序生成一个“apply from”条目。 apply from:"${rootProject.projectDir}/gradle/clo
传统上对象继承如下所示: function Parent() { console.log('parent constructor'); } Parent.prototype.method = f
今天在检查Jasmine 的源代码时here我偶然发现了以下内容: if (queueableFn.timeout) { timeoutId = Function.prototype.appl
据我所知,关键字new会使用this创建一个包含函数中定义的属性的对象。但我不知道如何应用 使用 apply 将其他函数链接到该函数。并且创建的对象在这些函数中具有属性。有人能弄清楚代码中发生了什么吗
我一直在我的 InitComponent 中使用 Ext.Apply,就像这样 Ext.apply(that, { xtype: 'form', items: [.
我们有数百个存储库,并定期从上游接收补丁。作业应用这些补丁 git apply --check .如果没有错误,则应用补丁 git apply 并且更改已提交。如果有任何错误,补丁将标记为 conf
我最近通过调用 console.log.toString() 查看了 firebugs console.log 的代码并得到了这个: function () { return Function.app
拿这个代码: $scope.$apply(function(){ $scope.foo = 'test'; }); 对比这个: $scope.foo = 'test'; $scope.$app
我在 Oracle-12c 中有一个类似于典型论坛的架构 accounts , posts , comments .我正在编写一个查询来获取... 一位用户 该用户的所有帖子 对每个帖子的评论 以及每
我试图更好地理解在 Angular 中使用 $timeout 服务作为一种“安全 $apply”方法的细微差别。基本上在一段代码可以运行以响应 Angular 事件或非 Angular 事件(例如 j
到目前为止,我使用的是 this当我有多个时间序列要预测时,我使用了 Hyndman 教授的方法。但是当我有大量的 ts 时它相当慢。 现在我正在尝试使用 apply() 函数,如下所示 librar
我听说过很多关于 pandas apply 很慢的说法,应该尽可能少用。 我这里有个情况: df = pd.DataFrame({'Date': ['2019-01-02', '2019-01-03'
在学习Javascript时,我尝试重新声明函数的apply属性。到目前为止没有问题。 function foo() { return 1; } alert(foo()); // 1 alert(fo
所以我正在做 learnRx http://reactive-extensions.github.io/learnrx/我有一个关于制作 mergeAll() 函数的问题(问题 10)。 这是我的答案
我是一名优秀的程序员,十分优秀!