Numpy简单使用

```python
import numpy

arr = numpy.array([[1,2,3,4],[5,6,7,8]])
print(arr)
print(arr[1, 2])
print(arr.ndim)    # rank 维度数
print(arr.shape)   # rows, columns 行列数
print(arr.size)    # number of element 元素个数
print(type(arr)) 
# help(numpy.array)

# print(numpy.ones(3, 2))   # float 1填充
# print(numpy.zeros(3, 4)) 
print(numpy.random.random(3))  # array of random value [0.0, 1.0)
print(numpy.random.random_sample((3, 2)) # 三行两列 [0.0, 1.0)
print(numpy.full((3, 3), 12))  # new_arr 12填充
print(numpy.full((3, 3), 12, dtype=numpy.float32)) # 指定数值类型
a = arr.copy()      # 

# numpy.loadtxt('')  # load from file
# numpy.save(a, '')  # save to file

print(numpy.arange(0, 10, 2))    # int  同range函数
print(numpy.linspace(0, 10, 6))  # float  [0,10]等分取6个元素0,2,4,6,8,10
# arr.resize(4, 2)    # resize return None 就地修改
# numpy.resize(arr, (4, 2))  # 返回新的arr
print(arr.reshape(2, 4))   # resize return new_arr
print(arr.ravel())      # flattened array return new_arr 扁平化
print(arr.transpose())  # transpose an array return new_arr
print(arr[0:1:100])   # start, end, step 行切片
print(arr[:, 2])      # columns 列切片
print(arr[..., 0:2])   # columns 列切片
print(arr[0:2, 0:2])   # rows, columns, 行列切片

# operations + - * / % ** < == >
# add, subtract, multiply, divide, remainder, power
print(numpy.dot(a.reshape(4, 2), [1, 0]))  **dot运算**: 二维行去dot列, columns1 = rows2
# dot operation is not commutative  A . B != B . A
print(arr + numpy.array(10))  # have same shape otherwise broadcast 广播
print(numpy.array(10) + numpy.ones((3, 2)))  # boardcast
aa[:, 0:1] += numpy.ones((4, 1), dtype=int)
print(numpy.exp(n))         # e**n , e = 2.718281828459045
print(numpy.exp(arr))       # x *= e
print(numpy.square(arr))    # x**2
print(numpy.sqrt(arr))      # x**(1/2)
print(numpy.around(1.5))    # Evenly round to the given number of decimals. 四舍六入五取偶
print(numpy.trunc(1.5))     # trunc截断, 类似int的操作, 返回float, 1.0 
print(numpy.floor(-1.5))    # float(floor)   math.floor + 0.0
print(numpy.ceil(-1.5))     # float(ceil)   math.ceil + 0.0
print(numpy.log(arr))       # log(x) 
print(numpy.sum(arr, axis=1), numpy.max(arr), numpy.min(arr))  
# axis=1 rows sum 行求和,axis=0 columns sum列求和
print(numpy.cumsum(arr, axis=1))    # cumulative sum 累加
print(numpy.mean(arr))      # mean value,average 平均数
print(numpy.median(arr))    # median 中位数, 从大到小排序取最中间位置的数, 或者中间两个数的平均值
```

 

你可能感兴趣的:(Python,第三方库,Numpy)