示例页面

python-高效设置

魔法命令

魔法命令是python中的一种快捷方式,通常是 % 加上指定函数,就可快速执行该命令。

魔法命令 含义 示例
%time 对代码行测速 %time sum(range(100000))
%timeit 类似%time,但是%timeit可以统计多次运行后的测速结果 %timeit sum(range(100000))
或者 %timeit -n 2000 -r 5 sum(range(100000))
%%timeit 对整个代码单元格测速
%history 获取代码单元格的运行历史记录 %history -1 3
%ls 确认正在使用的文件名称 %ls
%pwd 当前文件夹路径 %pwd
%autosave 可设置每隔多长时间自动保存 %autosave 60
%matplotlib 与模块matplotlib设置相关的魔法命令,最好写在最开始的代码单元格中 %matplotlib inline 绘制好的图形显示在代码单元格下方
%matplotlib tk 在新的窗口中绘制交互图形界面

%matplotlib notebook 代码单元格下方显示图形,但是是可交互的

LaTex格式公式

在代码单元格中可以像这样来显示公式

\begin{align} # 固定语法

\sqrt{2x-1}+(3+x)^3 # 输入公式

\end{align} # 固定语法

\begin{align}
\sqrt{2x-1}+(3+x)^3
\end{align}

行公式:$E=mc^2$
\(E=mc^2\)

块公式:$$E=mc^2$$

\[E=mc^2 \]

也可以在内部显示公式$\sqrt{a^2+b^2}$ \(\sqrt{a^2+b^2}\)

\(\alpha\)

# 一个代码块展示多个结果
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

数据展示问题

import pandas as pd
pd.set_option('display.float_format', lambda x: '%.3f' % x) # 为了直观的显示数字,不采用科学计数法

数据在console展示宽度

import pandas as pd
pd.set_option("display.width", 80)  # 数据在console展示宽度
pd.set_option("display.max_colwidth", 100)  # 每一列数据最大宽度
# 设置pandas最大显示行数
import pandas as pd
pd.set_option("display.max_rows", 100)

绘图格式问题

# 查看当前系统字体
import matplotlib
mpl_fonts=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
print('all font list get from matplotlib.font_manager:')
for fonts in mpl_fonts:
    print('\t' + fonts)
# 绘图中能够正常显示中文
import matplotlib.pyplot as plt
plt.rcParams["font.family"]="SimHei" # windows版本

# mac端解决中文显示问题
# 先在终端输入:fc-list :lang=zh,若显示not found,再输入conda install fontconfig,然后选择 y 即可
plt.rcParams['font.sans-serif'] = ['Arial Black'] # mac版本
# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # mac系统

# 绘图中正常显示负数
plt.rcParams['axes.unicode_minus'] = False
import os 
os.getcwd() # 获取系统当前路径

忽略运行警告

import warnings
warnings.filterwarnings('ignore')

统计函数

函数 说明 函数 说明
count 非缺损值的数量 unique 去掉重复值后的数量
top 出现频率最多的值 freq top值的数量
sum 求和 mean 平均值
median 中位数 min 最小值
max 最大值 std 标准差
var 方差 skew 样本值的偏度(三阶矩)
kurt 样本值的峰度(四阶矩) quantile 样本分位数(百分比值)
cov 协方差矩阵 corr 连续变量的相关系数
posted @ 2022-03-22 15:34  没有风格的Wang  阅读(81)  评论(0编辑  收藏  举报