[Python for Data Analysis]Chapter3 --Ipython

Ipython 历史

2001, Fernando Perez

优点

  1. execute-explore workflow instead of the typical edit-compile-run workflow
  2. tight integration with operating system’s shell and file system.
    Since much of data analysis involves exploration, trail and error, and iteration, Ipython will, in omst cases, help you get the job done faster.

新功能

  1. inline plotting

    
    %matplotlib inline
  2. a web-based interactive notebook(ipython notebook)
  3. a lightweight, fast parallel computing engine c

IPython Basics

普通python界面可以使用的IPython里面都可以使用

In [6]:
a = 5
In [7]:
a
Out[7]:
5

String Representation

通常比print更加整洁

In [8]:

import numpy
data = {i:numpy.random.randn() for i in range(7)}
In [9]:

data #string representation
Out[9]:
{0: -1.3550537839244003,
 1: 0.9366438785809313,
 2: -0.5206223861430287,
 3: -0.4677055985460479,
 4: 0.2493972618339726,
 5: -0.42102888827601476,
 6: 1.0239367134979809}
print data
{0: -1.3550537839244003, 1: 0.9366438785809313, 2: -0.5206223861430287, 3: -0.4677055985460479, 4: 0.2493972618339726, 5: -0.42102888827601476, 6: 1.0239367134979809}

Tab Completion

代码补全,和linux shell里面一样
1. Search for 变量
2. search for 对象的使用方法
3. search for 路径

anapple = 27
anexample = 42
# an<Tab> search for namespace
b = [1,2,3]
# b.<Tab> search for methods

Introspection

normal object

?b #object introspection

Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

function

#function
def add_number(a,b):
    '''
    Add two numbers together

    returns
    -------
    the_sum:type of arguments
    '''
    return a+b

? add_number

add_number?
Signature: add_number(a, b)
Docstring:
Add two numbers together

returns
-------
the_sum:type of arguments
File:      ~/python/pydata-book-master/<ipython-input-3-b6dcb4e5842d>
Type:      function

?? add_method(会显示源码)

Signature: add_number(a, b)
Source:
def add_number(a,b):
    '''
    Add two numbers together

    returns
    -------
    the_sum:type of arguments
    '''
    return a+b
File:      ~/python/pydata-book-master/<ipython-input-3-b6dcb4e5842d>
Type:      function

The %run Command

Any file can be run as a Python program inside the environment of your IPython session uing the %run command.
名字.py跑完之后, 所有变量包括结果, 都可以再IPthon中找到
用Ctrl-C结束

如果我们想要script使用IPython已经定义的变量,那么我们需要用%run -i, 而不是%run

Executing Code from the Clipboard

In practice it is very useful. 举个例子,如果你想检验一个script piece by piece, 那么可以分阶段检验。

  • %paste
  • %cpaste

Exceptions and Tracebacks

IPython will by default print a full call stack trace with a few lines of context around the position at each point in the stack.
以后还会用到 %xmode, %debug, %pdb after a error has occured for interactive post-mortem debugging.

run ch03/ipython_bug.py

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/home/sen/python/pydata-book-master/ch03/ipython_bug.py in <module>()
     13     throws_an_exception()
     14 
---> 15 calling_things()

/home/sen/python/pydata-book-master/ch03/ipython_bug.py in calling_things()
     11 def calling_things():
     12     works_fine()
---> 13     throws_an_exception()
     14 
     15 calling_things()

/home/sen/python/pydata-book-master/ch03/ipython_bug.py in throws_an_exception()
      7     a = 5
      8     b = 6
----> 9     assert(a + b == 10)
     10 
     11 def calling_things():

AssertionError: 

Magic Commands

prefix %
Magic can be viewed as Command Line Program run within the IPython system
Many of them have “command line” option.

例子 %timeit check the execution time of any python statement

In [18]:

import numpy as np
a = np.random.randn(100,100)
In [19]:

%timeit np.dot(a,a)
The slowest run took 924.59 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 84.7 µs per loop
In [20]:

