我有一个相当大的程序,我random
在不同的文件中使用模块中的函数.我希望能够在一个地方设置一次随机种子,使程序始终返回相同的结果.甚至可以实现python
吗?
如果我在列表中有一组项目.我想根据另一个权重列表从该列表中进行选择.
例如我的收藏是['one', 'two', 'three']
和权重[0.2, 0.3, 0.5]
,我希望这个方法在所有抽奖的大约一半中给我'三'.
最简单的方法是什么?
我需要获得任意大小的numpy.ndarray的第一个和最后一个维度.
如果我有shape(A) = (3,4,4,4,4,4,4,3)
我的第一个想法会做result = shape(A)[0,-1]
但但这似乎不适用于元组,为什么不呢?
有没有比这更简洁的方式
s=shape(A)
result=(s[0], s[-1])
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我正在尝试在python中使用argparse库来读取可选和必需的参数.到目前为止我这样做:
import argparse
parser = argparse.ArgumentParser(description='Cleanup Script for Folder')
parser.add_argument('PATH_TO_WORKDIR_ROOT', type=str, dest='PATH_TO_WORKDIR_ROOT', action='store', help='(absolute or) relative path to directory \n to work on, e.g. "..\MyFolder\\"')
parser.add_argument('--PATH_TO_BACKUP_ROOT', type=str, dest='PATH_TO_BACKUP_ROOT', action='store', help='(absolute or) relative path to Backup-Directory \n default is ".\BACKUP\"')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
现在我正在测试我的代码,它给了我一个我不理解的值错误:
$ python argparsetest.py --help
Traceback (most recent call last):
File "argparsetest.py", line 5, in <module>
parser.add_argument('PATH_TO_WORKDIR_ROOT', type=str, dest='PATH_TO_WORKDIR_ ROOT', action='store', help='(absolute or)
relative path to directory \n to wo rk on, e.g. "..\MyFolder\\"')
File "C:\Program
Files\Enthought\Canopy\App\appdata\canopy-1.3.0.1715.win-x86_
64\lib\argparse.py", …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将matplotlib.colormap对象与pandas.plot函数结合使用:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
df = pd.DataFrame({'days':[172, 200, 400, 600]})
cmap = cm.get_cmap('RdYlGn')
df['days'].plot(kind='barh', colormap=cmap)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我知道我应该以某种方式告诉colormap它正在被输入的值的范围,但我无法弄清楚如何使用pandas .plot()函数,因为这个plot()不接受vmin例如/ vmax参数.
我正在使用 numpy.svd 来计算条件不良矩阵的奇异值分解。对于某些特殊情况,svd 不会收敛并引发 Linalg.Error。我做了一些研究,发现 numpy 使用 LAPACK 中的 DGEESDD 例程。标准实现的硬编码迭代限制为 35 次或更多迭代。如果我尝试在 Matlab 中分解相同的矩阵,一切都会正常,我认为有两个原因: 1. Matlab 使用 DGESVD 而不是 DGEESDD,后者通常看起来更稳健。2. Matlab 在例程中使用 75 次迭代限制。(他们在源代码中更改了它并重新编译了它。)
现在的问题是:是否有一种简单的方法可以将 numpy 中使用的后端从 DGEESDD 更改为 DGESVD,而无需修改 numpy 源?
预先感谢米莎
我有一个带有索引的数据框,有时包含具有相同索引值的行。现在我想对该数据帧进行切片并根据行索引设置值。
考虑以下示例:
import pandas as pd
df = pd.DataFrame({'index':[1,2,2,3], 'values':[10,20,30,40]})
df.set_index(['index'], inplace=True)
df1 = df.copy()
df2 = df.copy()
#copy warning
df1.iloc[0:2]['values'] = 99
print(df1)
df2.loc[df.index[0:2], 'values'] = 99
print(df2)
Run Code Online (Sandbox Code Playgroud)
df1 是预期结果,但给了我一个SettingWithCopyWarning。df2 似乎是文档建议的访问方式,但给了我错误的结果(因为重复索引)
是否有一种“正确”的方法可以在存在重复索引值的情况下正确设置这些值?
由于我之前描述的原因,我需要在 Python 中使用 LAPACK dgesvd 和 zgesvd 方法,而不是用 numpy 包装的方法。
有人指出,我可以使用 f2py 来创建我自己的 python 包。问题是,lapack 中的 dgesdd 调用了一堆其他方法,如 dbdsqr、dgelqf 以及一些 BLAS 例程,我不知道我应该如何处理。
谁能指出,在不必重新编译整个 lapack 库的情况下,创建 dgesvd python 模块的正确方法是什么?
非常感谢米莎
嗨,我是用f2py包裹LAPACK例行dgesvd,通过编译dgesvd.f文件和链接它反对llapack,如解释在这里
根据文档字符串,dgesvd 模块具有签名:
dgesvd - Function signature:
dgesvd(jobu,jobvt,m,n,a,s,u,vt,work,lwork,info,[lda,ldu,ldvt])
Required arguments:
jobu : input string(len=1)
jobvt : input string(len=1)
m : input int
n : input int
a : input rank-2 array('d') with bounds (lda,*)
s : input rank-1 array('d') with bounds (*)
u : input rank-2 array('d') with bounds (ldu,*)
vt : input rank-2 array('d') with bounds (ldvt,*)
work : input rank-1 array('d') with bounds (*)
lwork : input int
info : input int
Optional arguments:
lda := shape(a,0) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用SelectKBest示例的稍作修改的版本,但仍在继续获取ValueError(“ Unknown label type:%s”%repr(ys))
这是我的代码:
# Importing dependencies
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.datasets import load_iris
#The Example from:
#http://scikit-learn.org/stable/modules/feature_selection.html#univariate-feature-selection
iris = load_iris()
X, Y = iris.data, iris.target
print(X.shape, type(X), type(X[0,0]))
print(Y.shape, type(Y), type(Y[0]))
X_new = SelectKBest(chi2, k=2).fit_transform(X, Y)
#My toyproblem:
X = np.random.uniform(0,1, size=(5000, 10))
Y = np.random.uniform(0,1, size=(5000,))
#Type cast which might solve my problem by thi suggestion:
# /sf/ask/3174258531/
X=X.astype('float')
Y=Y.astype('float')
print(X.shape, type(X), type(X[0,0]))
print(Y.shape, type(Y), type(Y[0]))
X_new = …
Run Code Online (Sandbox Code Playgroud)