Recently I am working on a data mining project and investigating on different visualization and computation tools to help to analyze the data model. Both Octave and R with R Studio interest me.
I am eager to try out both tools and come across an interesting article about using Monte Carlo simulation to find out the pi. I know this is not exactly data mining topic. However, it is still interesting.
Here is the article by Niall O'Higgins.
http://niallohiggins.com/2007/07/05/monte-carlo-simulation-in-python-1/
The author uses python to demonstrate the concept. After learning R will couple hours, I rewrote code in R.
n <- 1000000
x <- runif(n, 0.0, 1.0)
y <- runif(n, 0.0, 1.0)
score <- ifelse((sqrt(x^2 + y^2) <=1.0), 1, 0)
hit <- sum(score)
myPi <- 4 * hit / n
I am surprisingly the result code can be pretty compact. R seems to have a better data import/export capability than Octave. I am very impressed with both Octave and R can do.
No comments:
Post a Comment