这篇文章重点介绍如何在有编程基础的情况下,学习python和numpy用来做后面的AI算法开发的思路, 和在学习过程中的一些笔记,会随着后续的学习逐渐整理
关于Python:
未完待续
python 开发环境构建
很多资料,我们采用Anoconda ,并使用其中的Jupyter (代码即文档)来上手python
语法特点: 解释型语言,缩进型代码块 indent as block, 冒号分割 colon as expression
变量:
Immutable variables:Numbers : int long float .etc ,bool stringtuple
Mutable Variables : list dict
有意思的关键字: dir ,del 等等 后续补充
有意思的函数:
complex(real [,imag ]) 创建一个复数 ,不知道有没有转换极坐标的ord(x ) 将一个字符转换为它的整数值
hex(x ) 将一个整数转换为一个十六进制字符串
oct(x ) 将一个整数转换为一个八进制字符串
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
各种循环判断
for condition:
elif condition :
else :
for iterating in range(10) :
block1
for iterating in quence :
block2
break
contiune
pass
异常处理 , read those typical exceptions :
https://www.runoob.com/python/python-exceptions.html
try:
except IOException:
except Exception:
文件,数组,socket string 的常用处理
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
list: note that list is different with the array in numpy, list in python has only one dimention
https://www.runoob.com/python/python-lists.html
cmp(l1,l2) , len(l1), max(l1) , list.append(obj) , list.pop(index)
list1 = []
list1.append('Google')
list2[1:5]
del list1[2]
面向对象中的继承,接口等
内存管理
编译原理,底层
python如何管理复杂项目
Package Management
如何测试
Numpy 科学数据库
未完待续
提供了一系列库函数帮助我们实现AI 算法,还可以通过结合matplotlib绘制出各种有用的图表
以下是对于numpy 库的简单介绍:
Ndarray : numpy 运算的元结构数据
import numpy as np
arr = np.arrange(8).reshape(4,3)
brr = np.sin(arr)
除了一些基本科学计算方法外,numpy 中的函数,方法,主要是针对ndarry 进行各种操作和计算:
元数据结构生成 generation
arr.ndim , arr.size , arr.shape
元数据index
arr[1,3,4]
arr[arr>3], arr[-1,:]
for row in arr , for col in arr.T , for item in arr.flat
元数据结构矩阵操作 operation
matrix operations , sort
计算 computing
br
元数据除了支持常规向量的操作比如:
arr.dot(brr) , arr.T()
还有各种特殊操作来满足计算需求:例如
Intersting :
Array Indexing and Boolean indexing
For example, if you start with this array:
>>>
>>> a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
You can easily print all of the values in the array that are less than 5.
>>>
>>> print(a[a < 5])
[1 2 3 4]
You can select elements that are divisible by 2:
>>>
>>> divisible_by_2 = a[a%2==0]
>>> print(divisible_by_2)
[ 2 4 6 8 10 12]
How it works, it's is manipulated step by step
>>> a = np.arange(12).reshape(3,4)
>>> b = a > 4
>>> b # b is a boolean with a's shape
array([[False, False, False, False],
[False, True, True, True],
[ True, True, True, True]])
>>> a[b] # 1d array with the selected elements
array([ 5, 6, 7, 8, 9, 10, 11])
>>> a[b] = 0 # All elements of 'a' higher than 4 become 0
>>> a
array([[0, 1, 2, 3],
[4, 0, 0, 0],
[0, 0, 0, 0]])
You could also implement the reduce as follows:
>>>
>>> def ufunc_reduce(ufct, *vectors):
... vs = np.ix_(*vectors)
... r = ufct.identity
... for v in vs:
... r = ufct(r,v)
... return r
ufunc_reduce(np.add,a,b,c)
>>> a = np.arange(30)
>>> b = a.reshape((2, -1, 3)) # -1 means "whatever is needed"
>>> b.shape
(2, 5, 3)
References :
NdArray in Memory , = ,view(), copy()
https://numpy.org/doc/stable/user/quickstart.html#copies-and-views
https://numpy.org/doc/stable/user/absolute_beginners.html
https://numpy.org/doc/stable/user/absolute_beginners.html#how-to-create-an-array-from-existing-data
https://numpy.org/doc/stable/user/quickstart.html#functions-and-methods-overview
特殊机制
Broadcasting allows universal functions to deal in a meaningful way with inputs that do not have exactly the same shape.
添1 规则
The first rule of broadcasting is that if all input arrays do not have the same number of dimensions, a “1” will be repeatedly prepended to the shapes of the smaller arrays until all the arrays have the same number of dimensions.
同数补完规则
The second rule of broadcasting ensures that arrays with a size of 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is assumed to be the same along that dimension for the “broadcast” array.
After application of the broadcasting rules, the sizes of all arrays must match.
特殊索引
NumPy offers more indexing facilities than regular Python sequences.arrays can be indexed by arrays of integers and arrays of booleans.
This is a good way of generating sub arrays
如何批量数据处理
性能问题,如何处理海量数据
如何绘制曲线
import matplotlib.pyplot as plt