我正在使用json模块创建一个json包含类似条目的文件
json.dumps({"fields": { "name": "%s", "city": "%s", "status": "%s", "country": "%s" }})
Run Code Online (Sandbox Code Playgroud)
但是,在json创建的-file中,字段的顺序错误
{"fields": {"status": "%s", "city": "%s", "name": "%s", "country": "%s"}}
Run Code Online (Sandbox Code Playgroud)
这是一个问题,因为%s-strings的替换现在是不正确的.
如何强制dumps函数保持给定的顺序?
我试图在TensorFlow中将F1得分定义为自定义指标DNNClassifier.为此,我写了一个函数
def metric_fn(predictions=[], labels=[], weights=[]):
P, _ = tf.contrib.metrics.streaming_precision(predictions, labels)
R, _ = tf.contrib.metrics.streaming_recall(predictions, labels)
if P + R == 0:
return 0
return 2*(P*R)/(P+R)
Run Code Online (Sandbox Code Playgroud)
使用streaming_precision和streaming_recall来自TensorFlow来计算F1得分.之后,我为validation_metrics创建了一个新条目:
validation_metrics = {
"accuracy":
tf.contrib.learn.MetricSpec(
metric_fn=tf.contrib.metrics.streaming_accuracy,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
"precision":
tf.contrib.learn.MetricSpec(
metric_fn=tf.contrib.metrics.streaming_precision,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
"recall":
tf.contrib.learn.MetricSpec(
metric_fn=tf.contrib.metrics.streaming_recall,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
"f1score":
tf.contrib.learn.MetricSpec(
metric_fn=metric_fn,
prediction_key=tf.contrib.learn.PredictionKey.CLASSES)
}
Run Code Online (Sandbox Code Playgroud)
但是,虽然我得到了正确的精度和召回值,但f1score总是nan:
INFO:tensorflow:Saving dict for global step 151: accuracy = 0.982456, accuracy/baseline_label_mean = 0.397661, accuracy/threshold_0.500000_mean = 0.982456, auc = 0.982867, f1score = nan, global_step = …Run Code Online (Sandbox Code Playgroud) 我想在我的计算机上打开一个特定的应用程序,可以通过运行来完成
application://open/window/
Run Code Online (Sandbox Code Playgroud)
在 Windows 中,通过 Windows 运行命令(开始 -> 运行,或 Windows 键 + R)。
我怎样才能从Python运行它?
以下不起作用:
import os
os.system("application://open/window/")
Run Code Online (Sandbox Code Playgroud)
因为这将从 Windows 命令行运行命令,而 Windows 命令行不知道是什么application:。
我有一个名为savegame的文件mysave.sav,如果该文件已存在,我想将数据添加到该文件中.如果该文件不存在,我想创建该文件,然后添加数据.
添加数据工作得很好.但附加数据会覆盖现有数据.我按照axtavt的说明(PrintWriter追加方法不附加).
String savestr = "mysave.sav";
File f = new File(savestr);
PrintWriter out = new PrintWriter(savestr);
if ( f.exists() && !f.isDirectory() ) {
out = new PrintWriter(new FileOutputStream(new File(savestr), true));
out.append(mapstring);
out.close();
}
else {
out.println(mapstring);
out.close();
}
Run Code Online (Sandbox Code Playgroud)
mapstring我要附加的字符串在哪里.你能帮助我吗?谢谢!
我有一个带有gunicorn的Flask API。Gunicorn将所有请求记录到我的API中,即
172.17.0.1 - - [19/Sep/2018:13:50:58 +0000] "GET /api/v1/myview HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
但是,我想过滤日志以排除某个端点,该端点在几秒钟内都从其他服务调用。
我编写了一个过滤器以排除此端点被记录:
class NoReadyFilter(logging.Filter):
def filter(self, record):
return record.getMessage().find('/api/v1/ready') == -1
Run Code Online (Sandbox Code Playgroud)
并且如果我将此过滤器添加到werkzeug记录器并使用Flask开发服务器,则该过滤器将起作用。请求/api/v1/ready将不会出现在日志文件中。但是,我似乎无法将过滤器添加到gunicorn记录器。使用以下代码,请求/api/v1/ready仍然出现:
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.glogging.Logger')
gunicorn_logger.setLevel(logging.INFO)
gunicorn_logger.addFilter(NoReadyFilter())
Run Code Online (Sandbox Code Playgroud)
如何为gunicorn记录器添加过滤器?我尝试gunicorn.error按照此处的建议将其添加到-logger中,但没有帮助。
如果我使用Sobel过滤器将图像应用于Python中的图像,scipy.ndimage.filters.convole则会得到有意义的结果,例如,对于此简单的输入图像img
0 255 0
0 255 0
0 255 0
Run Code Online (Sandbox Code Playgroud)
卷积
dimage.filters.convolve(img, Kx)
Run Code Online (Sandbox Code Playgroud)
与 Kx
-1 0 1
-2 0 2
-1 0 1
Run Code Online (Sandbox Code Playgroud)
在x方向上返回有意义的渐变:
-1020 0 1020
-1020 0 1020
-1020 0 1020
Run Code Online (Sandbox Code Playgroud)
我不知道如何在C ++中使用openCV2获得等效的结果。当我通过定义输入图像时
int image_data[9] = {0, 255, 0, 0, 255, 0, 0, 255, 0};
cv::Mat image = cv::Mat(3, 3, CV_32F, image_data);
Run Code Online (Sandbox Code Playgroud)
并通过以下方式应用内核
cv::Mat gradientx;
double sobelx_data[9] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
cv::Mat sobelx = cv::Mat(3, 3, CV_32F, sobelx_data); …Run Code Online (Sandbox Code Playgroud) 申请时,rad2deg我得到
>>> np.rad2deg(4*np.pi)
720.0
Run Code Online (Sandbox Code Playgroud)
的角度720.0度是在应用等同于角许多360.0度.
将弧度(从dtype=float64)转换为结果为正确值的度数的最佳方法是[0,180]什么?