在numpy
某些操作中,有些操作会返回,(R, 1)
但有些操作会返回(R,)
.这将使矩阵乘法更加繁琐,因为reshape
需要显式.例如,给定一个矩阵M
,如果我们想要做的行数numpy.dot(M[:,0], numpy.ones((1, R)))
在哪里R
(当然,同样的问题也会出现在列中).我们会得到matrices are not aligned
错误,因为M[:,0]
是在外形(R,)
,但numpy.ones((1, R))
在形状(1, R)
.
所以我的问题是:
形状(R, 1)
和形状有什么区别(R,)
.我知道字面上它是数字列表和列表列表,其中所有列表只包含一个数字.只是想知道为什么不设计numpy
使它有利于形状(R, 1)
而不是(R,)
更容易的矩阵乘法.
上面的例子有更好的方法吗?没有明确重塑像这样:numpy.dot(M[:,0].reshape(R, 1), numpy.ones((1, R)))
Python中代字号运算符的用法是什么?
我能想到的一件事是在字符串或列表的两边做一些事情,比如检查字符串是否是回文符号:
def is_palindromic(s):
return all(s[i] == s[~i] for i in range(len(s) / 2))
Run Code Online (Sandbox Code Playgroud)
还有其他好用吗?
JavaScript中的函数是否类似于Python range()
?
我认为应该有一个更好的方法,而不是每次写下面的行:
array = new Array();
for (i = 0; i < specified_len; i++) {
array[i] = i;
}
Run Code Online (Sandbox Code Playgroud) 如何在Python中给出mean,std给出正态分布的概率?我总是可以根据像这个问题中的OP这样的定义明确地编写我自己的函数:在Python中的分布中计算随机变量的概率
只是想知道是否有一个库函数调用将允许你这样做.在我的想象中它会这样:
nd = NormalDistribution(mu=100, std=12)
p = nd.prob(98)
Run Code Online (Sandbox Code Playgroud)
在Perl中有一个类似的问题:如何在Perl中给出正态分布的点处计算概率?.但我没有在Python中看到一个.
Numpy
有一个random.normal
功能,但它就像采样,而不是我想要的.
我正在使用jq和json一起玩.我想知道如何有条件地打印那些东西.
说我对野外电话感兴趣geo
.我使用以下命令,发现只有一个条目geo
是null
:
% cat all.json | jq '.geo != null' | sort | uniq -c
1 false
6891 true
Run Code Online (Sandbox Code Playgroud)
如何在不打印其他所有内容的情况下打印出该条目?
print
在手册中没有看到类似命令的内容.这不起作用:cat all.json | jq 'if .place == null then . end'
.jq
抱怨语法错误.
我nginx
在OS X 10.8上使用.新安装nginx
但无法找到重启nginx的方法除外kill nginx_pid
说kill 64116
.想知道是否有更好的方法重新启动nginx
.
在Google和SO上找到了一些方法,但没有奏效:
nginx -s restart
sudo fuser -k 80/tcp ; sudo /etc/init.d/nginx restart
Run Code Online (Sandbox Code Playgroud)
错误消息nginx -s restart
是
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
Run Code Online (Sandbox Code Playgroud)
有时也会得到这个错误消息:
nginx: invalid option: "-s restart"
Run Code Online (Sandbox Code Playgroud) 向后循环python字符串的最佳方法是什么?
对于-1偏移的所有需要,以下似乎有点尴尬:
string = "trick or treat"
for i in range(len(string)-1, 0-1, -1):
print string[i]
Run Code Online (Sandbox Code Playgroud)
以下看起来更简洁,但实际上它是否产生了反向字符串,因此会有轻微的性能损失?
string = "trick or treat"
for c in string[::-1]:
print c
Run Code Online (Sandbox Code Playgroud) 是否有很好的方法可以"扩展"numpy ndarray?说我有这样的ndarray:
[[1 2]
[3 4]]
Run Code Online (Sandbox Code Playgroud)
并且我希望每行通过填充零来包含更多元素:
[[1 2 0 0 0]
[3 4 0 0 0]]
Run Code Online (Sandbox Code Playgroud)
我知道必须有一些蛮力的方法(比如用零构造一个更大的数组,然后从旧的小数组中复制元素),只是想知道有没有这样做的pythonic方法.尝试numpy.reshape
但没有奏效:
import numpy as np
a = np.array([[1, 2], [3, 4]])
np.reshape(a, (2, 5))
Run Code Online (Sandbox Code Playgroud)
Numpy抱怨说: ValueError: total size of new array must be unchanged
我已尝试使用pip install matplotlib
,git clone
然后python setup.py install
按照Mac OS 10.7 的安装常见问题中所述.但是我得到了同样的错误:
[...]
llvm-gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -DPY_ARRAY_UNIQUE_SYMBOL=MPL_ARRAY_API -DPYCXX_ISO_CPP_LIB=1 -I/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include -I. -I/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include/freetype2 -I./freetype2 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/ft2font.cpp -o build/temp.macosx-10.7-intel-2.7/src/ft2font.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
In file included from src/ft2font.cpp:3:
src/ft2font.h:16:22: error: ft2build.h: …
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法随机洗牌两个相关的列表而不打破其他列表中的对应?我发现相关问题在numpy.array
和c#
,但不完全一样的.
作为第一次尝试,一个简单的zip
技巧将做:
import random
a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
b = [2, 4, 6, 8, 10]
c = zip(a, b)
random.shuffle(c)
a = [e[0] for e in c]
b = [e[1] for e in c]
print a
print b
Run Code Online (Sandbox Code Playgroud)
它将获得输出:
[[1, 2], [7, 8], [3, 4], [5, 6], [9, 10]]
[2, 8, 4, 6, 10]
Run Code Online (Sandbox Code Playgroud)
只是发现它有点尴尬.它还需要一个额外的清单.
python ×8
numpy ×2
command-line ×1
iterator ×1
javascript ×1
jq ×1
json ×1
linux ×1
list ×1
macos ×1
matplotlib ×1
matrix ×1
nginx ×1
operators ×1
optimization ×1
probability ×1
restart ×1
reverse ×1
scipy ×1
shuffle ×1
statistics ×1
string ×1