我需要模拟MATLAB函数find,它返回数组非零元素的线性索引.例如:
>> a = zeros(4,4)
a =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> a(1,1) = 1
>> a(4,4) = 1
>> find(a)
ans =
1
16
Run Code Online (Sandbox Code Playgroud)
numpy具有类似的功能nonzero,但它返回索引数组的元组.例如:
In [1]: from numpy import *
In [2]: a = zeros((4,4))
In [3]: a[0,0] = 1
In [4]: a[3,3] = 1
In [5]: a
Out[5]:
array([[ 1., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., …Run Code Online (Sandbox Code Playgroud) 我有一个使用Numpy的Python 3.2系统,但我让Homebrew升级到Python 3.3,所以我必须再次安装所有软件包.这对于失败pip3 install numpy与此非常大的输出.
你能建议解决吗?
或者,如何恢复旧的工作安装?
我正在寻找一种pythonic和简洁的方法来选择.csv文件中的列并存储列的所有单元格,例如列表.
import csv
with open("/path/to/file.csv","r") as csvfile:
reader = csv.DictReader(csvfile, delimiter=";")
# TODO: select column for key "foo"
# TODO: select column for key "bar"
# TODO:store "foo" data in list
# TODO: store "bar" data in list
Run Code Online (Sandbox Code Playgroud) 我正在学习D语言,因为我对它支持并行性感兴趣.这是我项目中的并行代码段:
import std.parallelism;
foreach (node v; taskPool.parallel(std.range.iota(z))) {
// call here
handle(v);
}
Run Code Online (Sandbox Code Playgroud)
如何控制并行工作的线程数?是否有相当于OpenMP的功能omp_set_num_threads?
我在每个ssh工作的机器上运行ipython交互式工作.我可以从IPython启动长时间运行的Python函数,切断ssh连接并稍后重新登录到IPython会话中以观察结果吗?
我需要一些简洁的帮助,首先在以下操作的熊猫中有效配方:
给定格式的数据框架
id a b c d
1 0 -1 1 1
42 0 1 0 0
128 1 -1 0 1
Run Code Online (Sandbox Code Playgroud)
构造格式的数据框:
id one_entries
1 "c d"
42 "b"
128 "a d"
Run Code Online (Sandbox Code Playgroud)
也就是说,列"one_entries"包含原始帧中的条目为1的列的连接名称.
我刚刚发现了scikit-learn的流水线功能,我发现它对于在训练我的模型之前测试预处理步骤的不同组合非常有用。
管道是实现fit和transform方法的对象链。现在,如果我想添加一个新的预处理步骤,我曾经编写一个继承自sklearn.base.estimator. 但是,我认为必须有一种更简单的方法。我真的需要将我想应用到 estimator 类中的每个函数都包装起来吗?
例子:
class Categorizer(sklearn.base.BaseEstimator):
"""
Converts given columns into pandas dtype 'category'.
"""
def __init__(self, columns):
self.columns = columns
def fit(self, X, y):
return self
def transform(self, X):
for column in self.columns:
X[column] = X[column].astype("category")
return X
Run Code Online (Sandbox Code Playgroud) matplotlib.pyplot.figure()
....
seaborn.despine(offset=10)
Run Code Online (Sandbox Code Playgroud)
seaborn 的功能despine使轴脊从绘图中偏移,看起来相当不错。该函数需要在绘制图形的代码之后调用。然而,我希望这是任何情节的默认设置,至少是任何seaborn情节。如何配置?
或者,如何将其作为绘图上下文的一部分?
我试图在 PySpark 中拼凑一个二元计数程序,它采用一个文本文件并输出每个适当二元的频率(句子中的两个连续单词)。
from pyspark.ml.feature import NGram
with use_spark_session("Bigrams") as spark:
text_file = spark.sparkContext.textFile(text_path)
sentences = text_file.flatMap(lambda line: line.split(".")) \
.filter(lambda line: len(line) > 0) \
.map(lambda line: (0, line.strip().split(" ")))
sentences_df = sentences.toDF(schema=["id", "words"])
ngram_df = NGram(n=2, inputCol="words", outputCol="bigrams").transform(sentences_df)
Run Code Online (Sandbox Code Playgroud)
ngram_df.select("bigrams") 现在包含:
+--------------------+
| bigrams|
+--------------------+
|[April is, is the...|
|[It is, is one, o...|
|[April always, al...|
|[April always, al...|
|[April's flowers,...|
|[Its birthstone, ...|
|[The meaning, mea...|
|[April comes, com...|
|[It also, also co...|
|[April begins, be...|
|[April …Run Code Online (Sandbox Code Playgroud) 在我的基于Dash的应用程序中,一个按钮会触发一个长时间运行的计算。在结果尚未出现时显示加载动画,并使按钮处于非活动状态,以便在计算完成之前不会再次单击它,这不是很好吗?
我正在使用Bulma进行 UI 设计,并希望button is-loading为此目的使用CSS 类。
我的第一个想法是有两个回调:一个由单击按钮触发以将按钮设置为is-loading,另一个由输出更改触发以将其设置回正常。
@app.callback(
Output('enter-button', 'className'),
[
Input('graph', 'figure')
],
)
def set_trend_enter_button_loading(figure_changed):
return "button is-large is-primary is-outlined"
@app.callback(
Output('enter-button', 'className'),
[
Input('enter-button', 'n_clicks')
],
)
def set_trend_enter_button_loading(n_clicks):
return "button is-large is-primary is-outlined is-loading"
Run Code Online (Sandbox Code Playgroud)
显然它不能那样工作:
dash.exceptions.CantHaveMultipleOutputs:
You have already assigned a callback to the output
with ID "enter-button" and property "className". An output can only have
a single callback function. Try combining your inputs and
callback functions together …Run Code Online (Sandbox Code Playgroud) python ×8
numpy ×2
python-3.x ×2
apache-spark ×1
css ×1
csv ×1
d ×1
dashboard ×1
data-munging ×1
data-science ×1
dataframe ×1
homebrew ×1
ipython ×1
matlab ×1
matplotlib ×1
openmp ×1
pandas ×1
plot ×1
plotly-dash ×1
pyspark ×1
pyspark-sql ×1
scikit-learn ×1
seaborn ×1
shell ×1
ssh ×1