ipython jupyter notebook,快速掌握数据分析必备工具,ipython和jupyter notebook

原标题:快速掌握数据分析必备工具,ipython和jupyter notebook

来源:麦叔编程

作者:麦叔

37a0ad088a2a03cf793b0346b74f5753.png

本文帮你快速掌握数据分析师必须会用的两个工具 - ipython和jupyter notebook。

既然有了Python,为什么还要ipython?麦叔不用说话,给你一张图你就明白啦。jupyter notebook又是什么鬼?

建议把本文放到收藏夹。吃灰也好过需要的时候找不到。

一、iPython

iPython是Interactive Python,它是基于Python的一个包装。它其实就是一个可以通过pip安装的包。提供了普通python之外的一些功能,其中一个功能就是可以显示图片。

b9aa9020a7c1f49c22591c7dda02d4ea.png

iPython在数据分析师,数据科学家,人工智能科学中经常使用。

(1)安装

python -m pip install ipython

(2)使用

ipython就是Python,使用方法和使用普通的交互式Python一样,代码也一样。只不过输出显示上有一定优化。

进入ipython:

zjueman@maishu data_analysis % ipython

Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)

Type 'copyright', 'credits' or 'license' for more information

IPython 7.21.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

2.使用ipython:为了运行一下代码,请先安装numpy:

python -m pip install numpy

In [1]: a = 5

In [2]: b = "麦叔"

In [3]: import numpy as np

In [4]: data = {i:np.random.randn() for i in range(7)}

In [5]: data

Out[5]:

{0: 0.8738401705018338,

1: 0.7173530856483666,

2: 1.269301701227684,

3: -0.6322949353286054,

4: -2.3619895093818295,

5: -0.9031446928993554,

6: -0.07942775508126601}

3.问号寻求帮助:

In [4]: name = 'maishu'

In [5]: name?

Type: str

String form: maishu

Length: 6

Docstring:

str(object='') -> str

str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or

errors is specified, then the object must expose a data buffer

that will be decoded using the given encoding and error handler.

Otherwise, returns the result of object.__str__() (if defined)

or repr(object).

encoding defaults to sys.getdefaultencoding().

errors defaults to 'strict'.

4.退出

In [10]: quit()

zjueman@maishu data_analysis %

5.画图 为了运行一下代码需要先安装matplotlib

python -m pip install matplotlib

In [1]: import numpy as np

In [2]: %matplotlib

Using matplotlib backend: MacOSX

In [3]: import matplotlib.pyplot as plt

In [4]: plt.plot(np.random.randn(50).cumsum())

Out[4]: []

b58edf5acbed65f564c2c0228d1b47fd.png

二、jupyter notebook

数据科学家们觉得ipython还不够过瘾,又在ipython基础上开发了jupyter notebook:一个基于网页的写代码界面。

3e51af140da91153ecd94ec9211c4c78.png

jupyter是基于ipython的,很多操作几乎都一样。但是它有很多独特优点:

(1)文件可以保存为ipynb的文件

(2)在线编写代码

(3)支持多人协作

(4)支持markdown格式的文档

1. 安装

python -m pip install jupyter

2. 启动

> jupyter notebook

这个命令会在本机的8888端口上运行一个网站,并自动打开浏览器:

http://localhost:8888/tree

6c593a0fffaf5c242c8ef9da41130758.png

3. 基本使用

(1)创建文件

cdafa9f94073931b20972734adc2a31b.png

(2)编写和运行代码

393d4aabbe9d6fc6406d6d2e0d0b77e1.png

(3)保存和修改文件名

07430ffd272e9b936075d513efc25c79.png

文件保存在你打开jupyter notebook的目录下:

文件的格式是ipynb

35ddb360d041ec750926cdc7ef798c4d.png

4. Tab补全

在notebook中打代码的过程中,按Tab键可以自动提示和补全,类似于Pycharm和VSCode等IDE的功能:

9d188348af6e63bb343dbe61b4ce8ec3.png

它可支持:

(1)自动补全变量名

(2)自动补全函数名

(3)自动补全文件名等

5. 集成matplotlib画图

edf6877f22556fe463c952c847fa7111.png

6. 魔术命令

(1)运行脚本:%run

(2)打印命令输入历史:%hist

(3)运行效率:%timeit

(4)其他魔术命令

07800f0dd4057b457a05e3b7454f2c04.png

7. 快捷键

(1)停止执行:Ctrl+C

(2)其他ipython快捷键

责任编辑:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值