我想在我的ipython Notebook中运行一个bash脚本,并将输出保存为python变量中的字符串,以便进一步操作.基本上我想将bash魔法的输出传递给变量,例如输出如下:
%%bash
some_command [options] foo bar
Run Code Online (Sandbox Code Playgroud) 有没有比以下代码更有效的东西来交换numpy 1D数组的两个值?
input_seq = arange(64)
ix1 = randint(len(input_seq))
ixs2 = randint(len(input_seq))
temp = input_seq[ix2]
input_seq[ix2] = input_seq[ix1]
input_seq[ix1] = temp
Run Code Online (Sandbox Code Playgroud) 如何在python中解决旅行商问题?我没有找到任何库,应该有一种方法使用scipy函数进行优化或其他库.
我的hacky-extremelly-lazy-pythonic强制解决方案是:
tsp_solution = min( (sum( Dist[i] for i in izip(per, per[1:])), n, per) for n, per in enumerate(i for i in permutations(xrange(Dist.shape[0]), Dist.shape[0])) )[2]
Run Code Online (Sandbox Code Playgroud)
其中Dist(numpy.array)是距离矩阵.如果Dist太大,这将需要永远.
建议?
我有一个这样的数据框:
ID type value
1 A 8
2 A 5
3 B 11
4 C 12
5 D 1
6 D 22
7 D 13
Run Code Online (Sandbox Code Playgroud)
我想过滤数据框,以便我有一个唯一出现的“类型”属性(例如 A 只出现一次),如果有更多的行具有相同的“类型”值,我想选择具有更高值的行. 我想得到类似的东西:
ID type value
1 A 8
3 B 11
4 C 12
6 D 22
Run Code Online (Sandbox Code Playgroud)
我如何用熊猫做到这一点?
在scipy中,不支持使用数据拟合负二项分布(可能是因为scipy中的负二项式仅是离散的).
对于正态分布,我会这样做:
from scipy.stats import norm
param = norm.fit(samp)
Run Code Online (Sandbox Code Playgroud)
在任何其他库中是否有类似"准备使用"的功能?
我想获取一组熊猫数据框列的数字索引。
一列非常简单:
nonzero(df.columns.values == 'conditionA')
Run Code Online (Sandbox Code Playgroud)
但是有多个元素?我有一些有用的东西,但很冗长,很亲切:
df = pd.DataFrame(columns=['conditionF', 'conditionB', 'conditionA', 'conditionD', 'conditionC'])
cols_to_find = ['conditionA', 'conditionB', 'conditionC']
[i for i in range(len(df.columns.values)) if df.columns.tolist()[i] in cols_to_find ]
Run Code Online (Sandbox Code Playgroud)
更好的想法?
我在 networkx 中有一个有向图 G,我想得到它的最小生成树。我愿意:
T = nx.algorithms.minimum_spanning_tree( G.to_undirected() )
Run Code Online (Sandbox Code Playgroud)
这是无方向的,我想恢复方向,但我不知道该怎么做。我试过:
G[T.edges()]
Run Code Online (Sandbox Code Playgroud)
最后一行看起来非常pythonic,但这不是networkx的工作方式,显然......有谁知道怎么做?
换句话说:在给定(无向)边的情况下,如何获得有向树的子图?
在 statsmodels 中,普通最小二乘法实施了似然比检验
OLSResults.compare_lr_test(restricted)
Run Code Online (Sandbox Code Playgroud)
对于广义线性模型 (GLM) 来说,情况并非如此。我尝试复制 OLS 实现:
from scipy import stats
llf_full = results.llf
llf_restr = results_res.llf
df_full = results.df_resid
df_restr = results_res.df_resid
lrdf = (df_restr - df_full)
lrstat = -2*(llf_restr - llf_full)
lr_pvalue = stats.chi2.sf(lrstat, df=lrdf)
lr_pvalue
Run Code Online (Sandbox Code Playgroud)
它看起来很简单,但事实上它没有实现,这让我很怀疑。它是否正确?
我想使用 python 生成一些正在移动的二维几何对象(圆形、正方形等)的视频。
我怀疑解决方案可能是使用 pygame、piglet 等库进行渲染,然后使用其他库保存屏幕截图并附加到视频文件。
重要的是,我需要在不打开屏幕/窗口的情况下执行此操作;基本上 pyagme 或 Piglet 应该在某个缓冲区而不是屏幕上写入图像。
我使用 matplotlib 取得了一些成功,但我觉得它不是这个项目最合适的工具,特别是如果我想让图形更漂亮并且想要有一些运行速度快的东西。
编辑:我最终使用了 ffmpeg 等命令行工具
以下代码适合使用以下简化的广义线性模型: statsmodels
model = smf.glm('Y ~ 1', family=sm.families.NegativeBinomial(), data=df)
results = model.fit()
Run Code Online (Sandbox Code Playgroud)
这给出了系数和标准差:
coef stderr
Intercept 2.9471 0.120
Run Code Online (Sandbox Code Playgroud)
现在,我想以图形方式将变量Y(直方图)的实际分布与来自模型的分布进行比较。
但是我需要两个参数r
并p
对其进行评估stats.nbinom(r,p)
和绘制。
有没有办法从拟合结果中检索参数?如何绘制PMF?
当我尝试使用以下 python 代码计算马哈拉诺比斯距离时,我在结果中得到了一些 Nan 条目。您知道为什么会发生这种情况吗?我的 data.shape = (181, 1500)
from scipy.spatial.distance import pdist, squareform
data_log = log2(data + 1) # A log transform that I usually apply to my data
data_centered = data_log - data_log.mean(0) # zero centering
D = squareform( pdist(data_centered, 'mahalanobis' ) )
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
data_standard = data_centered / data_centered.std(0, ddof=1)
D = squareform( pdist(data_standard, 'mahalanobis' ) )
Run Code Online (Sandbox Code Playgroud)
还得到了nan。输入没有损坏,并且可以很好地计算其他距离,例如相关距离。由于某种原因,当我减少功能数量时,我就不再获得 Nans 了。例如,以下示例没有得到任何 Nan:
D = squareform( pdist(data_centered[:,:200], 'mahalanobis' ) )
D = squareform( pdist(data_centered[:,180:480], 'mahalanobis' ) )
Run Code Online (Sandbox Code Playgroud)
而其他人则得到 Nans:
D …
Run Code Online (Sandbox Code Playgroud) 关于过去发生的事件,我有成千上万的句子.例如
sentence1 = 'The Knights Templar are founded to protect Christian pilgrims in Jerusalem.'
sentence2 = 'Alfonso VI of Castile captures the Moorish Muslim city of Toledo, Spain.'
sentence3 = 'The Hindu Medang kingdom flourishes and declines.'
Run Code Online (Sandbox Code Playgroud)
我想将它们转换成表格的问题:
question1 = 'When were the Knights Templar founded to protect Christian pilgrims in Jerusalem?'
question2 = 'When did Alfonso VI of Castile capture the Moorish Muslim city of Toledo, Spain?'
question3 = 'When did the Hindu Medang kingdom flourish and decline?'
Run Code Online (Sandbox Code Playgroud)
我意识到这是一个复杂的问题,我的成功率为80%. …
python ×12
scipy ×5
numpy ×4
statistics ×4
statsmodels ×3
dataframe ×2
pandas ×2
distribution ×1
graph ×1
graphics ×1
ipython ×1
mahalanobis ×1
modeling ×1
models ×1
networkx ×1
nlp ×1
nltk ×1
optimization ×1
pygame ×1
pyglet ×1
python-2.7 ×1
string ×1
subgraph ×1
swap ×1
text ×1
video ×1