我需要specificity我的分类,定义为:
TN/(TN+FP)
我正在写一个自定义得分手功能:
from sklearn.metrics import make_scorer
def specificity_loss_func(ground_truth, predictions):
print predictions
tp, tn, fn, fp = 0.0,0.0,0.0,0.0
for l,m in enumerate(ground_truth):
if m==predictions[l] and m==1:
tp+=1
if m==predictions[l] and m==0:
tn+=1
if m!=predictions[l] and m==1:
fn+=1
if m!=predictions[l] and m==0:
fp+=1
`return tn/(tn+fp)
score = make_scorer(specificity_loss_func, greater_is_better=True)
Run Code Online (Sandbox Code Playgroud)
然后,
from sklearn.dummy import DummyClassifier
clf_dummy = DummyClassifier(strategy='most_frequent', random_state=0)
ground_truth = [0,0,1,0,1,1,1,0,0,1,0,0,1]
p = [0,0,0,1,0,1,1,1,1,0,0,1,0]
clf_dummy = clf_dummy.fit(ground_truth, p)
score(clf_dummy, ground_truth, p)
Run Code Online (Sandbox Code Playgroud)
当我运行这些命令时,我p打印为:
[0 0 0 0 0 …Run Code Online (Sandbox Code Playgroud) 我们将AWS Elasticsearch用于日志.日志通过Logstash连续流式传输.定期删除旧索引的最佳方法是什么?
我搜索过,推荐的各种方法是:
使用lambda删除旧索引 - https://medium.com/@egonbraun/periodically-cleaning-elasticsearch-indexes-using-aws-lambda-f8df0ebf4d9f
使用预定的泊坞容器 - http://www.tothenew.com/blog/running-curator-in-docker-container-to-remove-old-elasticsearch-indexes/
对于像"删除15天以上的索引"这样的基本要求,这些方法似乎有些过分
实现这一目标的最佳方法是什么?AWS是否提供我可以调整的任何设置?
我有一台罗技C920挂在我的电脑上,我正试图用它来点击使用OpenCV的图片.
我知道我可以使用以下方法捕获图像:
cam = cv2.VideoCapture(1)
s, im = cam.read() # captures image
cv2.imshow("Test Picture", im) # displays captured image
cv2.imwrite("test.bmp",im) # writes image test.bmp to disk
Run Code Online (Sandbox Code Playgroud)
但它让我获得了我的相机能够拍摄的15MP静态照片.我从上面得到的东西远远不如我click拍的照片.
那么,有没有办法拍照(就像它可以在官方的WebCam软件中完成)?
我三者之间很困惑.我知道闭包是由另一个函数返回的函数,并且可以从封闭范围访问局部变量
例:
def add_nums(one):
def adder(two):
return one+two
return adder
a_10 = add_nums(10)
print a_10(5)
15
Run Code Online (Sandbox Code Playgroud)
在这里,adder是一个封闭.
但是,这也不是一个例子 partial
from functools import partial
a_10 = partial(add_nums, 10)
print a_10()(5)
15
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?
此外,装饰器用于向函数添加功能.
def add_nums(one):
def adder(two):
print "foo, bar"
return one+two
return adder
a_10 = add_nums(10)
print a_10(5)
foo, bar
15
Run Code Online (Sandbox Code Playgroud)
这三者有什么区别?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
x = [916,684,613,612,593,552,487,484,475,474,438,431,421,418,409,391,389,388,
380,374,371,369,357,356,340,338,328,317,316,315,313,303,283,257,255,254,245,
234,232,227,227,222,221,221,219,214,201,200,194,169,155,140]
kmeans = KMeans(n_clusters=4)
a = kmeans.fit(np.reshape(x,(len(x),1)))
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
print(centroids)
print(labels)
colors = ["g.","r.","y.","b."]
for i in range(len(x)):
plt.plot(x[i], colors[labels[i]], markersize = 10)
plt.scatter(centroids[:, 0], marker = "x", s = 150, linewidths = 5, zorder = 10)
plt.show()
Run Code Online (Sandbox Code Playgroud)
上面的代码显示了4个群集,但是它们绝对不是我想要的。
我还会收到一个错误,这甚至更糟。我得到的输出在下面的图片中。
我得到的错误是:TypeError: scatter() missing 1 required positional argument: 'y'错误不是大问题,因为无论如何我都不喜欢我拥有的东西。
以下是我希望群集输出如何显示的图像。
我正在读书filter,lambda而且map.当我尝试使用它们时,我发现了这个特点:
def returnAsIs(element):
return element
print range(10)
print filter(returnAsIs, range(10))
print map(lambda x : x, range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
为什么过滤器省略了第一个条目?
我有一个列表,希望根据关系操作输出项目.
a = range(10).我希望:
min(a[a>5])- >6
我该如何做到这一点?
我有print min([x for x in a if x > 5])但有更好的东西吗?
输出a[a>5]是1.
这是什么意思 ?
我最初以为它返回1表明,这种情况是通过列表的成员感到满意.但是,a[a>50]也回来了1.
我目前正在尝试从网站上读取txt文件.
到目前为止我的脚本是:
webFile = urllib.urlopen(currURL)
Run Code Online (Sandbox Code Playgroud)
这样,我可以使用该文件.但是,当我尝试存储文件(in webFile)时,我只获得了一个指向套接字的链接.我尝试的另一个解决方案是使用read()
webFile = urllib.urlopen(currURL).read()
Run Code Online (Sandbox Code Playgroud)
然而,这似乎删除格式化(\n,\t等等)都被删除.
如果我打开这样的文件:
webFile = urllib.urlopen(currURL)
Run Code Online (Sandbox Code Playgroud)
我可以逐行阅读:
for line in webFile:
print line
Run Code Online (Sandbox Code Playgroud)
这将导致:
"this"
"is"
"a"
"textfile"
Run Code Online (Sandbox Code Playgroud)
但我得到:
't'
'h'
'i'
...
Run Code Online (Sandbox Code Playgroud)
我希望在我的计算机上获取该文件,但同时保持格式.
我有一个使用NumberInput小部件从用户接受歌曲评级的表单。该数字应在0-5之间。我的歌曲模型有一个,MaxValueValidator但我希望NumberInput小部件仅显示0-5之间的选项。
简洁的代码更漂亮.所以,我会去
str_ = "Hello World "
print str_.strip().replace(" ", "-").lower()
Run Code Online (Sandbox Code Playgroud)
相比 :
str_ = "Hello World "
str_ = str_.strip()
str_ = str_.replace(" ", "-")
str_ = str_.lower()
print str_
Run Code Online (Sandbox Code Playgroud)
但是,如果函数返回不兼容的对象(None例如),则无效.
例证:
set1 = set([1, 2, 3])
set2 = set([1, 3, 5])
set3 = set()
set3.update(set1) #returns None
set3.update(set2)
set3.update(set1.union(set2))
print len(set3)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何方法可以链接这些命令,也许是一些括号技巧?