Revised with 1.6.0
This commit is contained in:
parent
e367d2ed21
commit
dd705eb0a6
500
Data_Science/11__From_other_languages.ipynb
Normal file
500
Data_Science/11__From_other_languages.ipynb
Normal file
@ -0,0 +1,500 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Using other languages\n",
|
||||
"Often, I hear that the biggest challenge of moving from another language to Julia is giving up all the codes you have written in other languages or your favorite packages from other languages. **This notebook is not about data science, but it's about your next data science project** (if you're working on a data science project in Julia and you want to use functionality from other langages). Here, we will specifically cover Python, R, and C."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### ⚫Python"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"using PyCall"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You can import any python package..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.7071067811865475"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"math = pyimport(\"math\")\n",
|
||||
"math.sin(math.pi / 4) # returns ≈ 1/√2 = 0.70710678..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Hint: you may need to do:\n",
|
||||
"```\n",
|
||||
"julia> using Conda\n",
|
||||
"julia> Conda.add(\"networkx\")\n",
|
||||
"```\n",
|
||||
"for the line below to work."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"PyObject <module 'networkx' from '/opt/conda/lib/python3.9/site-packages/networkx/__init__.py'>"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"python_networkx = pyimport(\"networkx\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You can also write your own Python code as follows"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"py\"\"\"\n",
|
||||
"import numpy\n",
|
||||
"def find_best_fit_python(xvals,yvals):\n",
|
||||
" meanx = numpy.mean(xvals)\n",
|
||||
" meany = numpy.mean(yvals)\n",
|
||||
" stdx = numpy.std(xvals)\n",
|
||||
" stdy = numpy.std(yvals)\n",
|
||||
" r = numpy.corrcoef(xvals,yvals)[0][1]\n",
|
||||
" a = r*stdy/stdx\n",
|
||||
" b = meany - a*meanx\n",
|
||||
" return a,b\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(0.9590099079443583, 3.3945823140627924)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"xvals = repeat(1:0.5:10, inner=2)\n",
|
||||
"yvals = 3 .+ xvals .+ 2 .* rand(length(xvals)) .-1\n",
|
||||
"find_best_fit_python = py\"find_best_fit_python\"\n",
|
||||
"a,b = find_best_fit_python(xvals,yvals)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If the above python code was in a file called `fit_linear.py`, you can call it as follows:\n",
|
||||
"```\n",
|
||||
"python_linear_fit = pyimport(\"fit_linear\") \n",
|
||||
"python_linear_fit.find_best_fit_python(xvals,yvals)```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### ⚫R code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"using RCall"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"`$` can switch to an `R` REPL from julia's REPL. We'll take a look..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RObject{RealSxp}\n",
|
||||
"[1] 11\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# we can use the rcall function\n",
|
||||
"r = rcall(:sum, Float64[1.0, 4.0, 6.0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Float64"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"typeof(r[1])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The `@rput` allows you to put julia variable in the `R` context."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"z = 1\n",
|
||||
"@rput z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RObject{IntSxp}\n",
|
||||
"[1] 2\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"r = R\"z+z\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"r[1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"10-element Vector{Float64}:\n",
|
||||
" -0.8375683420545423\n",
|
||||
" 0.7936207892163949\n",
|
||||
" -0.9384437083801473\n",
|
||||
" 1.3499189652001669\n",
|
||||
" 0.9703398099979935\n",
|
||||
" -0.6405138798150218\n",
|
||||
" -1.5560041013786536\n",
|
||||
" -0.5666575019004383\n",
|
||||
" -0.21398352496411505\n",
|
||||
" 2.059761076234788"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = randn(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You can apply R functions on julia variables"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RObject{IntSxp}\n",
|
||||
"[1] 6\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"@rimport base as rbase\n",
|
||||
"rbase.sum([1, 2, 3])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Hint: for the code below to work, you will need to type `$` in the REPL followed by:\n",
|
||||
"```\n",
|
||||
"install.packages(\"boot\")\n",
|
||||
"```\n",
|
||||
"the `$` will enter you into the R REPL mode."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RObject{VecSxp}\n",
|
||||
"\n",
|
||||
"\tOne Sample t-test\n",
|
||||
"\n",
|
||||
"data: `#JL`$x\n",
|
||||
"t = 0.11328, df = 9, p-value = 0.9123\n",
|
||||
"alternative hypothesis: true mean is not equal to 0\n",
|
||||
"95 percent confidence interval:\n",
|
||||
" -0.7975999 0.8816938\n",
|
||||
"sample estimates:\n",
|
||||
" mean of x \n",
|
||||
"0.04204696 \n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"R\"t.test($x)\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The equivalent in Julia would be"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"One sample t-test\n",
|
||||
"-----------------\n",
|
||||
"Population details:\n",
|
||||
" parameter of interest: Mean\n",
|
||||
" value under h_0: 0\n",
|
||||
" point estimate: 0.042047\n",
|
||||
" 95% confidence interval: (-0.7976, 0.8817)\n",
|
||||
"\n",
|
||||
"Test summary:\n",
|
||||
" outcome with 95% confidence: fail to reject h_0\n",
|
||||
" two-sided p-value: 0.9123\n",
|
||||
"\n",
|
||||
"Details:\n",
|
||||
" number of observations: 10\n",
|
||||
" t-statistic: 0.11328193986719481\n",
|
||||
" degrees of freedom: 9\n",
|
||||
" empirical standard error: 0.3711708880068253\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"using HypothesisTests\n",
|
||||
"OneSampleTTest(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### ⚫C code\n",
|
||||
"Calling standard libraries is easy"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"57583336"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"t = ccall(:clock, Int32, ())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Can look at Python and C/C++ examples here: https://github.com/xorJane/Excelling-at-Julia-Basics-and-Beyond/blob/master/JuliaCon2019_Huda/Julia%20Wrappers.ipynb\n",
|
||||
"```\n",
|
||||
"ccall((:hello_world_repeated,\"hello_world_lib.dylib\"),\n",
|
||||
" Int64,\n",
|
||||
" (Int64,),\n",
|
||||
" 10)\n",
|
||||
" ```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Finally**, I would say that this is the only off-topic notebook in this course, and it's a topic that can be covered on its own in a standalone tutorial... Nevertheless, the goal of this notebook is to tell you that porting your code from Python, R, and C should be easy and straight forward in Julia. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 🥳 One cool finding\n",
|
||||
"\n",
|
||||
"You can easily call Python, R, C, and Cpp code from Julia!"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.6.0",
|
||||
"language": "julia",
|
||||
"name": "julia-1.6"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.6.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user