目录
五. 对多个相同维度的类数组同时打乱顺序,但是保持它们之间的一一对应关系:
六. 获取当前所打开文件的路径os.path.dirname(os.path.realpath(sys.argv[0])):
七. 修改文件夹下所有文件的名字的简单示例os.rename:
八. python 执行某段程序,因不知名原因而一直卡顿着也不报错退出,可以设置超时,跳过这部分的执行。func_timeout.func_set_timeout(1, False)
十. signal.argrelextrema寻找局部极大、小值:
一. yield:
在一个函数中,当一个循环的结果经过yield,那么,这个函数就不再是普通的函数,它成为了一个generator(生成器)函数,返回的结果是生成器,那么,你就可以通过s.__next__()
或者next(s)
(s是调用函数返回值)方法获得每一次迭代的值。这样做的好处是节省内存。 比如下面的代码,每调用一次__next__()
,就会执行yield之前的代码,直到yield,就会挂起,等下一次再调用__next__()
,会接着上一次停留的地方继续执行,直到yield。
def f():
for i in range(3):
yield i
s = f()
print(s.__next__()) #或者next(s)
print(s.__next__()) #或者next(s)
print(s.__next__()) #或者next(s)
二. tf.constant:
创建一个常量tensor。
import tensorflow as tf;
import numpy as np;
t1 = tf.constant(value=[1, 2.2, 3, 5e-3],
dtype=None,
shape=None,
name="Const",
verify_shape=False)
with tf.Session() as sess:
print(sess.run(t1)) #output: [1. 2.2 3. 0.005]
三. tf.stack 和 tf.unstack:
tf.stack([x, y, z]) 等价于np.stack([x, y, z]),用于将多个相同形状的tensor按照指定维度堆叠在一起。与之相反的是tf.unstack,假设t.shape = (A,B,C), 那么tf.unstack(t,axis=0)相当于将t分解成A个(B,C)组合成的list。
#coding=utf-8
import tensorflow as tf;
import numpy as np;
x = tf.constant([1, 4])
y = tf.constant([2, 5])
z = tf.constant([3, 6])
s1 = tf.stack(values=[x, y, z],
axis=0,
name="stack") # [[1, 4], [2, 5], [3, 6]]
s2 = tf.stack([x, y, z], axis=1) # [[1, 2, 3], [4, 5, 6]]
s3 = tf.unstack(value=s1,
num=None,
axis=0,
name="unstack")
with tf.Session() as sess:
print(sess.run(s1)) #output: [[1 4] [2 5] [3 6]]
print(sess.run(s2)) #output: [[1 2 3] [4 5 6]]
print(sess.run(s3)) #output: [array([1, 4]), array([2, 5]), array([3, 6])]
四. tf.tensordot:
目前对其了解就是,用第一个张量中的值依次与第二个张量中的值相乘。比如下面的例子,当axes为0时,第一个张量是[1,2],第二个是[4,5,6],它们的结果就是1与[4,5,6]分别相乘和2与[4,5,6]分别相乘的结果组合在一起的新张量。
#coding=utf-8
import tensorflow as tf;
import numpy as np;
from openpyxl.styles.builtins import output
t = tf.constant([1,2])
t1 = tf.constant([4,5,6])
t2 = tf.tensordot(t, t1, axes=0)
with tf.Session() as sess:
print(sess.run(t2))
'''
output:
[[ 4 5 6]
[ 8 10 12]]
'''
五. 对多个相同维度的类数组同时打乱顺序,但是保持它们之间的一一对应关系:
#coding=utf-8
'''对多个相同维度的类数组同时打乱顺序,但是保持它们之间的一一对应关系'''
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
size = a.shape[0]
size = np.arange(size)
print(size) #[0 1 2 3]
np.random.shuffle(size)
print(size) #[2 3 0 1]
print(a[size]) #[3 4 1 2]
print(b[size]) #[7 8 5 6]
六. 获取当前所打开文件的路径os.path.dirname(os.path.realpath(sys.argv[0])):
#coding=utf-8
import os
import sys
s = os.path.dirname(os.path.realpath(sys.argv[0]))
print(s) #输出你的当前文件的绝对路径
七. 修改文件夹下所有文件的名字的简单示例os.rename:
#coding=utf-8
import os
fileDir = r'E:\test' #原文件夹
fileList = os.listdir(fileDir) #将文件夹下的文件名存入list
for i, f in enumerate(fileList): #遍历每一个文件
srcName = os.path.join(fileDir, f) #原文件名(绝对路径)
newFileName = str(i) + "_" + f.split("_")[0] + ".jpg" #新文件名,这个新文件名视情况而定,这里举的例子是我方便自己用的
dstName = os.path.join(fileDir, newFileName) #新文件名(绝对路径)
os.rename(src=srcName, dst=dstName) #重命名(原名,新名)
八. python 执行某段程序,因不知名原因而一直卡顿着也不报错退出,可以设置超时,跳过这部分的执行。func_timeout.func_set_timeout(1, False)
转载自:Python函数超时,用装饰器解决 func_timeout
#coding=utf-8
from func_timeout import func_set_timeout, FunctionTimedOut
import time
@func_set_timeout(timeout=0.1, allowOverride=False) #设置时间超过0.1s,就超时
def task():
while True:
print("Hello world")
time.sleep(1)
if __name__ == '__main__':
try:
task()
except FunctionTimedOut: #程序某部分运行时间超过了设定时间,则会抛出FunctionTimedOut异常
print("超时了") #写超时后,接下来执行的代码
九. 在代码编写中查看某一库的版本:
import numpy
print(numpy.__version__)
十. signal.argrelextrema寻找局部极大、小值:
from scipy import signal
#计算一维数组局部极大值所在的索引
y = np.array([3,5,8,3,6,1])
local_max_index = signal.argrelextrema(y, np.greater)
print(local_max_index) #output:(array([2, 4], dtype=int64),)
local_max_index = signal.argrelextrema(y, np.greater)[0] #加[0]返回的仅有数值部分
print(local_max_index) #output:[2 4]
#计算一维数组局部极小值所在的索引
x = np.array([6,2,8,7,3,9])
local_min_index = signal.argrelextrema(x, np.less)
print(local_min_index) #output:(array([1, 4], dtype=int64),)
local_min_index = signal.argrelextrema(x, np.less)[0] #加[0]返回的仅有数值部分
print(local_min_index) #output:[1 4]
#计算二维数组局部极大值所在的索引;axis=0,表示行与行之间的比较
y = np.array([[1, 2, 1, 2], [6, 6, 4, 0], [3, 6, 3, 4]])
local_max_index = signal.argrelextrema(y, np.greater, axis=0) #默认axis=0,表示行与行之间的比较
print(local_max_index)
#output:(array([1, 1], dtype=int64), array([0, 2], dtype=int64)),第一项指局部极大值在某列所在索引,第二项指局部极大值所在的列
local_max_index = signal.argrelextrema(y, np.greater, axis=0)[0] #加[0]返回局部极大值在某列的索引,加[1]表示返回局部极大值所在列
print(local_max_index) #output:[1 1]
#计算二维数组局部极大值所在的索引;axis=1,表示列与列之间的比较
y = np.array([[1, 2, 1, 2], [6, 6, 4, 0], [3, 6, 3, 4]])
local_max_index = signal.argrelextrema(y, np.greater, axis=1)
print(local_max_index)
#output:(array([0, 2], dtype=int64), array([1, 1], dtype=int64)),第一项指局部极大值在某行所在索引,第二项指局部极大值所在的行
local_max_index = signal.argrelextrema(y, np.greater, axis=1)[0] #加[0]返回的仅有数值部分
print(local_max_index) #output:[0 2]
#二维数组局部极小值所在索引的计算同上,仅将np.greater改为np.less即可。
。 。 。 。 。 。
来源:CSDN
作者:缘*
链接:https://blog.csdn.net/qq_35240640/article/details/103967624