- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
Modules
In Pythoh, any .py
file is a module in that it can be imported. Because the interpreter runs the entrie file when a moudle is imported, it is traditional to use a guard to ignore code that should only run when the file is executed as a script.
%%file foo.py """ When this file is imported with `import foo`, only `useful_func1()` and `useful_func()` are loaded, and the test code `assert ...` is ignored. However, when we run foo.py as a script `python foo.py`, then the two assert statements are run. Most commonly, the code under `if __naem__ == '__main__':` consists of simple examples or test cases for the functions defined in the moule. """ def useful_func1(): pass def useful_fucn2(): pass if __name__ == '__main__': assert(useful_func1() is None) assert(useful_fucn2() is None)
Overwriting foo.py
Organization of files in a module
When the number of files you write grow large, you will probably want to orgnize them into their own directory structure. To make a folder a module, you just need to include a file named __init__.py
in the folder. This file can be empty. For example, here is a module named pkg
with sub-modules sub1
and sub2
.
./pkg: __init__.py foo.py sub1 sub2 ./pkg/sub1: __init__.py more_sub1_stuff.py sub1_stuff.py ./pkg/sub2: __init__.py sub2_stuff.py
import pkg.foo as foo
foo.f1()
1
pkg.foo.f1()
1
How to import a module at the same level
Within a package, we need to use absolute path names for importing other modules in the same directory. This prevents confusion as to whether you want to import a system moudle with the same name. For example, foo.sub1.more_sub1_stuff.py
imports functions from foo.sub1.sub1_stuff.py
! cat pkg/sub1/more_sub1_stuff.py
from pkg.sub1.sub1_stuff import g1, g2 def g3(): return 'g3 uses %s, %s' % (g1(), g2())
from pkg.sub1.more_sub1_stuff import g3 g3()
'g3 uses g1, g2'
How to import a moudle at a different level
Again, just use absolute paths. For example, sub2_stuff.py
in the sub2
directory uses functions from sub1_stuff.py
in the sub1
directory:
! cat pkg/sub2/sub2_stuff.py
from pkg.sub1.sub1_stuff import g1, g2 def h1(): return g1() def h2(): return g1() + g2()
from pkg.sub2.sub2_stuff import h2 h2()
'g1g2'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论