小编sta*_*ane的帖子

在私人网站上托管交互式 jupyter 笔记本

我目前使用 Wordpress 运行一个个人网站(但托管在 siteground 上),这是一组工程学习指南。我想使这些学习指南具有交互性(即基于滑块刷新图形,进行基本计算以指示设计是否有效,因此我需要 numpy)。一位朋友建议我为此目的使用 Jupyter 笔记本,因为您既可以渲染 LaTeX(我目前使用 Mathjax 和 Wordpress 来完成),也可以使用 Bokeh 或 Plotly 获得我想要的交互式图形类型。

虽然我已经看过在特定服务器上共享笔记本的教程,但我所追求的是让其他人能够在他们的浏览器中运行我的笔记本(只读),笔记本是私人托管的。

我仍然不确定 Jupyter 是否是实现我想要的正确途径,所以我愿意接受其他建议(有人也推荐使用 Julia,但我看到的例子较少)。

python web-hosting python-interactive ijulia-notebook jupyter-notebook

9
推荐指数
1
解决办法
7278
查看次数

如何使用Homebrew安装Octave 4.2文档?

我尝试使用命令行安装Octave(4.2)

brew install octave
Run Code Online (Sandbox Code Playgroud)

但这不会安装文档.当我运行八度音程时,我收到以下错误:

octave:1> doc
error: doc: unable to find the Octave info manual, Octave installation is incomplete    
Run Code Online (Sandbox Code Playgroud)

一些谷歌搜索表明我应该跑

brew install octave --with-docs
Run Code Online (Sandbox Code Playgroud)

但这会回来

Warning: octave: this formula has no --with-docs option so it will be ignored!
Run Code Online (Sandbox Code Playgroud)

如何从命令行安装Octave文档?

installation homebrew command-line octave

5
推荐指数
1
解决办法
177
查看次数

numpy/scipy 中非线性函数的数值梯度

我正在尝试在 numpy 中实现数值梯度计算,以用作 cyiopt 中梯度的回调函数。我对 numpy 梯度函数的理解是,它应该返回基于有限不同近似值在某个点计算的梯度。

我不明白如何用这个模块实现非线性函数的梯度。给出的示例问题似乎是一个线性函数。

>>> f = np.array([1, 2, 4, 7, 11, 16], dtype=np.float)
>>> np.gradient(f)
array([ 1. ,  1.5,  2.5,  3.5,  4.5,  5. ])
>>> np.gradient(f, 2)
array([ 0.5 ,  0.75,  1.25,  1.75,  2.25,  2.5 ])
Run Code Online (Sandbox Code Playgroud)

我的代码片段如下:

import numpy as np

# Hock & Schittkowski test problem #40
x = np.mgrid[0.75:0.85:0.01, 0.75:0.8:0.01, 0.75:0.8:0.01, 0.75:0.8:0.01]
# target is evaluation at x = [0.8, 0.8, 0.8, 0.8]
f = -x[0] * x[1] * x[2] * …
Run Code Online (Sandbox Code Playgroud)

python optimization numpy scipy numerical-methods

4
推荐指数
1
解决办法
5508
查看次数

Scipy.optimize.minimize 目标函数 ValueError

我正在使用 scipy.optimize.minimize 来解决具有 9 个自由变量的小型优化问题。我的目标函数基本上是另一个函数的包装器,如果我评估我的目标函数,返回类型是“numpy.float32”...这是一个标量?但是,在尝试使用最小化函数时出现以下错误:

raise ValueError("Objective function must return a scalar")
ValueError: Objective function must return a scalar
Run Code Online (Sandbox Code Playgroud)

是否不可能将目标函数包裹在另一个函数中?其他函数参数是全局声明的,但如果这不行,我可以将它们硬编码到 beam_shear 函数中。

相关代码片段:

from numpy import array, shape, newaxis, isnan, arange, zeros, dot, linspace
from numpy import pi, cross, tile, arccos,sin, cos, sum, atleast_2d, asarray, float32, ones

from numpy import sum, reshape
from scipy.optimize import minimize

def normrow(A):
    A = atleast_2d(asarray(A, dtype=float32))
    return (sum(A ** 2, axis=1) ** 0.5).reshape((-1, 1))

def beam_shear(xyz, pt0, pt1, pt2, x):

    # will not work …
Run Code Online (Sandbox Code Playgroud)

python optimization numpy scipy

4
推荐指数
1
解决办法
1万
查看次数

GatsbyJS 网站非页面组件中的 GraphQL 查询投诉

