栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

02 jupyter 魔法命令(调包 调.py 时间等等)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

02 jupyter 魔法命令(调包 调.py 时间等等)

一、 %run   执行文件 1. 执行同文件夹下的 .py文件
%run hello.py
2.执行不同文件夹、不同文件夹多个.py文件
# 方法1 import函数名
import Mymodule.FirstML
Mymodule.FirstML.predict(1)

# 方法2 from import函数名
from Mymodule import FirstML
FirstML.predict(2)

二、%load  读取文件内容 1. .py文件的加载(加载单一模板)
# 运行%LOAD XXX.py后出现以下代码

# %load hello.py
def hello(name):
    print("hello", name, "!")

hello("jupyter")


# %load magic/Hello.py
def Hello(name):
    print("hello", name, "!")

Hello("jupyter")


三、%timeit  多次测试代码执行时间,求平均值

1. %timeit 语法

%timeit L = [i**2 for i in range(1000)]
194 µs ± 7.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

2.可用于推测算法时间复杂度

%%timeit    # 使用时间高于上述
L = []
for n in range(1000):
    L.append(n ** 2)
207 µs ± 3.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

3.%time 语法

%time L = [i**2 for i in range(1000)]
# CPU times: user 193 µs, sys: 0 ns, total: 193 µs
# Wall time: 195 µs 单句运算时长

%%time
L = []
for n in range(1000):
    L.append(n**2)
# CPU times: user 472 µs, sys: 6 µs, total: 478 µs
# Wall time: 482 µs 整体运算时长

import random
L = [random.random() for i in range(10000)]
%timeit L.sort()
# 33.4 µs ± 912 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
# timeit 计算平均运行时长

L = [random.random() for i in range(10000)]
%timeit L.sort()
# 32.7 µs ± 464 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit L.sort()
# 33.9 µs ± 295 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
四、 %lsmagic 查看其他魔法命令api

1. 查看api

%lsmagic

"""Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %conda  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics."""

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号