numpy 相关 (或list)
构造矩阵/向量
a = np.zeros((2,3)) #(一定要两层括号)
a = np.random.randn(5,1)
np.arange(0, samplePerChannel)
#python的二维数据表示要用二层括号来进行表示。
#如:产生一个2×3的全0矩阵,若是zeros(2,3)这种写法就会出现 TypeError: #data type not understood 这种错误;
# 正确的写法是 zeros((2,3))
numpy与list相互转换
a = np.zero(1,1)
a.tolist()
b = [[1,2,3],[3,4,5]]
b = np.array(b)
numpy读写csv或txt
np.loadtxt(f, delimiter=",", skiprows=41, usecols=np.arange(1, 107))
np.savetxt('features.csv', features, fmt='%.8e', delimiter=',')
取元素,长度
len(my_list)
my_array.shape #3,5
'''numpy'''
data([row,col]) # 只能numpy,不能list
data([row,:]) # the whole row
data([:,col]) # the whole col
data([row,start:end:step) # include start, not include end
'''list'''
my_list[1] #
my_list[-1] # 最后一个元素
增删改元素
'''list'''
my_list.append(element) # 末尾增加
my_list.pop(index) # 根据索引删除
my_list.pop() # 如果不写index,则删除最后一个。 若为空列表会报错
del listname[start : end] # 根据索引删除
del listname[index] # 根据索引删除
listname.remove(value) #根据元素值进行删除
listname。clear() # 清空列表
list 乱序
import random
random.shuffle(my_list) # 没有返回值,直接修改的list本身
拼接:
np.block()
x = np.array([1,2])#注意一定得是np.array,不能直接list
y = np.array([3,4])
z = np.block([x,y]) #[1 2 3 4]
z2 = np.block([[x],[y]]) # 第一行12,第二行34
print(z2)
my_list= my_list[0:-1] # 删除最后一个, 空列表不会报错
元素转为int
x=np.zeros(16).astype(int)
矩阵形状: shape(), reshape()
assert(a.shape == (5,1))
a = a.reshape((5,1))
v = image.reshape( image.shape[0] *image.shape[1] * image.shape[2])
增加一维
arr1 = arr[:, :, np.newaxis]
交换两轴
np.swapaxes(data,axis1,axis2)
对所有元素计算 log, exp, abs等
u=np.log() # 默认e为底
x = np.array([1, 2, 3])
print(np.exp(x))
np.abs()
v **2
最大,最小,求和,均值, 方差,标准差
np.maximum()
np.maximum(v,0)
np.sum(A)
A.sum(axis = 0) # axis = 0 ,按列; 1按行
np.mean(arr) # 方差
np.var(arr) # 标准差
np.std(arr) # 标准差
行标准化
x_norm = np.linalg.norm(x, axis = 1, keepdims = True)
x = x/x_norm
转置
w.T
乘法
np.dot(A,B) # 矩阵乘法(点乘)
Z = np.dot(w.T,X) + b
np.multiply(x1,x2) # 逐元素相乘
np.outer(x1,x2) # outer product
逐元素相除
np.true_divide(a,b)
累加
np.cumsum(deltaCHbO)
numpy 或list 排序
a_list.sort() # 改变a_list, 没有返回值
a_list.sorted() # 不改变a_list, 有返回值
np.sort(a) # 返回a排序的结果
np.argsort(a)# 返回排序的索引
np.argsort(np.argsort(a) # 返回排名
np.argmax(a) # 返回最大值的索引
a[np.argsort(a[:,0])] # 按第一列排序
# 例子
x = np.arange(100)
np.random.shuffle(x)
x = x[:16]
print(x)
print(np.sort(x))
print(np.argsort(x))
rank = np.argsort(np.argsort(x))
print(rank)
'''
[71 47 34 8 64 25 51 0 79 16 94 32 39 23 86 14]
[ 0 8 14 16 23 25 32 34 39 47 51 64 71 79 86 94]
[ 7 3 15 9 13 5 11 2 12 1 6 4 0 8 14 10]
[12 9 7 1 11 5 10 0 13 3 15 6 8 4 14 2]
'''
随机抽取list元素
import random
# random.choice:随机抽取1个
selected_element = random.choice(my_list)
# random.sample:随机抽取n个
selected_elements = random.sample(my_list, selected_num)
# 也可以先打乱再按顺序抽取
random.shuffle(my_list) # 没有返回值,直接修改的list本身
selected_elements = my_list[0:n]
找到(查找)列表中满足(符合)条件的元素
a = [1,2,3,4,5,6,7,8,9]
b = [str(x) for x in a if x%2==1]
s = ‘’.join(b)
print(s)
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr = arr[arr % 2 == 1]
结果就是13579.
找到满足条件的元素的索引
res = np.where(a > 3)[0]
- 如果a是一维的,会返回这样的东西:
- 第一个参数是array,第二个是dtype,
- 比如:(array([ 2, 2, 2, …, 29, 29, 29], dtype=int64)。
- 数据只取第一个即可。
- 如果a是多维的,会返回一个tuple,tuple的每个元素对应一个维度,每个元素也是上面的格式
- 如果是多维的话,可以用np.argwhere(a), 这样返回的是符合条件的索引,比如(n,3)
np.where(condition, x, y)
x:满足条件时函数的输出
y:不满足条件时的输出
2.np.where(condition)
没有x和y参数,则以元组形式输出满足条件的列表索引。
3.np.argwhere(condition)
对比两个list
wehavenot = [x for x in json_scenes if x not in our_data_scene_path_list]
theyhavenot = [x for x in our_data_scene_path_list if x not in json_scenes]
求出不重复的元素个数
(如果是numpy,先flatten,然后转换为list,
然后转换为set,就可以求出len)
detected_targets = set(proposal_map[:,1].flatten().tolist())
关于axis:
实验如下;
print("HbData.shape: ")
print(HbData.shape)
for i in range(0,3):
print(i)
print((np.mean(HbData, axis=i)).shape)
HbData.shape:
(96, 3, 15000, 53)
0
(3, 15000, 53)
1
(96, 15000, 53)
2
(96, 3, 53)
结论:
● 会沿axis这个轴计算
● aixs标号从0开始
● 比如如果是mean,sum这种只有一个值的,这一轴就会消失
几种不同的乘法
np.dot(x1,x2)
np.outer(x1,x2)
np.multiply(x1,x2)
● 向量点乘DOT PRODUCT OF VECTORS:
○ 结果是标量。
○ 同位置的元素相乘,结果相加。
○ np.dot(x1,x2)
● 逐元素相乘ELEMENTWISE:
○ 结果是向量。
○ 相同位置的元素相乘,还放在这个位置。
○ np.multiply(x1,x2)
● 外积 OUTER PRODUCT, 张量积,
○ 结果是矩阵
○ outer[i,j] = x1[i]*x2[j]
○
○ np.outer(x1,x2)
● 另一个外积:Exterior Product , Cross Product, 外积:向量积,叉乘,
random
import random
# random.choice:随机抽取1个
selected_element = random.choice(my_list)
# random.sample:随机抽取n个
selected_elements = random.sample(my_list, selected_num)
# 打乱顺序
random.shuffle(my_list) # 没有返回值,直接修改的list本身
os
import os
os.path.join(path,filename) # 连接两个或多个path
os.path.exists(path) # 判断是否存在
os.listdir(path) # 列出path下所有路径或文件
os.makedirs(path) # 新建文件夹,可以有多层
os.mkdir(path) # 新建文件夹,不能有多层
os.system(command) # 运行命令,比如linux的ls命令等
例子
delta_OD_1 = np.block([np.array( [0]),np.log(np.true_divide(filtedData1[0:samplePerChannel-1] , filtedData1[1:samplePerChannel] ))])
delta_OD_2 = np.block([np.array( [0]),np.log(np.true_divide(filtedData2[0:samplePerChannel-1] , filtedData2[1:samplePerChannel] ))])
deltaCHbO = (delta_OD_2*epsilon_HbR_1*L1 - delta_OD_1 * epsilon_HbR_2 * L2)\
/((epsilon_HbO_2*epsilon_HbR_1 - epsilon_HbO_1 * epsilon_HbR_2)*L1*L2)
deltaCHbR = (delta_OD_2*epsilon_HbO_1*L1 - delta_OD_1 * epsilon_HbO_2 * L2)\
/((epsilon_HbO_1 * epsilon_HbR_2 - epsilon_HbO_2* epsilon_HbR_1)* L1*L2 )
CHbO = np.cumsum(deltaCHbO) + 0.1
CHbR = np.cumsum(deltaCHbR) + 0.1
CHbTotal = CHbO + CHbR