Convert a R code into Python script
I got the following R code and I need to convert it to python and run it in python environment, basically I have done this with rpy2 module, but it looks kind of dull with python doing the same things, so could someone find a better way to rewrite the following R code to an equivalent python script with the rpy2 module?
mymad <- function (x)
{
center <- median(x)
y <- abs(x - center)
n <- length(y)
if (n == 0)
return(NA)
half <- (n + 1)/2
1.48开发者_如何学运维26 * if (n%%2 == 1) {
sort(y, partial = half)[half]
}
else {
sum(sort(y, partial = c(half, half + 1))[c(half, half +
1)])/2
}
}
You could have stated the purpose of your function, which is Median Absolute Deviation. What you call mymad
is an approximation of the standard deviation of the population, based on the assumption of large samples of normally distributed variables.
According to this website:
def median(pool):
copy = sorted(pool)
size = len(copy)
if size % 2 == 1:
return copy[(size - 1) / 2]
else:
return (copy[size/2 - 1] + copy[size/2]) / 2
So, you want a function mad
which would verify :
mad(x) == median(abs(x-median(x)))
Thanks to Elenaher (give his comment credits), here is the code:
def mad(x):
return median([abs(val-median(x)) for val in x])
And then, I believe your are computing:
def mymad(x):
return 1.4826*mad(x)
Probably a little slower than a numpy/Python written one, but certainly faster to implement (as no wheel gets reinvented):
# requires rpy2 >= 2.1
from rpy2.robjects.packages import importr
stats = importr('stats')
stats.mad(x)
import numpy
# x is the input array
x = numpy.array( [1,2,4,3,1,6,7,5,4,6,7], float ) }
# mad = median( | x - median(x) | )
mad = numpy.median( numpy.abs( ( x - numpy.median( x ) ) )
精彩评论