SELECT AVG(t1.evaluate_result) as median FROM (SELECT @rownum:=@rownum+1 as `row_number`, evaluate_result FROM (SELECT evaluate_result FROM pms_product WHERE evaluate_result IS NOT NULL ORDER BY evaluate_result) as tmp1 JOIN (SELECT @rownum:=0) as tmp2 ) as t1, ( SELECT count(1) as total_rows FROM pms_product WHERE evaluate_result IS NOT NULL ) as t2 WHERE 1 AND t1.row_number in (floor((total_rows+1)/2), floor((total_rows+2)/2)); 解释一下这条SQL
时间: 2023-06-12 20:06:59 浏览: 57
这条SQL语句的作用是计算具有非空评价结果的pms_product表中所有记录的中位数。以下是每个子查询的作用:
1. 子查询t1:将pms_product表中具有非空评价结果的记录按照评价结果从小到大排序,并在每行记录前添加一个行号。行号的初始值为0,这是通过使用MySQL内置变量@rownum实现的。然后,t1从排序后的结果中选择中位数所对应的两个行号(如果总行数为奇数,则为中间行号;如果总行数为偶数,则为中间两行的行号)并计算这些行的平均评价结果。
2. 子查询t2:计算具有非空评价结果的pms_product表中的记录总数。
最终的查询将两个子查询的结果结合起来,以计算中位数。
相关问题
以下代码有什么错误,怎么修改: import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from PIL import Image import matplotlib.pyplot as plt import input_data import model import numpy as np import xlsxwriter num_threads = 4 def evaluate_one_image(): workbook = xlsxwriter.Workbook('formatting.xlsx') worksheet = workbook.add_worksheet('My Worksheet') with tf.Graph().as_default(): BATCH_SIZE = 1 N_CLASSES = 4 image = tf.cast(image_array, tf.float32) image = tf.image.per_image_standardization(image) image = tf.reshape(image, [1, 208, 208, 3]) logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES) logit = tf.nn.softmax(logit) x = tf.placeholder(tf.float32, shape=[208, 208, 3]) logs_train_dir = 'log/' saver = tf.train.Saver() with tf.Session() as sess: print("从指定路径中加载模型...") ckpt = tf.train.get_checkpoint_state(logs_train_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('模型加载成功, 训练的步数为: %s' % global_step) else: print('模型加载失败,checkpoint文件没找到!') prediction = sess.run(logit, feed_dict={x: image_array}) max_index = np.argmax(prediction) workbook.close() def evaluate_images(test_img): coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for index,img in enumerate(test_img): image = Image.open(img) image = image.resize([208, 208]) image_array = np.array(image) tf.compat.v1.threading.Thread(target=evaluate_one_image, args=(image_array, index)).start() coord.request_stop() coord.join(threads) if __name__ == '__main__': test_dir = 'data/test/' import glob import xlwt test_img = glob.glob(test_dir + '*.jpg') evaluate_images(test_img)
在代码中,我注意到了几个错误:
1. 在导入`input_data`和`model`模块时,代码中没有找到这两个模块的导入语句。
2. `evaluate_one_image`函数中的`image_array`参数未定义。
3. 在`evaluate_images`函数中,`tf.train.Coordinator()`和`tf.train.start_queue_runners()`函数之间缺少了一个空行。
4. 在调用`evaluate_one_image`函数时,传递的参数应该是`(image_array, index)`而不是`(image_array, index)`。
以下是修改后的代码:
```python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from PIL import Image
import matplotlib.pyplot as plt
import input_data
import model
import numpy as np
import xlsxwriter
num_threads = 4
def evaluate_one_image(image_array, index):
workbook = xlsxwriter.Workbook('formatting.xlsx')
worksheet = workbook.add_worksheet('My Worksheet')
with tf.Graph().as_default():
BATCH_SIZE = 1
N_CLASSES = 4
image = tf.cast(image_array, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 208, 208, 3])
logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[208, 208, 3])
logs_train_dir = 'log/'
saver = tf.train.Saver()
with tf.Session() as sess:
print("从指定路径中加载模型...")
ckpt = tf.train.get_checkpoint_state(logs_train_dir)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print('模型加载成功, 训练的步数为: %s' % global_step)
else:
print('模型加载失败,checkpoint文件没找到!')
prediction = sess.run(logit, feed_dict={x: image_array})
max_index = np.argmax(prediction)
workbook.close()
def evaluate_images(test_img):
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for index, img in enumerate(test_img):
image = Image.open(img)
image = image.resize([208, 208])
image_array = np.array(image)
tf.compat.v1.threading.Thread(target=evaluate_one_image, args=(image_array, index)).start()
coord.request_stop()
coord.join(threads)
if __name__ == '__main__':
test_dir = 'data/test/'
import glob
import xlwt
test_img = glob.glob(test_dir + '*.jpg')
evaluate_images(test_img)
```
请注意,我只修复了我在代码中发现的错误,并没有对其他部分进行修改。如果还有其他错误或问题,请提供更多信息,以便我能够更好地帮助你。
#判断真值 def evaluate_formula(self,node,data): if node is not None: if node.value == '∧': return self.evaluate_formula(node.left,data) and self.evaluate_formula(node.right,data) elif node.value == '∨': return self.evaluate_formula(node.left,data) or self.evaluate_formula(node.right,data) elif node.value == '¬': if node.left is not None: return not self.evaluate_formula(node.left,data) elif node.left is None and node.right is not None: return not self.evaluate_formula(node.right,data) else: return data.get(node.value,False)
这个函数实现了对逻辑表达式的求值。其中,node表示表达式树的根节点,data是一个字典,包含了变量名与对应的布尔值。
对于每个节点,根据它的值来进行不同的操作:如果是“∧”,则递归求解左右子树的真值,并返回它们的逻辑与;如果是“∨”,则递归求解左右子树的真值,并返回它们的逻辑或;如果是“¬”,则递归求解左子树的真值,并返回它的逻辑非;否则,说明该节点是一个变量节点,直接从data中获取它的真值并返回。
最终,该函数会返回整个表达式的真值。