# Jupyter Notebook最实用的5个魔术命令

Jupyter Notebook是一个基于Web的交互式工具，机器学习和数据科学社区都频繁使用它。它们用于快速测试，作为报告工具，甚至是在线课程中非常复杂的学习材料。我最喜欢的一个功能是魔术命令。这些快捷方式可显着扩展Notebook的功能。以下是你应首先学习的五种命令列表，以便成为更好的数据向导。

### ％time，％timeit和%% time

```import numpy as np
from numpy.random import randint

# A function to simulate one million dice throws.
def one_million_dice():
return randint(low=1, high=7, size=1000000)

# Let's try %time first
%time throws = one_million_dice()
%time mean = np.mean(throws)

# Outputs:
# Wall time: 20.6 ms
# Wall time: 3.01 ms

# Let's do the same with %timeit
%timeit throws = one_million_dice()
%timeit mean = np.mean(throws)

# Outputs:
# 10 loops, best of 3: 22.2 ms per loop
# 100 loops, best of 3: 2.86 ms per loop

# And finally %%time
%%time
throws = one_million_dice()
mean = np.mean(throws)

# Outputs:
# Wall time: 36.6 ms```

### ％matplotlib

```from numpy.random import randint
import matplotlib.pyplot as plt

# Sample 1000 random values to create a scatterplot
x = randint(low=1, high=1000, size=100)
y = randint(low=1, high=1000, size=100)

# This will show nothing in a Jupyter Notebook
plt.scatter(x, y)
plt.show()

# Let the magic happen!
%matplotlib inline
plt.scatter(x, y)
plt.show()```

```# load the autoreload extension
# Set extension to reload modules every time before executing code

from helperfunctions import complicated_function_to_return_a_number

complicated_function_to_return_a_number()
# Output: 123

# Go to helperfunctions.py and change something withing the function

complicated_function_to_return_a_number()
# Output: 321```

### %system

```# Easy to read version
%system date

# Shorthand with "!!" instead of "%system" works equally well
!!date```

### ％who_ls

```# Outputs a list of all interactive variables in your environment
%who_ls

# Reduces the output to interactive variables of type "function"
%who_ls function```

1835 篇文章92 人订阅

0 条评论

## 相关文章

10620

27850

35570

### 帝国cms调用栏目自定义字段(栏目简介)如何操作

开源的cms就像一个操作系统，可以满足大部分人的需求，如果你想增加一些特殊的功能，那就二次开发呗，就像APP一样。帝国cms默认的栏目是没有调用栏目自定义字...

62180

### 【编程技巧】提高程序员技能的11招

1.清晰的分析问题 2.三思而后行如何解决这个问题 3.收集完整的需求。 花点时间，想好产品的目标形态和最终的用户群。在这个阶段思路清晰会给以后节省很多时间。 ...

35870

387110

20820

8030

22460

44880