Jupyter

Jupyter Notebook 是一个基于 Web 的交互式计算环境,让你可以在浏览器中编写和运行代码、创建可视化、添加说明文字,非常适合数据分析、机器学习和教学。

使用 pip 安装

1
2
3
4
5
6
7
8
# 确保已安装 Python 3.7+
python --version

# 安装 Jupyter
pip install notebook

# 或者安装新一代的 JupyterLab
pip install jupyterlab

启动 Jupyter Notebook

1
2
3
4
5
6
7
8
# 基本启动
jupyter notebook

# 指定目录启动
jupyter notebook --notebook-dir=/path/to/your/folder

# 指定端口启动
jupyter notebook --port=9999

启动后会自动打开浏览器,默认地址为:http://localhost:8888

运行代码:

  • Shift + Enter:运行当前单元格并跳转到下一个
  • Ctrl + Enter:仅运行当前单元格
  • Alt + Enter:运行当前单元格并在下方插入新单元格

常用快捷键

命令模式(按 Esc 进入):

  • A:在上方插入新单元格
  • B:在下方插入新单元格
  • D, D:删除当前单元格
  • Z:撤销删除
  • Y:转换为 Code 单元格
  • M:转换为 Markdown 单元格

编辑模式(按 Enter 进入):

  • Tab:代码补全
  • Ctrl + /:注释/取消注释
  • Ctrl + Shift + -:从光标处分割单元格

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
# 数据分析示例
import pandas as pd
import numpy as np

# 创建随机数据
data = np.random.randn(5, 3)
df = pd.DataFrame(data, columns=['A', 'B', 'C'])

# 显示数据
df

# 计算统计信息
df.describe()
A B C
count 5.000000 5.000000 5.000000
mean -0.327939 0.134459 -0.056706
std 1.072673 1.999852 0.793373
min -1.621929 -3.321018 -0.750508
25% -0.934563 0.378673 -0.725583
50% -0.390032 0.594543 -0.406651
75% 0.092108 1.482626 0.798334
max 1.214724 1.537472 0.800877

额外的内核

Kotlin/kotlin-jupyter: Kotlin kernel for Jupyter/IPython