timeit np.dot(a,a) #automagic
The slowest run took 16.44 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 84.2 µs per loop

? 和 ??

%reset? ?可以查询additional使用方法, 命令等

其他常用magic方法

  • %quickref 查magic的方法
  • %hist 查命令历史
  • %magic 详细magic方法
  • %reset 重置
  • %time, %timeit 看每次执行时间
  • %who, %whos, %whos #display variables defined in interactive namespace
  • %page b 打印b
  • %xdel b 删除b 也可以用reset删除

Qt-based Rich GUI Console

The Qt console can launch multiple IPython processes in tabs, enabling you to switch between tasks.

Matplotlib Integration and Pylab Mode

ipython --pylab
在ipython notebook中使用可以加%matplotlib inline即可

Using the Command History

  • Searching and reusing Command History
    Ctrl_R, Up-arrow
  • _ and __ are the previous 1 and 2 output; _in input for nth line, _n output for nth line
  • %logstart and %logoff
    用于记录命令

Interacting with the Operating Syetem

可以使用命令行而不用退出IPython

  • executing shell commands
  • changing directories
  • storing the results of a command in a Python object
CommandDescription
!cmdExecute cmd in the system shell
output = !cmd argsRun cmd and store the stdout in output
%alias alias_name cmdDefine an alias for a system (shell) command
%bookmarkUtilize IPython’s directory bookmarking system
%cd directoryChange system working directory to passed directory
%pwdReturn the current system working directory
%pushd directoryPlace current directory on stack and change to target directory
%popdChange to directory popped off the top of the stack
%dirsReturn a list containing the current directory stack
%dhistPrint the history of visited directories
%envReturn the system environment variables as a dict

assignment and intact with python

In [1]:

# !ls 
a = !ls #赋值
In [2]:

a
a
Out[2]:
['appendix_python.ipynb',
 'ch02',
 'CH02 Introductory Examples.ipynb',
 'ch02.ipynb',
 'ch03',
 'CH03--IPython.ipynb',
 'ch04.ipynb',
 'CH04 Numpy Basics -- Arrays and Vectorized Computation.ipynb',
 'ch05.ipynb',
 'ch06',
 'ch06.ipynb',
 'ch07',
 'ch07.ipynb',
 'ch08',
 'ch08.ipynb',
 'ch09',
 'ch09.ipynb',
 'ch10.ipynb',
 'ch11',
 'ch11.ipynb',
 'ch12.ipynb',
 'ch13',
 'fec_study.ipynb',
 'hello_world.py',
 'ipython_script_test.py',
 'PythonBasic2.ipynb',
 'PythonBasic3--Functions.ipynb',
 'PythonBasic.ipynb',
 'README.md']

pass value to shell

In [22]:

b = a[0]
!ls $b
appendix_python.ipynb

%alias

In [17]:

%alias ll ls -l
In [20]:

ll /usr
total 140
drwxr-xr-x   2 root root 61440 Feb 14 17:35 bin
drwxr-xr-x   2 root root  4096 Jan 12 16:04 games
drwxr-xr-x  43 root root  4096 Jan 17 13:08 include
drwxr-xr-x 173 root root 20480 Feb 14 17:35 lib
drwxr-xr-x  10 root root  4096 Apr 22  2015 local
drwx------   2 root root 16384 Jun 15  2015 lost+found
drwxr-xr-x   2 root root 12288 Feb 14 17:34 sbin
drwxr-xr-x 324 root root 12288 Feb 14 17:34 share
drwxr-xr-x  10 root root  4096 Sep 30 16:15 src

%bookmark

In [23]:

%bookmark db /home/sen/python/pydata-book-master/
In [24]:

%bookmark?
In [25]:

%bookmark -l
Current bookmarks:
db -> /home/sen/python/pydata-book-master/
In [28]:

%cd db 
(bookmark:db) -> /home/sen/python/pydata-book-master/
/home/sen/python/pydata-book-master

Software Developmetnt Tools

IPython HTML Notebook

ipython notebook

Tips for Productive Code Development Using IPython

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值