我有一个二维数组,我想计算考虑每一行的每列之间的平均距离。
例如,如果我有2D数组:
import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { ExampleService } from "../example.service";
import "rxjs/add/operator/switchMap";
@Component({
templateUrl: "./example.component.html",
styleUrls: ["./example.component.scss"]
})
export class ExampleComponent implements OnInit {
constructor(
private exampleService: ExampleService
) { }
ngOnInit() {
var selections = new Subject<string>();
var appointments = selections
// exampleService.getData returns an HTTP observable.
.switchMap(date => this.exampleService.getData(date));
var group = appointments
.map(data => this.process(data));
var times = appointments
.map(data => this.calculateTimes(data));
// Calling subscribe each time sends the HTTP request multiple
// times - I only want it to be send once for both of them: they
// can share the data!!
group.subscribe();
times.subscribe();
// selections.next(someData) is called periodically from some
// omitted code.
}
processExample(data: string[]) {
/*
* Some processing code.
*/
return data;
}
calculateTimes(data: string[]) {
/*
* Some processing code.
*/
return data;
}
}
我想计算所有行中第1列和第2列之间的平均距离,所有行中的第1列和第3列之间的平均距离,以及所有行中的第2行和第3列之间的平均距离。
第1列和第2列的平均距离为PROGRAM MAIN
VAR
systime:GETSYSTEMTIME;
fbSystemTime : GETSYSTEMTIME;
timeAsFileTime : T_FILETIME;
timeAsDT : DT;
END_VAR
fbSystemTime(
timeLoDW =>timeAsFileTime.dwLowDateTime,
timeHiDW =>timeAsFileTime.dwHighDateTime
);
timeAsDT := FILETIME_TO_DT( timeAsFileTime );
,等于2。
是否有一个numpy函数可以实现这个目标?
答案 0 :(得分:3)
这样的东西?
*
输出:
import numpy as np
x = np.array([[2,2,3],[4,2,5],[1,5,2]])
def calc(cols):
return np.mean(np.abs(np.diff(x[:, cols])))
print(calc([0,1]))
还要考虑:
2.0
输出:
import itertools
print(list(itertools.combinations(range(x.shape[1]), 2))) # outer list because using py3
答案 1 :(得分:1)
我建议改为:
from scipy.spatial.distance import pdist
m, n = in_arr.shape
pdist(in_arr.T, 'cityblock') / m
Out: array([ 2. , 1. , 2.33333333])
如果您想知道哪一个距离与哪一对相关,请使用:
np.stack(np.triu_indices(n, 1))
Out:
array([[0, 0, 1],
[1, 2, 2]], dtype=int32)
这应该比使用for
循环或itertools