我在python中拥有自己的包,而且我经常使用它.什么是最优雅或传统的目录,我应该把我的包,所以它将被导入而不玩PYTHONPATH或sys.path?
那么站点包呢?
/usr/lib/python2.7/site-packages.
在python中复制和粘贴包在那里常见吗?
我使用python 2.7并试图挑选一个对象.我想知道泡菜协议之间真正的区别是什么.
import numpy as np
import pickle
class data(object):
def __init__(self):
self.a = np.zeros((100, 37000, 3), dtype=np.float32)
d = data()
print "data size: ", d.a.nbytes/1000000.
print "highest protocol: ", pickle.HIGHEST_PROTOCOL
pickle.dump(d,open("noProt", 'w'))
pickle.dump(d,open("prot0", 'w'), protocol=0)
pickle.dump(d,open("prot1", 'w'), protocol=1)
pickle.dump(d,open("prot2", 'w'), protocol=2)
out >> data size: 44.4
out >> highest protocol: 2
Run Code Online (Sandbox Code Playgroud)
然后我发现保存的文件在磁盘上有不同的大小:
noProt:177.6MB prot0:177.6MB prot1:44.4MB prot2:44.4MB我知道这prot0是一个人类可读的文本文件,所以我不想使用它.我猜协议0是默认给出的协议.
我想知道协议1和协议2之间的区别是什么,我有理由选择其中一个或另一个吗?
什么是更好用,pickle或cPickle?
我正在尝试使用get_pip.py文件在我的Mac Yosemite 10.10.5上安装pip,但我遇到以下问题
Bachirs-MacBook-Pro:Downloads bachiraoun$ sudo python get-pip.py
The directory '/Users/bachiraoun/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/bachiraoun/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with …Run Code Online (Sandbox Code Playgroud) 我试图在matplotlib中的轴的末尾设置一个箭头.我不想移除刺并用纯箭替换它们,因为我需要它们的功能......
import matplotlib.pyplot as plt
import numpy as np
def arrowed_spines(ax=None, arrowLength=30, labels=('X', 'Y'), arrowStyle='<|-'):
xlabel, ylabel = labels
for i, spine in enumerate(['left', 'bottom']):
# Set up the annotation parameters
t = ax.spines[spine].get_transform()
xy, xycoords = [1, 0], ('axes fraction', t)
xytext, textcoords = [arrowLength, 0], ('offset points', t)
# create arrowprops
arrowprops = dict( arrowstyle=arrowStyle,
facecolor=ax.spines[spine].get_facecolor(),
linewidth=ax.spines[spine].get_linewidth(),
alpha = ax.spines[spine].get_alpha(),
zorder=ax.spines[spine].get_zorder(),
linestyle = ax.spines[spine].get_linestyle() )
if spine is 'bottom':
ha, va = 'left', 'center'
xarrow = …Run Code Online (Sandbox Code Playgroud) 我需要在matplotlib图上添加一个半透明的皮肤.我正在考虑在图中添加一个矩形,其中alpha <1且zorder足够高,因此它绘制在所有内容之上.
我在想这样的事情
figure.add_patch(Rectangle((0,0),1,1, alpha=0.5, zorder=1000))
Run Code Online (Sandbox Code Playgroud)
但我猜矩形只由Axes处理.有转弯吗?
是否可以像matplotlib.text.Text一样旋转matplotlib.axes.Axes
# text and Axes instance
t = figure.text(0.5,0.5,"some text")
a = figure.add_axes([0.1,0.1,0.8,0.8])
# rotation
t.set_rotation(angle)
a.set_rotation()???
Run Code Online (Sandbox Code Playgroud)
文本实例上的简单set_rotation将使文本围绕其坐标轴旋转角度值.对轴实例有什么办法吗?
实现比较两个numpy浮点数组的unittest的最佳方法是什么.
我已经尝试过unittest.assertEqual()但是对于float数组不起作用,因为float永远不会100%相等.我不能使用assertAlmostEqual,因为它测试圆(浮点)相等...
有没有人实现这样的事情
self.assertFloatArrayEqual(array1, array2, msg = "array are not equal")
Run Code Online (Sandbox Code Playgroud)
谢谢
我试图从git分支中删除所有文件,除了1个文件
# this removes all files
git rm -rf .
# what should i use to do somthing like that
git rm -rf . --except file1 file2
Run Code Online (Sandbox Code Playgroud) 生成有偏概率随机向量的最佳方法是什么?换句话说,给定方向向量“D(dx,dy,dz)”,有偏随机向量生成器仍将在所有方向上生成随机向量,但更有可能在 D 方向上生成向量
import numpy as np
# generate 1000 vectors in all directions
vectors = np.random.random((1000,3))-np.random.random((1000,3))
# generate biased vectors probability
# proba argument gives the biasing intensity or probability to be close to D vector
vectors = biased_proba_random_vectors(directon=(dx,dy,dz), proba=0.7,size=(1000,3))
# biased_proba_random_vectors is a representation, any other implementation is welcomed
Run Code Online (Sandbox Code Playgroud)
应该如下图所示

我只需要获取数组的小数部分。使用 numpy 或简单的 python modf 函数很方便。
如果我们有大量的正分数数据,例如可以像 (1000000,3) 一样大,这样做更方便:
numpy.modf(array)[0]
array-numpy.trunc(array)
Run Code Online (Sandbox Code Playgroud)
在我看来,2 在内存使用方面更快更便宜……但不确定。python 和 numpy 专家怎么看?
可散列的对象需要一个__hash__方法,并且它具有一个在其生命周期内永远不会改变的哈希值.
由于我完全忽略的原因,Python列表不可清除,我想知道以下实现是否正常,或者是否有一些我不知道的故障.
class L(list):
def __hash__(self):
return id(self)
a = range(10)
l = L(range(10))
print a
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print l
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
hash(l)
>> 41889288
hash(a) # unsurprisingly it returns an error because a list is not hashable
>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
# lets append a value to …Run Code Online (Sandbox Code Playgroud) python ×9
python-2.7 ×9
numpy ×4
matplotlib ×3
git ×1
hashable ×1
mutable ×1
pickle ×1
pip ×1
unit-testing ×1