标签:pen cal 方法 flat 最大值 运算符 body type 画布
‘‘‘ ndarray: N维数组类型ndarray,它描述了相同类型的“items”集合 ‘‘‘ import numpy # 存储数据 score = numpy.array([ [80,89,86,67,79], [78,97,89,67,81], [90,94,90,67,69], [91,91,90,67,69], [76,87,75,67,86], [70,79,84,67,84], [94,92,93,67,64], [86,85,83,67,80] ]) print(score,"\n",type(score))
import numpy import time import random a = [] for i in range(100000000): a.append(random.random()) t1 = time.time() sum1 = sum(a) t2 = time.time() b = numpy.array(a) t4 = time.time() # numpy求和 sum3 = numpy.sum(b) t5 = time.time() print(t2-t1,t5-t4)
import numpy # ndarray的属性 score = numpy.array([ [80,89,86,67,79], [78,97,89,67,81], [90,94,90,67,69], [91,91,90,67,69], [76,87,75,67,86], [70,79,84,67,84], [94,92,93,67,64], [86,85,83,67,80] ]) # 数组的行列 (8, 5) print(score.shape) # 数组的维度 print(score.ndim) # 数组的元素个数 print(score.size) # 数组的类型 int32 print(score.dtype) # 数组中每个元素所占的字节数 4 print(score.itemsize) # 数组中各元素的虚部 print(score.imag)
名称 | 描述 |
---|---|
bool_ | 布尔型数据类型(True 或者 False) |
int_ | 默认的整数类型(类似于 C 语言中的 long,int32 或 int64) |
intc | 与 C 的 int 类型一样,一般是 int32 或 int 64 |
intp | 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64) |
int8 | 字节(-128 to 127) |
int16 | 整数(-32768 to 32767) |
int32 | 整数(-2147483648 to 2147483647) |
int64 | 整数(-9223372036854775808 to 9223372036854775807) |
uint8 | 无符号整数(0 to 255) |
uint16 | 无符号整数(0 to 65535) |
uint32 | 无符号整数(0 to 4294967295) |
uint64 | 无符号整数(0 to 18446744073709551615) |
float_ | float64 类型的简写 |
float16 | 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位 |
float32 | 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位 |
float64 | 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位 |
complex_ | complex128 类型的简写,即 128 位复数 |
complex64 | 复数,表示双 32 位浮点数(实数部分和虚数部分) |
complex128 | 复数,表示双 64 位浮点数(实数部分和虚数部分 |
import numpy as np # ndarray的形状 a = np.array([[1,2,3],[4,5,6]]) b = np.array([1,2,3,4]) c = np.array([ [ [1,2,3], [4,5,6] ], [ [1,2,3], [4,5,6] ] ]) # (2, 3) (4,) (2, 2, 3) print(a.shape,b.shape,c.shape) # int32 print(a.dtype) data = np.array([1.1,2.2,3.3]) # float64 print(data.dtype) # 创建时可以指定数据类型 data = np.array([1.1,2.2,3.3],dtype="float32") print(data.dtype) # 基本不用于字符串的运算
import numpy as np # 生成0和1的数组 # res = np.zeros((3,4)) # print(res) # # res = np.ones((3,4),dtype="float32") # print(res) # 从现有数组中生成 # score = np.array([ # [80,89,86,67,79], # [78,97,89,67,81], # [90,94,90,67,69], # [91,91,90,67,69], # [76,87,75,67,86], # [70,79,84,67,84], # [94,92,93,67,64], # [86,85,83,67,80] # ]) # data1 = np.array(score) # print(data1) # data2 = np.asarray(score) # print(data2) # data3 = np.copy(score) # print(data3) # # score[3,1] = 0 # print(data1) # print(data2) # print(data3) ‘‘‘ 结论: 只有asarray进行了浅拷贝,其余都为深拷贝 ‘‘‘ # 生成固定范围的数组 data4 = np.linspace(0,10,11,dtype="float64") # 生成[0,10]范围内等距的11个数 print(data4) data5 = np.arange(0,10,2,dtype="float64") # 生成[0,10)范围,以2为步长的数 print(data5) # 生成随机数组
import numpy as np import matplotlib.pyplot as plt data1 = np.random.uniform(-1,1,(3,3)) # 从[-1,1) 中生成均匀分布的数 # # 1.创建画布 # plt.figure(figsize=(20,8),dpi=50) # # 2.绘制直方图 # plt.hist(data1,100) # # 3.显示图像 # plt.show() data2 = np.random.normal(1.75,0.2,10000) # 生成(u=1.75,sigma=0.2)的10000个正态分布数 # # 1.创建画布 # plt.figure(figsize=(20,8),dpi=50) # # 2.绘制直方图 # plt.hist(data2,100) # # 3.显示图像 # plt.show()
import numpy as np stock_change = np.random.normal(0,1,(8,10)) # print(stock_change) # # 获取指定的数据 左闭右开 # print(stock_change[0,0:3]) # print(stock_change[0,]) # print(stock_change[0,0:8:2]) # # c = np.array([ # [ # [1,2,3], # [4,5,6] # ], # [ # [1,2,3], # [4,5,6] # ] # ]) # print(c[1,0,0:2]) # c[1,0,0] = 1000 # print(c[1,0,0:2]) # 形状修改 # res = stock_change.reshape((10,8)) # 只是对数据按顺序重新分割,返回新的 # print(res) # stock_change.resize() # 修改原始的数据,也只是对数据按顺序重新分割 # print(stock_change) # print(stock_change.T) # 矩阵的转置 # 类型的修改 # res = stock_change.astype("int64") # print(res) # print(stock_change.tostring()) # 数组的去重 temp = np.array([[1,2,3,4],[3,4,5,6]]) res = np.unique(temp) print(res) # 把多为数组拆成一维数组 res = temp.flatten() print(res)
import numpy as np # stock_change = np.random.normal(0,1,(4,4)) # print(stock_change) # # 找绝对值大于0.5的 # print(stock_change.__abs__()>0.5) # # 输出绝对值大于0.5的位置 # print(stock_change[stock_change.__abs__()>0.5]) # # 把绝对值大于0.5的改为0 # stock_change[stock_change.__abs__()>0.5] = 0 # print(stock_change) ‘‘‘ 通用判断函数: np.all(一组布尔值) 只要有一个False就返回False np.any(一组布尔值) 只要有一个True就返回True ‘‘‘ # 三元运算符 np.where(布尔值,True时置的值,False置的值) # 逻辑运算 # data = np.array([[0,0.5,1],[2,0,0.6],[0.7,0.4,0.8]]) # print(data) # res = np.logical_and(data>0.5,data<1) # print(res) # res = np.logical_not(data>=0.5) # print(res) # res = np.logical_or(data<0.5,data>1) # print(res) # res = np.logical_xor(data,0) # print(res)
‘‘‘ 常用的统计函数: max,min,sum,prod(计算所有元素的积),std,var, mean(均值),median(中位数) ‘‘‘ import numpy as np data = np.array([[0,0.5,1],[2,0,0.6],[0.7,0.4,0.8]]) # 按列求最大值 print(data.max(axis=0)) # 按行求最大值 print(data.max(axis=1)) # 返回最大值,最小值所在的位置 ‘‘‘ np.argmax(temp,axis=) np.argmin(temp,axis=) ‘‘‘ print(np.argmax(data,axis=0)) print(np.argmax(data,axis=1))
import numpy as np # 数组与数的运算 arr = np.array([[0,0.5,1],[2,0,0.6],[0.7,0.4,0.8]]) # print(arr + 1) # print(arr - 1) # print(arr * 2) # print(arr / 2) # 数组与数组的运算 ‘‘‘ 满足一下条件之一: 1.维度相同 2.shape中对应的一个地方为1 例如: 256 256 3 3 256 256 3 9 1 7 1 8 1 5 9 8 7 5 5 4 1 5 4 15 3 5 15 1 1 15 3 5 以上的都可以进行运算 ‘‘‘
import numpy as np ‘‘‘ 两种存储矩阵的方法 1.ndarray 二维数组 矩阵乘法:np.matmul np.dot @ 2.matrix数据结构 矩阵乘法:直接相乘 ‘‘‘ data = np.array([ [80,86], [82,80], [85,78], [90,90], [86,82], [82,90], [78,80], [92,94] ]) # print(type(data)) data_mat = np.mat([ [80,86], [82,80], [85,78], [90,90], [86,82], [82,90], [78,80], [92,94] ]) # print(type(data_mat)) ‘‘‘ 运算规则 形状 (m,n)*(n,l) = (m,l) ‘‘‘ weight = np.array([[0.3],[0.7]]) weight_mat = np.mat(weight) # print(np.matmul(data,weight)) # print(np.dot(data,weight)) # print(data_mat*weight_mat) print(data@weight)
import numpy as np # a = np.array([[1,2,3],[4,5,6]]) # b = np.array([[7,8,9],[10,11,12]]) # # 水平拼接 # print(np.hstack((a,b))) # print(np.concatenate((a,b),axis=1)) # # 垂直拼接 # print(np.vstack((a,b))) # print(np.concatenate((a,b),axis=0)) a = np.array([[1,2],[3,4]]) b = np.array([[5,6]]) print(np.concatenate((a,b),axis=0)) print(np.concatenate((a,b.T),axis=1)) # 矩阵分割 x = np.arange(8) # 分成4份,必须是可分的 # [array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7])] print(np.split(x,4)) # 分到指定下标前面的部分 # [array([0]), array([1]), array([2]), array([3, 4, 5, 6, 7]), array([], dtype=int32)] print(np.split(x,[1,2,3,20]))
标签:pen cal 方法 flat 最大值 运算符 body type 画布
原文地址:https://www.cnblogs.com/yuyunjie/p/13137528.html