我在下面有一个来自大型数据集的示例数据,其中每个参与者都有多个评分条件。
Participant<-c("p1","p1","p2","p2","p3","p3")
Condition<-c( "c1","c2","c1","c2","c1","c2")
Score<-c(4,5, 5,7,8,2)
T<-data.frame(Participant, Condition, Score)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 K 均值聚类将参与者分成不同的组,考虑到条件不是数字,有什么好的方法吗?
谢谢!
我尝试使用计算列表的平均值Parallel.For()
.我决定反对它,因为它比简单的串行版本慢大约四倍.然而我很感兴趣的是它并没有产生与序列结果完全相同的结果,我认为了解原因是有益的.
我的代码是:
public static double Mean(this IList<double> list)
{
double sum = 0.0;
Parallel.For(0, list.Count, i => {
double initialSum;
double incrementedSum;
SpinWait spinWait = new SpinWait();
// Try incrementing the sum until the loop finds the initial sum unchanged so that it can safely replace it with the incremented one.
while (true) {
initialSum = sum;
incrementedSum = initialSum + list[i];
if (initialSum == Interlocked.CompareExchange(ref sum, incrementedSum, initialSum)) break;
spinWait.SpinOnce();
}
});
return sum / list.Count;
} …
Run Code Online (Sandbox Code Playgroud) 从在线搜索和在该组中搜索,这似乎应该有效:
> mean(r_lab$ozone, na.rm=TRUE)
Run Code Online (Sandbox Code Playgroud)
但是,我得到的是:
[1] NA
Warning message:
In mean.default(r_lab$ozone, na.rm = TRUE) :
argument is not numeric or logical: returning NA
Run Code Online (Sandbox Code Playgroud)
这是数据集中该列的内容:
> r_lab$Ozone
[1] 41 36 12 18 NA 28 23 19 8 NA 7 16 11 14
[15] 18 14 34 6 30 11 1 11 4 32 NA NA NA 23
Run Code Online (Sandbox Code Playgroud)
我有点慌
我有一个 numpy csr 矩阵,我想得到它的平均值,但它包含很多零,因为我消除了主对角线上及其下方的所有值,仅取上三角形值,现在转换时我的 csr 矩阵数组看起来像这样:
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.63646664 0.34827262
0.24316454 0.1362165 0.63646664 0.15762204 0.31692202 0.12114576
0.35917146
Run Code Online (Sandbox Code Playgroud)
据我所知,为了让 csr 矩阵工作并显示如下内容,零点很重要:
(0,5) 0.5790418
(3,10) 0.578210
(5,20) 0.912370
(67,5) 0.1093109
Run Code Online (Sandbox Code Playgroud)
我看到 csr 矩阵有它自己的均值函数,但是这个均值函数是否考虑了所有零,因此除以数组中包括零的元素数量?因为我只需要非零值的平均值。我的矩阵包含多个向量之间的相似性,更像是一个矩阵列表:
[[ 0. 0.63646664 0.48492084 0.42134077 0.14366401 0.10909745
0.06172853 0.08116201 0.19100626 0.14517247 0.23814955 0.1899649
0.20181049 0.25663533 0.21003358 0.10436352 0.2038447 1.
0.63646664 0.34827262 0.24316454 0.1362165 0.63646664 0.15762204
0.31692202 0.12114576 0.35917146]
[ 0. 0. …
Run Code Online (Sandbox Code Playgroud) 如何在数据中添加一列 price.wk.average 使 price.wk.average 等于上周的平均价格,并在数据中添加一列 price.mo.average 使其等于平均值上个月的价格?price.wk.average 将在整个星期内保持不变。
Dates Price Demand Price.wk.average Price.mo.average
2010-1-1 x x
2010-1-2 x x
......
2015-1-1 x x
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一种方法来计算List
Scala中给定元素的平均值。这是我的代码:
def meanElements(list: List[Float]): Float = {
list match {
case x :: tail => (x + meanElements(tail))/(list.length)
case Nil => 0
}
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时meanElements(List(10,12,14)))
,我得到的结果与 12 不同。有人可以帮忙吗?
我有一个日期框架(“daten”),其中大多数列都是数值。它们的范围通常从 0 到 5。但是,它们也可以取值 99。我想计算列的平均值,仅排除值 99。
例如:
> mean(c(0, 1, 2, 3, 4, 5, 99))
[1] 16.28571
Run Code Online (Sandbox Code Playgroud)
不是我需要的,而是我希望它被计算为好像向量是
> mean(c(0, 1, 2, 3, 4, 5))
[1] 2.5
Run Code Online (Sandbox Code Playgroud)
,给我我正在寻找的意思。
有一个类似的问题(通过排除任何给定数字来计算平均值,中位数),但该解决方案对我不起作用。然而,我想,一旦我可以排除任何列中的某个值,我就可以简单地将它与 组合apply
,所以我实际上正在寻找一种方法来计算某个向量的平均值,但忽略了某些值。
我正在制作MEAN Stack应用,但出现此错误:
TypeError: Cannot read property 'indexOf' of undefined.
Run Code Online (Sandbox Code Playgroud)
问题似乎在这一行:
<div *ngIf="newComment.indexOf(product._id) > -1">
Run Code Online (Sandbox Code Playgroud)
我将newComment定义为组件中的变量,如下所示:
newComment = [];`
Run Code Online (Sandbox Code Playgroud)
所以应该定义它。有谁可以帮忙吗?
编辑
根据要求,enire文件。
product.component.html(以div结尾)
<h1 class="pb-2 mt-4 mb-2 border-bottom">Producten</h1>
<div class="row show-hide-message" *ngIf="message && newProduct">
<div [ngClass]="messageClass">
{{ message }}
</div>
</div>
<button type="button" name="button" class="btn btn-warning" *ngIf="!newProduct" (click)="newProductForm()">Nieuwe productvraag</button>
<button [disabled]="loading" type="button" name="button" class="btn btn-default" *ngIf="!newProduct" (click)="reload()"><i class="fas fa-sync-alt"></i> Reload</button>
<div><br></div>
<form [formGroup]="form" name="productForm" (submit)="onProductSubmit()" *ngIf="newProduct">
<div class="form-group">
<label for="title">Titel</label>
<div>
<input type="text" name="title" class="form-control" placeholder="*Product Titel" autocomplete="off" formControlName="title" />
<ul class="help-block …
Run Code Online (Sandbox Code Playgroud) 我有一个清单:
first = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
Run Code Online (Sandbox Code Playgroud)
我想要另一个具有三个值的平均值的列表,因此新列表是:
new = [2,5,8,11,14,17]
Run Code Online (Sandbox Code Playgroud)
新列表中将只有 6 个值,因为第一个列表中只有 18 个元素。
我正在寻找一种优雅的方法来执行此操作,并且对大列表使用最少的步骤。
我有一个包含多行和多列的 Pandas 数据框,其中每个单元格都包含一个值列表。我想分别计算每行的平均值(在列表中)。
数据框看起来像这样:
l1 = [[1,2,4,3],[1,2,4,3], [1,2,4,3]]
l2 = [[8,2,6,4],[1,2,4,3],[1,2,4,3]]
l3 = [[1,2,4,9],[1,2,4,3],[1,2,4,3]]
df = pd.DataFrame([l1, l2, l3], columns=list('xyz'))
Run Code Online (Sandbox Code Playgroud)
df:
x y z ...
x [1,2,4,3] [1,2,4,3] [1,2,4,3]
y [8,2,6,4] [1,2,4,3] [1,2,4,3]
z [1,2,4,9] [1,2,4,3] [1,2,4,3]
Run Code Online (Sandbox Code Playgroud)
我想要这样的结果:
x y z MEAN
x [1,2,4,3] [1,2,4,3] [1,2,4,3] 2.50000
y [8,2,6,4] [1,2,4,3] [1,2,4,3] 3.33333
z [1,2,4,9] [1,2,4,3] [1,2,4,3] 3.00000
Run Code Online (Sandbox Code Playgroud)
Any suggestions?