求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   Code  
会员   
要资料
 
 

利用python进行数据分析
搭建python环境
统计 1880-2010 年间 全美婴儿姓名的趋势
Ipython & Ipython Notebook
NumPy Basics: Arrays and Vectorized Computation
Pandas(Python Data Analysis Library)
数据加载、储存与文件格式
绘图和可视化(Matplotlib)
时间序列
经济,金融数据应用
补充例子
国王与囚徒
利用python进行科学计算
分形与混沌之-Mandelbrot集合
分形与混沌之-迭代函数系统(IFS)
分形与混沌之-蔡氏电路模拟
对于μ子实验数据进行快速处理与分析
37%法则 - "非诚勿扰" 中的博弈
关于时间/日期的转换
深入研究
一切从游戏开始:完整的一个 python to hack 实例!
习题:扑克牌发牌
 
 

Ipython & Ipython Notebook
316 次浏览
5次  
 捐助

Ipython & Ipython Notebook

介绍一下一个增强的交互式编译平台Ipython和Ipython Notebook.

一个相关的介绍1203_ipython_pycon.pdf

Ipython Basics

Ipython的可读性更佳(pretty printed)

In [542]: data = {i : randn() for i in range(7)}
In [543]: data
Out[543]:
{0: 0.6900018528091594,
1: 1.0015434424937888,
2: -0.5030873913603446,
3: -0.6222742250596455,
4: -0.9211686080130108,
5: -0.726213492660829,
6: 0.2228955458351768}

#和传统的shell里面相比

>>> from numpy.random import randn
>>> data = {i : randn() for i in range(7)}
>>> print data
{0: -1.5948255432744511, 1: 0.10569006472787983, 2: 1.972367135977295,
3: 0.15455217573074576, 4: -0.24058577449429575, 5: -1.2904897053651216,
6: 0.3308507317325902}

自动补全(Tab Completion)

Ipython比python自带shell多了自动补全功能 ,用Tab实现:

In [1]: an_apple = 27
In [2]: an_example = 42
In [3]: an<Tab>

an_apple and an_example any

In [3]: b = [1, 2, 3]
In [4]: b.<Tab>
b.append b.extend b.insert b.remove b.sort
b.count b.index b.pop b.reverse

自动补全功能方便我们写重复的代码

%run命令

我们能在Ipython里跑一个python文件

比如有一个ipython_script_test.py文件

def f(x, y, z):
return (x + y) / z
a = 5
b = 6
c = 7.5
result = f(a, b, c)

In [550]: %run ipython_script_test.py #运行
In [551]: c
Out[551]: 7.5
In [552]: result
Out[552]: 1.4666666666666666

魔术命令 (Magic Command)

Ipython里特有的一些命令,以%开头作为标志

保留格式的粘帖 %cpaste

In [7]: %cpaste # 有时候要复制一大段代码到shell里而直接粘帖会破坏换行格式,这时%cpaste就派用场了。
Pasting code; enter '--' alone on the line to stop or use Ctrl-D. #右键,粘帖
:x = 5
:y = 7
:if x > 5:
: x += 1
:
: y = 8
:-- # 结束的时候打‘--’

计时 %timeit

In [554]: a = np.random.randn(100, 100)
In [555]: %timeit np.dot(a, a)
10000 loops, best of 3: 69.1 us per loop

重置 %reset

In [1]: %reset? # ?是内省(Introspection)功能,可以看到reset的细节
Resets the namespace by removing all names defined by the user.
Parameters
----------
-f : force reset without asking for confirmation. # 两种不同的reset
-s : 'Soft' reset: Only clears your namespace, leaving history intact.
References to objects may be kept. By default (without this option),
we do a 'hard' reset, giving you a new session and removing all
references to objects from the current session.
Examples
--------
In [6]: a = 1
In [7]: a
Out[7]: 1
In [8]: 'a' in _ip.user_ns
Out[8]: True
In [9]: %reset -f
In [1]: 'a' in _ip.user_ns
Out[1]: False

总结一些Magic Command,有待大家自己去尝试

%quickref Display the IPython Quick Reference Card
%magic Display detailed documentation for all of the available magic commands
%debug Enter the interactive debugger at the bottom of the last exception traceback
%hist Print command input (and optionally output) history
%pdb Automatically enter debugger after any exception
%paste Execute pre-formatted Python code from clipboard
%cpaste Open a special prompt for manually pasting Python code to be executed
%reset Delete all variables / names defined in interactive namespace
%page OBJECT Pretty print the object and display it through a pager
%run script.py Run a Python script inside IPython
%prun statement Execute statement with cProfile and report the profiler output
%time statement Report the execution time of single statement
%timeit statement Run a statement multiple times to compute an emsemble average execution time. Useful for timing code with very short execution time
%who, %who_ls, %whos Display variables defined in interactive namespace, with varying levels of information / verbosity
%xdel variable Delete a variable and attempt to clear any references to the object in the IPython internals

Ipython Notebook

Notebook 为将代码执行与实时计算文档的创建组合起来的交互式计算提供了工作环境。这些 Notebook 文件可以包含任意文本、数学公式、输入代码、结果、图形、视频以及新型 Web 浏览器能够显示的任何其他种类的媒体。

个人体会: 可以修改已经执行过得语句,重新的到结果(像Mathematica),这让修改变得很方便。

参考网站 http://ipython.org/notebook.html

启动本地notebook

cmd中输ipython notebook


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



316 次浏览
5次
 捐助