我正在尝试向gatsby-starter-hero-blog添加新模板,但我对新模板的 GraphQL 查询被拒绝:

warning The GraphQL query in the non-page component 
"/Users/mc/workspaces/mc/src/templates/NoteBookTemplate.js" will not be run.
Queries are only executed for Page or Layout components. Instead of a query,
co-locate a GraphQL fragment and compose that fragment into the query (or other
fragment) of the top-level page or layout that renders this component. 
For more
info on fragments and composition see: 
http://graphql.org/learn/queries/#fragments
Run Code Online (Sandbox Code Playgroud)

文件夹结构如下:

--src
  --components
  --images
  --pages
  --templates
    --CategoryTemplate.js
    --NotebookTemplate.js
    --PageTemplate.js
    --PostTemplate.js     
  --theme
  --utils
Run Code Online (Sandbox Code Playgroud)

NotebookTemplate.js 是我添加的新模板(用于使用 Nteract 的 …

reactjs graphql graphql-js jupyter-notebook gatsby

4
推荐指数
1
解决办法
3575
查看次数

np.where和掩码数组

由于我在stackoverflow上获得了一些帮助,因此我正在使用蒙版数组,但是我在使用np.where评估蒙版数组时遇到了问题。

我的蒙版数组是:

m_pt0 = np.ma.masked_array([1, 2, 3, 0, 4, 7, 6, 5],
                           mask=[False, True, False, False,
                                 False, False, False, False])
Run Code Online (Sandbox Code Playgroud)

并打印如下:

In [24]: print(m_pt0)
[1 -- 3 0 4 7 6 5]
Run Code Online (Sandbox Code Playgroud)

我正在寻找m_pt0中的索引,其中m_pt0 = 0,我希望

np.where(0 == m_pt0)
Run Code Online (Sandbox Code Playgroud)

会返回:

(array([3]))
Run Code Online (Sandbox Code Playgroud)

但是,尽管戴了口罩(或因为?),我反而得到了

(array([1, 3]),)
Run Code Online (Sandbox Code Playgroud)

使用掩码的全部目的是避免访问“空白”的索引,因此如何使用where(或其他函数)仅检索未掩码且与我的布尔条件匹配的索引。

python arrays numpy boolean-operations masked-array

2
推荐指数
1
解决办法
2170
查看次数

就地插入列表(或数组)

我在Python中运行一个脚本,我需要在某些索引位置将数字插入到数组(或列表)中.问题是,当我插入新数字时,索引位置无效.是否有一种巧妙的方法可以同时在索引位置插入新值?或者是我添加时增加索引号(该对的第一个值)的唯一解决方案?

示例测试代码段:

original_list = [0, 1, 2, 3, 4, 5, 6, 7]
insertion_indices = [1, 4, 5]
new_numbers = [8, 9, 10]
pairs = [(insertion_indices[i], new_numbers[i]) for i in range(len(insertion_indices))]

for pair in pairs:
    original_list.insert(pair[0], pair[1])
Run Code Online (Sandbox Code Playgroud)

结果是:

[0, 8, 1, 2, 9, 10, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)

而我想:

[0, 8, 1, 2, 3, 9, 4, 10, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)

python arrays indexing numpy list-comprehension

2
推荐指数
2
解决办法
147
查看次数

使用索引数组进行高效循环

如果我有一个包含所有唯一顺序值的索引数组,例如:

index_array = array([0, 4, 2, 5, 6, 1, 3, 7, 8])
Run Code Online (Sandbox Code Playgroud)

使用相应的值数组:

value_array = array([0, 400, 200, 500 600, 100, 300, 700, 800])
Run Code Online (Sandbox Code Playgroud)

是否有可能按顺序循环索引数组,这样我就可以了

array([0, 100, 200, 300, 400, 500, 600, 700, 800])
Run Code Online (Sandbox Code Playgroud)

我需要按顺序循环索引数组(即0,1,2,3,4 ......)和相应的值(即0,100,200,300,400).这些值不按顺序的原因是因为我细分了边,这意味着新的边被添加到索引数组的末尾(使用vstack),而不是在适当的点插入到索引数组中.

伪代码(如果我打印出值),将是这样的:

for point in sorted(index_array):

    print sorted(point(value_array))
Run Code Online (Sandbox Code Playgroud)

生产:

0

100

200

300
Run Code Online (Sandbox Code Playgroud)

这对内存是多么敏感(我猜我需要使用numpy.where)在循环之前重新排序是不是更好的做法,还是循环乱序的性能成本?

python indexing for-loop numpy

1
推荐指数
1
解决办法
99
查看次数