Reputation: 19164
I have a numpy array. I want to resample that numpy array to a specific value say 10.
X = [[ 6.99749994 17.76250029 5.01699996]
[ 10.5150001 18.28000021 4.06300002]
[ 12.47374988 19.4937501 6.93949986]
[ 15.38050032 21.92675018 6.68924999]
[ 17.19525003 19.25349998 7.71924984]
[ 15.75849962 17.17449951 5.07899988]
[ 16.83874989 19.46924973 2.56125003]
[ 20.24999952 19.40649986 3.77824998]
[ 20.32649994 15.83099985 3.59350002]
[ 19.17724943 15.48849988 0.23099999]
[ 21.44624996 18.01575041 -0.98599999]
[ 24.13700008 16.26849985 0.35250001]
[ 23.45549965 13.07250023 -0.88625002]
[ 22.66449976 14.09524989 -4.22149998]
[ 25.57133357 15.34866651 -4.50566673]
[ 27.54475021 12.71549988 -4.02225 ]
[ 25.7732501 11.273 -6.82424998]
[ 26.65899976 13.21299966 -9.15133333]
[ 27.97424984 12.19199991 -12.32075 ]
[ 26.63675022 8.88499999 -11.40549994]]
Right now my numpy array in 3x20 dimensions. I want to resample to 3x10. How can I do that using Biopython, numpy or scipy?
this is resampling function which I need to convert to python. pastebin.com/JsGeNyLp where input is numpy array and N is any integer value e.g.10
Upvotes: 0
Views: 747
Reputation: 21914
While the question is a bit vague, if all you're looking to do is randomly select some number of elements from a numpy
array at random, python has a convenient random
module that will let you do just that:
import random
resampled_X = random.random_sample(X, 10)
Looking at your linked MATLAB code, it looks like you might be looking for some 1d interpolation in addition to this. Again, hard to say exactly without more details in your question, but scipy
in fact has an interp1d
function just like MATLAB. You would use it like so:
from scipy.interpolate import interp1d
x = range(30)
y = [i**2 for i in x]
# quadratic can be replaced with linear, cubic, or just a number for polynomial degree
new_function = interp1d(x, y, 'quadratic')
new_function(1.5)
>>> 2.25
After reading through your code I can offer the following translation of it into numpy
and scipy
code, but it's an extremely unintuitive definition for "resampling"
import numpy as np
from scipy.interpolate import intep1d
def resample(X, N):
norms = [0] + [np.linalg.norm(X[:,i] - X[:,i-1]) for i in range(1,X.shape[1])]
cumdel = np.cumsum(norms)/sum(norms)
solution_space = np.linspace(0,1, N)
new_function = lambda i: interp1d(cumdel, X[i,:])
return np.array([new_function(i)(solution_space) for i in range(X.shape[0])]).T
Basically what it looks like this code is doing is this:
n
added (return line)Kind of a weird thing to be doing, but that's what it's doing. Let me know if you have any more problems.
Upvotes: 4