假设我有一个Python Numpy数组a.
a = numpy.array([1,2,3,4,5,6,7,8,9,10,11])
Run Code Online (Sandbox Code Playgroud)
我想从这个长度为5的数组创建一个子序列矩阵,步长为3.结果矩阵因此如下所示:
numpy.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11]])
Run Code Online (Sandbox Code Playgroud)
实现这一点的一种可能方式是使用for循环.
result_matrix = np.zeros((3, 5))
for i in range(0, len(a), 3):
result_matrix[i] = a[i:i+5]
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法来实现这个Numpy?
我有三个类1(活动),0(不活动)和-1(未知)。我想在TensorFlow中构建一个模型,该模型在给定输入的情况下预测活动或不活动。以下是仅通过活动标签和非活动标签计算损失并忽略未知标签的正确方法吗?
logits = tf.reshape(logits, [-1])
labels = tf.reshape(labels, [-1])
index = tf.where(tf.not_equal(labels, tf.constant(-1, dtype=tf.float32)))
logits = tf.gather(logits, index)
labels = tf.gather(labels, index)
entropies = tf.nn.sigmoid_cross_entropy_with_logits(logits, labels)
loss = tf.reduce_mean(entropies)
Run Code Online (Sandbox Code Playgroud) 我有关于模式匹配的问题:
是否有可能以某种方式匹配(字符串++ [char] ++ anotherstring)?
我尝试过类似的东西:
f (s++";"++r) = s++r (the rhs is trivial, but its just for testing ;))
Run Code Online (Sandbox Code Playgroud)
但这会导致解析错误.
我编写了一些简单的haskell函数来计算图中给定顶点的邻居(见下文).它编译得很好,但是,当我运行时adj g 1,我收到以下错误:Couldn't match expected type `Int' against inferred type `Integer'
代码:
module Test where
import Prelude
import Data.List
type Node = Int
type Edge = (Int, Int)
type Graph = ([Node], [Edge])
g = ([1,2,3,4,5,6], [(1,2),(2,3),(2,4),(5,6)])
adj :: Graph -> Node -> [Node]
adj (vs, []) n = []
adj (vs,((s,e):es)) n | s==n = e:rec
| e==n = s:rec
| otherwise = rec
where
rec = adj (vs,es) n
Run Code Online (Sandbox Code Playgroud) 如何使用replaceAll()方法替换括号?例如,如果我有字符串,"[][]teststring]]]]"我想使用replaceAll()将其减少为"teststring".