Ho un array numpy a 4 dimensioni (x, y, z, tempo) e vorrei fare un numpy.polyfit
attraverso la dimensione temporale, a ciascuna coordinata x, y, z . Per esempio:Regressione lungo una dimensione in una matrice numpy
import numpy as np
n = 10 # size of my x,y,z dimensions
degree = 2 # degree of my polyfit
time_len = 5 # number of time samples
# Make some data
A = np.random.rand(n*n*n*time_len).reshape(n,n,n,time_len)
# An x vector to regress through evenly spaced samples
X = np.arange(time_len)
# A placeholder for the regressions
regressions = np.zeros(n*n*n*(degree+1)).reshape(n,n,n,degree+1)
# Loop over each index in the array (slow!)
for row in range(A.shape[0]) :
for col in range(A.shape[1]) :
for slice in range(A.shape[2]):
fit = np.polyfit(X, A[row,col,slice,:], degree)
regressions[row,col,slice] = fit
Mi piacerebbe arrivare alla matrice regressions
, senza dover passare attraverso tutto il looping. È possibile?
[Questa risposta] (http://stackoverflow.com/a/16315330/832621) fornisce un esempio di un problema simile al tuo ... –
@SaulloCastro sicuro - ma questa risposta non acquista ancora alcun vantaggio in termini di prestazioni su un normale loop Python, che è più leggibile IMO –