我正在尝试使用pyplot.hist将一些数据绘制成直方图:
hst = pp.figure()
pp.hist(spkSum)
hst.show()
Run Code Online (Sandbox Code Playgroud)
spkSum包含以下数据:[1, 1, 9, 9, 20, 20, 33, 33, 50, 50]
理想情况下,我应该有一个垂直直方图,其条形图整齐地位于x轴上,在y轴上达到它们各自的值.相反,我有这个:

我该如何解决这个问题呢?
我有一个家庭作业问题,我应该为Kurtosis编写一个函数,如下所示:

分母中的theta是标准偏差(方差的平方根),分子中的x-with-the-bar是平均值x.
我已经实现了如下功能:
import numpy as np
from scipy.stats import kurtosis
testdata = np.array([1, 2, 3, 4, 5])
def mean(obs):
return (1. / len(obs)) * np.sum(obs)
def variance(obs):
return (1. / len(obs)) * np.sum((obs - mean(obs)) ** 2)
def kurt(obs):
num = np.sqrt((1. / len(obs)) * np.sum((obs - mean(obs)) ** 4))
denom = variance(obs) ** 2 # avoid losing precision with np.sqrt call
return num / denom
Run Code Online (Sandbox Code Playgroud)
前两个函数,mean并variance已成功的交叉验证用numpy.mean和numpy.var分别.
我尝试kurt …
假设我有以下数据框:
import pandas as pd
df = pd.DataFrame({'label': ['a', 'a', 'b', 'b', 'a', 'b', 'c', 'c', 'a', 'a'],
'numbers': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
'arbitrarydata': [False] * 10})
Run Code Online (Sandbox Code Playgroud)
arbitrarydata我想根据其他列中的值为该列分配一个值。一个天真的方法如下:
for _, grp in df.groupby(('label', 'numbers')):
grp.arbitrarydata = pd.np.random.rand()
Run Code Online (Sandbox Code Playgroud)
当然,这不会将更改传播回df. 有没有办法修改组以便更改反映在原始 DataFrame 中?
这是一个示例DataFrame,我将用它来更好地说明我的问题:
import pandas as pd
df = pd.DataFrame(pd.np.random.rand(30, 3), columns=tuple('ABC'))
df['event'] = pd.np.nan
df.loc[10, 'event'] = 'ping'
df.loc[20, 'event'] = 'ping'
df.loc[19, 'event'] = 'pong'
Run Code Online (Sandbox Code Playgroud)
我需要创建以每次出现为中心的n行窗口ping.
换句话说,我们i是一个包含行的索引ping中的event列.对于每一个i,我想选择df.ix[i-n:i+n].
因此,n=3我希望得到以下结果:
A B C event
7 0.8295863 0.2162861 0.4856461 NaN
8 0.156646 0.4730667 0.9968878 NaN
9 0.6709413 0.4796197 0.8747416 NaN
10 0.09942329 0.154008 0.5761598 ping
11 0.7168143 0.678207 0.7281105 NaN
12 0.8915475 0.8013187 0.9049722 NaN
13 0.9545411 …Run Code Online (Sandbox Code Playgroud) 我已经成功导入/安装 Twistedasyncioreactor并执行一个简单的异步函数:
from twisted.internet import asyncioreactor
asyncioreactor.install()
from twisted.internet import task
from twisted.internet.defer import inlineCallbacks
from twisted.internet.defer import ensureDeferred
async def sleepy(reactor):
print("SLEEPING")
await task.deferLater(reactor, 3.0, lambda: None)
print("done sleep")
return 42
@task.react
def main(reactor):
d = ensureDeferred(sleepy(reactor))
d.addCallback(print)
return d
Run Code Online (Sandbox Code Playgroud)
例如,我想在所述代码中混合异步库asyncio.sleep。我尝试过以下方法:
from twisted.internet import asyncioreactor
asyncioreactor.install()
from twisted.internet import task
from twisted.internet.defer import inlineCallbacks
from twisted.internet.defer import ensureDeferred
import asyncio
async def sleepy(reactor):
print("SLEEPING")
await asyncio.sleep(3)
print("done sleep")
return 42
@task.react
def main(reactor):
d = …Run Code Online (Sandbox Code Playgroud) 我想知道是否有某种迭代器可以迭代std :: string中的值,当它到达结尾时从头开始.换句话说,该对象将无限地迭代,一遍又一遍地吐出相同的值序列.
谢谢!
堆栈溢出,
下面是我的脚本的前几行:
from ConfigParser import SafeConfigParser
from docopt import docopt
import core as scrappy
ARGS = docopt(__doc__, version=scrappy.__version__)
if not ARGS['PATH']:
ARGS['PATH'] = './'
# load config file
CFG = SafeConfigParser()
if not CFG.read(ARGS['--cfg']): # call to CFG.read also loads file if it exists
raise IOError('Configuration file not found.')
Run Code Online (Sandbox Code Playgroud)
我尝试读取的配置文件与上述脚本位于同一目录中。默认情况下,docopt 将此文件的路径设置为./file.conf(我已经测试过,file.conf结果相同)。
脚本的最后一行总是被调用,提示找不到文件。我通过打印 的输出来确认这一点os.getcwd,该输出显示脚本的执行目录是终端指向的目录。
是什么赋予了?
我可以做什么来指向配置文件?
我需要在unicode字符串上评估levenshtein编辑距离,这意味着需要对包含相同内容的两个字符串进行规范化,以避免偏向编辑距离.
以下是我为测试生成随机unicode字符串的方法:
def random_unicode(length=10):
ru = lambda: unichr(random.randint(0, 0x10ffff))
return ''.join([ru() for _ in xrange(length)])
Run Code Online (Sandbox Code Playgroud)
这是一个失败的简单测试用例:
import unicodedata
uni = random_unicode()
unicodedata.normalize(uni, 'NFD')
Run Code Online (Sandbox Code Playgroud)
这是错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我检查确保它uni确实是一个unicode对象:
u'\U00020d93\U000fb2e6\U0005709a\U000bc31e\U00080262\U00034f00\U00059941\U0002dd09\U00074f6d\U0009ef7a'
Run Code Online (Sandbox Code Playgroud)
有人可以开导我吗?
python unicode normalization unicode-normalization python-unicode
当且仅当存在 cookie 时,我想将 URL 重定向到 Django 平台(通过 uwsgi)。否则,我需要将执行推迟到content_by_lua插件。
以下是我对这种逻辑的尝试:
location ~* "^/[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$" { # match a UUID v4
include uwsgi_params;
if ($cookie_admin) {
# if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
rewrite ^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$ /modif/$1 break;
uwsgi_pass frontend;
}
content_by_lua '
ngx.say("Ping! You got here because you have no cookies!")
';
}
Run Code Online (Sandbox Code Playgroud)
Nginx 认为用以下日志消息侮辱我的智商是必要和适当的:
nginx: [emerg] directive "rewrite" is not terminated by ";" in /opt/openresty/nginx/conf/nginx.conf:34
Run Code Online (Sandbox Code Playgroud)
也许我和 nginx 看起来一样密集,但我错过了什么?
额外的问题:我的一般方法安全和理智吗?有没有更好的方法来实现我的目标?
我正在尝试在Docker(v1.13.0)中设置Titan/Cassandra/Gremlin-Server堆栈.我面临的问题是,尝试连接到默认端口上的Gremlin-Server的应用程序8182报告错误(详情如下).
首先,这是一些相关的版本信息:
设置在a Dockerfile中进行,以便可重现.它假定一个卡桑德拉容器已经存在,在运行cassandra.yaml中start_rpc已设置为true.
该Dockerfile如下:
FROM openjdk:alpine
ENV TITAN 'titan-1.0.0-hadoop1'
RUN apk update && apk add bash unzip && rm -rf /var/cache/apk/* \
&& adduser -S -s /bin/bash -D srg \
&& wget -O /tmp/$TITAN.zip http://s3.thinkaurelius.com/downloads/titan/$TITAN.zip \
&& unzip /tmp/$TITAN.zip -d /opt && ln -s /opt/$TITAN /opt/titan \
&& rm /tmp/*.zip \
&& chown -R srg /opt/$TITAN/ \
&& /opt/titan/bin/gremlin-server.sh -i …Run Code Online (Sandbox Code Playgroud)