我有一个现有的Flask应用程序,我希望有一个到另一个应用程序的路由.更具体地说,第二个应用程序是Plotly Dash应用程序.如何在现有的Flask应用程序中运行我的Dash应用程序?
@app.route('/plotly_dashboard')
def render_dashboard():
# go to dash app
Run Code Online (Sandbox Code Playgroud)
我还尝试添加一个到Dash实例的路由,因为它是一个Flask应用程序,但我收到错误:
AttributeError: 'Dash' object has no attribute 'route'
Run Code Online (Sandbox Code Playgroud) 当我尝试使用virtualenv venv来自终端的命令使用python创建虚拟环境时,我收到以下错误:
Using base prefix '/Users/zacharythomas/anaconda3'
New python executable in /Users/zacharythomas/venv/bin/python
dyld: Library not loaded: @rpath/libpython3.6m.dylib
Referenced from: /Users/zacharythomas/venv/bin/python
Reason: image not found
ERROR: The executable /Users/zacharythomas/venv/bin/python is not functioning
ERROR: It thinks sys.prefix is '/Users/zacharythomas' (should be '/Users/zacharythomas/venv')
ERROR: virtualenv is not compatible with this system or executable
Run Code Online (Sandbox Code Playgroud)
我不是第一个遇到类似错误的人 - 我尝试按照这个答案的建议并运行:
gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete
Run Code Online (Sandbox Code Playgroud)
这没有用.也没有sudo virtualenv venv像超级用户一样运行命令.
我接下来应该调查什么?
我在R中有一个停用词的字符向量:
stopwords = c("a" ,
"able" ,
"about" ,
"above" ,
"abst" ,
"accordance" ,
...
"yourself" ,
"yourselves" ,
"you've" ,
"z" ,
"zero")
Run Code Online (Sandbox Code Playgroud)
假设我有字符串:
str <- c("I have zero a accordance")
如何删除我定义的停用词str?
我认为gsub或其他grep工具可能是一个很好的选择,尽管其他建议是受欢迎的.
双方!并%允许您从一个Jupyter笔记本运行shell命令.
%提供由IPython的内核,并允许您运行"神奇的命令",其中许多包括知名的shell命令.!由Jupyter提供,允许shell命令在单元格内运行.
我没有找到太多的比较两者,并且对于简单的shell命令等cd,我看到的主要区别%是交互式的,实际上会在笔记本中改变你在shell 中的位置.
在考虑在Jupyter笔记本中使用哪个符号用于shell命令时,还有其他的对比点或规则吗?
为什么我们安装scikit-learn包:
conda install scikit-learn
Run Code Online (Sandbox Code Playgroud)
但是然后使用名称在脚本中导入(模块)包sklearn,例如:
from sklearn import x
Run Code Online (Sandbox Code Playgroud) 我想在Google工作表中写一个公式,以便可以附加到按钮上,以使1每次单击该单元格中的值都会增加。
这是我在该主题上能找到的最好的。
我试图这样做,但无济于事。我正在使用Windows 7和Chrome。
(如果答案确实是R如何使用二进制数来存储小小数,那么仍然会喜欢听到有关为什么会发生这种情况的人的详细信息)
我用R来计算总和,特别是这个:
这是我的R代码:
r = 21 #Number of times the loop executes
n = 52
sum = 0 #Running total
for(k in 0:r){
sum = sum + (1-(choose(2^(k), n)*(factorial(n)/(2^(n*k)))))
print(sum)
}
Run Code Online (Sandbox Code Playgroud)
如果你看一下输出,你会注意到:
[1] 1
[1] 2
...
[1] 11.71419
[1] 11.71923
[1] 11.72176
[1] 12.72176
[1] 13.72176
Run Code Online (Sandbox Code Playgroud)
为什么在第19次迭代后它开始递增1?
还有其他免费提供的计算引擎更适合这项任务吗?
我正在使用python 2.7获取数据框的数字列,data并将其设为具有日期索引的单个对象(系列),该日期是的另一列data。
new_series = pd.Series(data['numerical_column'] , index=data['dates'])
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我NaN在系列中得到了一堆值:
dates
1980-01-31 NaN
1980-02-29 NaN
1980-03-31 NaN
1980-04-30 NaN
1980-05-31 NaN
1980-06-30 NaN
...
Run Code Online (Sandbox Code Playgroud)
为什么我的numerical_data价值观消失了?
我知道通过执行以下操作显然可以实现此目标,尽管我很好奇为什么最初的方法失败了。
new_series = data.set_index('dates')['numerical_column']
Run Code Online (Sandbox Code Playgroud) 我的朋友和我执行的代码这一行Python 2和Python 3:
import numpy as np
mat = np.array([[1,0,0],[-1,3,3],[1,2,2]])
np.linalg.inv(mat)
Run Code Online (Sandbox Code Playgroud)
返回:
array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.50119988e+16, 6.00479950e+15, -9.00719925e+15],
[ -1.50119988e+16, -6.00479950e+15, 9.00719925e+15]])
Run Code Online (Sandbox Code Playgroud)
这是奇怪的给定:
np.linalg.matrix_rank(mat)
Run Code Online (Sandbox Code Playgroud)
返回2,从而表明矩阵不可逆。
我从这个线程中了解到这可能是由于 numpy 和 python 处理浮点数的方式,尽管我的矩阵由整数组成。
是否有特殊原因mat会破坏 numpy 的反向实现?
已经提出了这个问题的变体(请参阅这个问题),但我还没有找到一个好的解决方案,这似乎是groupbyPandas 中的常见用例。
假设我有数据框lasts并且分组依据user:
lasts = pd.DataFrame({'user':['a','s','d','d'],
'elapsed_time':[40000,50000,60000,90000],
'running_time':[30000,20000,30000,15000],
'num_cores':[7,8,9,4]})
Run Code Online (Sandbox Code Playgroud)
我有这些我想要应用的函数groupby_obj(这些函数的作用并不重要,我编写了它们,只需知道它们需要数据框中的多个列):
def custom_func(group):
return group.running_time.median() - group.num_cores.mean()
def custom_func2(group):
return max(group.elapsed_time) -min(group.running_time)
Run Code Online (Sandbox Code Playgroud)
我可以将apply这些函数中的每一个单独地添加到数据帧,然后合并生成的数据帧,但这似乎效率低下,不优雅,我想必须有一个单行解决方案。
我还没有真正找到一个,尽管这篇博客文章(在页面底部搜索“创建一个函数来获取一组统计数据”)建议将这些函数作为字典包装到一个函数中:
def get_stats(group):
return {'custom_column_1': custom_func(group), 'custom_column_2':custom_func2(group)}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行代码时,我得到的是一列字典结果,groupby_obj.apply(get_stats)而不是列:
user
a {'custom_column_1': 29993.0, 'custom_column_2'...
d {'custom_column_1': 22493.5, 'custom_column_2'...
s {'custom_column_1': 19992.0, 'custom_column_2'...
dtype: object
Run Code Online (Sandbox Code Playgroud)
实际上,我想使用一行代码来获得更接近此数据框的内容:
user custom_column_1 custom_column_2
a 29993.0 10000
d 22493.5 75000
s 19992.0 30000
Run Code Online (Sandbox Code Playgroud)
关于改进此工作流程的建议?
我是这方面的新手。我想编写一个可以从命令行执行的脚本,以在 Heroku 托管的 PostgreSQL 数据库上运行查询。
是的,我有一个具有可执行权限的脚本script.sh,如下所示:
echo "Starting pull from postgres ..."
heroku pg:psql <db> --app <app-name>
\copy (<query>) to 'file.csv' WITH CSV
\q
echo "Done!"
Run Code Online (Sandbox Code Playgroud)
和命令运行良好,但是,一旦 Heroku 启动,脚本就不再echo注入heroku ...命令。只有在我手动关闭 Heroku 应用程序后,它才会注入最后三行。
我知道这是一个 bash 脚本,一旦 Heroku 打开,它不会输入 postgreSQL 命令,但有没有办法做到这一点?
我感觉这可能涉及到连接到 Heroku 并在一行中提交查询——我搜索了Heroku 的文档,但没有看到任何有帮助的内容。
我有以下代码:
stuart = 0
kevin = 0
for itr, char in enumerate(word_list):
if char in "aeiou":
kevin += len(word_list) - itr
else:
stuart += len(word_list) - itr
Run Code Online (Sandbox Code Playgroud)
我可以将if/else语句写成三元运算符吗?
我尝试了以下无济于事:
(kevin if (char in "aeiou") else stuart) += len(word_list) - itr
Run Code Online (Sandbox Code Playgroud)
和
kevin += len(word_list) - itr if char in "aeiou" else stuart += len(word_list) - itr
Run Code Online (Sandbox Code Playgroud)
有没有办法将其作为三元运算符编写为更紧凑的代码?
我有以下Python 2.7代码:
def average_rows2(mat):
'''
INPUT: 2 dimensional list of integers (matrix)
OUTPUT: list of floats
Use map to take the average of each row in the matrix and
return it as a list.
Example:
>>> average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]])
[4.75, 6.25]
'''
return map(lambda x: sum(x)/float(len(x)), mat)
Run Code Online (Sandbox Code Playgroud)
当我使用iPython笔记本在浏览器中运行它时,我得到以下输出:
[4.75, 6.25]
Run Code Online (Sandbox Code Playgroud)
但是,当我在命令行(Windows)上运行代码的文件时,我收到以下错误:
>python -m doctest Delete.py
**********************************************************************
File "C:\Delete.py", line 10, in Delete.average_rows2
Failed example:
average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]])
Expected:
[4.75, 6.25] …Run Code Online (Sandbox Code Playgroud) python ×9
dataframe ×2
pandas ×2
r ×2
flask ×1
group-by ×1
heroku ×1
numpy ×1
plotly-dash ×1
postgresql ×1
precision ×1
python-2.7 ×1
python-3.x ×1
scikit-learn ×1
series ×1
shell ×1
sum ×1
virtualenv ×1