我正在按照 的安装说明进行操作virtualenvwrapper
,描述为here。
我用过pip install virtualenvwrapper
,安装在路径上/home/.pyenv/shims/
。
但是当我运行命令时
source /home/.pyenv/shims/virtualenvwrapper.sh
,整个 Konsole 都会关闭。我之前把命令放在.bashrc
文件里,几乎把 Linux 弄坏了,因为 Konsole 打开后会立即崩溃。
我使用的是 Linux OpenSuse 和 Python 3.6.0 版。
任何想法可能导致崩溃?
我正在使用数据集 API 生成训练数据并将其分类为 NN 的批次。
这是我的代码的最小工作示例:
import tensorflow as tf
import numpy as np
import random
def my_generator():
while True:
x = np.random.rand(4, 20)
y = random.randint(0, 11)
label = tf.one_hot(y, depth=12)
yield x.reshape(4, 20, 1), label
def my_input_fn():
dataset = tf.data.Dataset.from_generator(lambda: my_generator(),
output_types=(tf.float64, tf.int32))
dataset = dataset.batch(32)
iterator = dataset.make_one_shot_iterator()
batch_features, batch_labels = iterator.get_next()
return batch_features, batch_labels
if __name__ == "__main__":
tf.enable_eager_execution()
model = tf.keras.Sequential([tf.keras.layers.Flatten(input_shape=(4, 20, 1)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(12, activation=tf.nn.softmax)])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
data_generator = my_input_fn()
model.fit(data_generator)
Run Code Online (Sandbox Code Playgroud)
该代码在 …
python tensorflow tensorflow-datasets tf.keras tensorflow2.0
我有一个数组数组,我试图在所有数组中找到最低的非零值。
minima = []
for array in K: #where K is my array of arrays (all floats)
if 0.0 in array:
array.remove(0.0)
minima.append(min(array))
print min(minima)
Run Code Online (Sandbox Code Playgroud)
这产生
AttributeError: 'numpy.ndarray' object has no attribute 'remove'
Run Code Online (Sandbox Code Playgroud)
我以为array.remove()
是删除元素的方法。我究竟做错了什么?
我正在使用Python 2.7.我有两个数组,A和B.要找到B中存在的A中元素的索引,我可以这样做
A_inds = np.in1d(A,B)
Run Code Online (Sandbox Code Playgroud)
我还想得到A中存在的B中元素的索引,即使用上面的代码找到的相同重叠元素的B中的索引.
目前我再次运行同一行,如下所示:
B_inds = np.in1d(B,A)
Run Code Online (Sandbox Code Playgroud)
但这个额外的计算似乎应该是不必要的.是否有更高效的计算方法来获取A_inds
和B_inds
?
我愿意使用列表或数组方法.
这是一个示例数据框:
import pandas as pd
df = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3],
'value':[42, 89, 250, 31, 130, 108, 107, 93]})
ID value
0 1 42
1 1 89
2 1 250
3 2 31
4 2 130
5 2 108
6 3 107
7 3 93
Run Code Online (Sandbox Code Playgroud)
对于每个 ID,我想提取值大于 100 的条目。
使用groupby
我可以获得以下内容
grouped = df.groupby('ID')
for name, group in grouped:
print(name, group)
1 ID value
0 1 42
1 1 89
2 1 250
2 ID value
3 2 31
4 2 130 …
Run Code Online (Sandbox Code Playgroud) 我有一个阵列A.
A = [5,2,8,14,6,13]
Run Code Online (Sandbox Code Playgroud)
我想得到一个数组,其中每个元素被添加到每个其他元素,所以前五个元素将是5 +每个元素,然后接下来的四个将是2 +每个元素等.所以结果将是
B = [7,13,19,11,18, 10,16,8,15, 22,14,21, 20,27, 19]
Run Code Online (Sandbox Code Playgroud)
在不使用for循环的情况下,最快的方法是什么?
注意:我试图解决的问题涉及大布尔数组而不是整数,实际操作是布尔'和',而不仅仅是加法.为了便于解释,我简化了问题.到目前为止,我一直在使用for循环,但我正在寻找更快的替代方案.
python ×6
arrays ×2
numpy ×2
indices ×1
pandas ×1
performance ×1
python-3.6 ×1
tensorflow ×1
tf.keras ×1