01. Python 工具
02. Python 基础
03. Numpy
- Numpy 简介
- Matplotlib 基础
- Numpy 数组及其索引
- 数组类型
- 数组方法
- 数组排序
- 数组形状
- 对角线
- 数组与字符串的转换
- 数组属性方法总结
- 生成数组的函数
- 矩阵
- 一般函数
- 向量化函数
- 二元运算
- ufunc 对象
- choose 函数实现条件筛选
- 数组广播机制
- 数组读写
- 结构化数组
- 记录数组
- 内存映射
- 从 Matlab 到 Numpy
04. Scipy
05. Python 进阶
- sys 模块简介
- 与操作系统进行交互:os 模块
- CSV 文件和 csv 模块
- 正则表达式和 re 模块
- datetime 模块
- SQL 数据库
- 对象关系映射
- 函数进阶:参数传递,高阶函数,lambda 匿名函数,global 变量,递归
- 迭代器
- 生成器
- with 语句和上下文管理器
- 修饰符
- 修饰符的使用
- operator, functools, itertools, toolz, fn, funcy 模块
- 作用域
- 动态编译
06. Matplotlib
- Pyplot 教程
- 使用 style 来配置 pyplot 风格
- 处理文本(基础)
- 处理文本(数学表达式)
- 图像基础
- 注释
- 标签
- figures, subplots, axes 和 ticks 对象
- 不要迷信默认设置
- 各种绘图实例
07. 使用其他语言进行扩展
- 简介
- Python 扩展模块
- Cython:Cython 基础,将源代码转换成扩展模块
- Cython:Cython 语法,调用其他C库
- Cython:class 和 cdef class,使用 C++
- Cython:Typed memoryviews
- 生成编译注释
- ctypes
08. 面向对象编程
09. Theano 基础
- Theano 简介及其安装
- Theano 基础
- Theano 在 Windows 上的配置
- Theano 符号图结构
- Theano 配置和编译模式
- Theano 条件语句
- Theano 循环:scan(详解)
- Theano 实例:线性回归
- Theano 实例:Logistic 回归
- Theano 实例:Softmax 回归
- Theano 实例:人工神经网络
- Theano 随机数流变量
- Theano 实例:更复杂的网络
- Theano 实例:卷积神经网络
- Theano tensor 模块:基础
- Theano tensor 模块:索引
- Theano tensor 模块:操作符和逐元素操作
- Theano tensor 模块:nnet 子模块
- Theano tensor 模块:conv 子模块
10. 有趣的第三方模块
11. 有用的工具
- pprint 模块:打印 Python 对象
- pickle, cPickle 模块:序列化 Python 对象
- json 模块:处理 JSON 数据
- glob 模块:文件模式匹配
- shutil 模块:高级文件操作
- gzip, zipfile, tarfile 模块:处理压缩文件
- logging 模块:记录日志
- string 模块:字符串处理
- collections 模块:更多数据结构
- requests 模块:HTTP for Human
12. Pandas
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Cython:Typed memoryviews
例子
这里 double[::1]
是一种 memoryview
方法,效率跟 Numpy
数组差不多,可以给 C
数组赋值,可以给 Numpy
数组赋值,可以像 Numpy
一样切片:
In [1]:
%%file cython_sum.pyx
def cython_sum(double[::1] a):
cdef double s = 0.0
cdef int i, n = a.shape[0]
for i in range(n):
s += a[i]
return s
Writing cython_sum.pyx
In [2]:
%%file setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext = Extension("cython_sum", ["cython_sum.pyx"])
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [ext],
)
Writing setup.py
In [3]:
!python setup.py build_ext -i
running build_ext
cythoning cython_sum.pyx to cython_sum.c
building 'cython_sum' extension
creating build
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Anaconda\Scripts\gcc.bat -DMS_WIN64 -mdll -O -Wall -IC:\Anaconda\include -IC:\Anaconda\PC -c cython_sum.c -o build\temp.win-amd64-2.7\Release\cython_sum.o
writing build\temp.win-amd64-2.7\Release\cython_sum.def
C:\Anaconda\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Release\cython_sum.o build\temp.win-amd64-2.7\Release\cython_sum.def -LC:\Anaconda\libs -LC:\Anaconda\PCbuild\amd64 -lpython27 -lmsvcr90 -o "C:\Users\lijin\Documents\Git\python-tutorial\07\. interfacing with other languages\cython_sum.pyd"
cython_sum.c: In function '__Pyx_BufFmt_ProcessTypeChunk':
cython_sum.c:13561:26: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13561:26: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13561:26: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c:13613:20: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13613:20: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13613:20: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c: In function '__pyx_buffmt_parse_array':
cython_sum.c:13675:25: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13675:25: warning: format '%d' expects argument of type 'int', but argument 3 has type 'size_t' [-Wformat]
cython_sum.c:13675:25: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c: In function '__Pyx_GetBufferAndValidate':
cython_sum.c:13860:7: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13860:7: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'Py_ssize_t' [-Wformat]
cython_sum.c:13860:7: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:13860:7: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c: In function '__Pyx_RaiseArgtupleInvalid':
cython_sum.c:14032:18: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:14032:18: warning: format '%s' expects argument of type 'char *', but argument 5 has type 'Py_ssize_t' [-Wformat]
cython_sum.c:14032:18: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:14032:18: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c: In function '__Pyx_RaiseTooManyValuesError':
cython_sum.c:14552:18: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:14552:18: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c: In function '__Pyx_RaiseNeedMoreValuesError':
cython_sum.c:14558:18: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:14558:18: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'Py_ssize_t' [-Wformat]
cython_sum.c:14558:18: warning: too many arguments for format [-Wformat-extra-args]
cython_sum.c: In function '__Pyx_ValidateAndInit_memviewslice':
cython_sum.c:15253:22: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:15253:22: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'Py_ssize_t' [-Wformat]
cython_sum.c:15253:22: warning: unknown conversion type character 'z' in format [-Wformat]
cython_sum.c:15253:22: warning: too many arguments for format [-Wformat-extra-args]
In [4]:
from cython_sum import cython_sum
from numpy import *
In [5]:
a = arange(1e6)
检查正确性:
In [6]:
cython_sum(a)
Out[6]:
499999500000.0
In [7]:
a.sum()
Out[7]:
499999500000.0
效率:
In [8]:
%timeit cython_sum(a)
100 loops, best of 3: 2.14 ms per loop
In [9]:
%timeit a.sum()
100 loops, best of 3: 2.38 ms per loop
In [10]:
import zipfile
f = zipfile.ZipFile('07-06-cython-sum.zip','w',zipfile.ZIP_DEFLATED)
names = ['cython_sum.pyx',
'setup.py']
for name in names:
f.write(name)
f.close()
!rm -f setup*.*
!rm -f cython_sum*.*
!rm -rf build
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论