我想使用numpy数组连接'列'向量,但是因为numpy默认将所有数组视为行向量,np.hstack
并且np.concatenate
沿任何轴都没有帮助(并且没有np.transpose
按预期进行).
a = np.array((0, 1))
b = np.array((2, 1))
c = np.array((-1, -1))
np.hstack((a, b, c))
# array([ 0, 1, 2, 1, -1, -1]) ## Noooooo
np.reshape(np.hstack((a, b, c)), (2, 3))
# array([[ 0, 1, 2], [ 1, -1, -1]]) ## Reshaping won't help
Run Code Online (Sandbox Code Playgroud)
一种可能性(但过于繁琐)是
np.hstack((a[:, np.newaxis], b[:, np.newaxis], c[:, np.newaxis]))
# array([[ 0, 2, -1], [ 1, 1, -1]]) ##
Run Code Online (Sandbox Code Playgroud)
还有更好的方法吗?
我试图进行矢量化(同意,不是最有效的方法来做到这一点,但我的问题是在装饰器上使用)以下函数
@np.vectorize
def diff_if_bigger(x, y):
return y - x if y > x else 0
x = np.array([5.6, 7.0])
y = 8
diff_if_bigger(x, y)
# outputs array([2, 1]) which is not what I want
Run Code Online (Sandbox Code Playgroud)
编辑:重启IPython后,输出正常.
任何人都可以解释为什么结果diff_if_bigger
变成一个数组,np.int
即使第一个参数x在这里是一个aray np.float
,与文档中的内容相反?
现在,我想强制浮动输出,所以我这样做了
@np.vectorize('np.float')
def diff_if_bigger(x, y):
return y - x if y > x else 0
# Error !!
# TypeError: Object is not callable.
@np.vectorize(otypes='np.float')
def diff_if_bigger(x, y):
return y - x if y > x else …
Run Code Online (Sandbox Code Playgroud) numpy中奇怪的行为(bug ??)与文档相反,以下代码给出了在fmax中遇到的RuntimeWarning:invalid值
a = np.random.uniform(0.1, 0.4, (5, 5))
b = np.random.uniform(0, 3.5, (5, 5))
b[0, 0] = np.nan
c = np.fmax(a, b) # Same problem with c = np.maximum(a, b)
Run Code Online (Sandbox Code Playgroud)
我被卡住了,因为我需要在我的阵列中使用这些NaN,现在我的功能在这个该死的警告中停止在iPython中(好吧,他们真的不会停止,但它相当烦人)
编辑:
numpy 1.6.1
ipython 0.13.1
我查看了Fira Code,我想与其中一个受支持的编辑器一起试用.所以我推出了RStudio(Win框上的版本0.99.491)并将字体设置为Fira Code但是......没有.那么如何在RStudio中启用字体连字?
编辑:下面接受的答案中的技巧仍然适用于RStudio版本1.0.44.我仍然希望有一个简单的按钮来启用它.
查看GNU make manual,空配方和无配方之间究竟有什么区别(例如,请参阅没有配方或先决条件的规则中的一个实例)?更重要的是,什么时候应该使用/避免这两种食谱中的每一种?
在我目前的理解中,应该始终使用
target: ;
Run Code Online (Sandbox Code Playgroud)
因为有时一个隐含的规则有可能会破坏一个人的目的,没有规则。
SO上的一个帖子说扩展std
是UB(好吧,除非你当然是标准作者).但不时,std
幸福地延长.什么时候可以这样做?
下面什么是复制和交换习惯用法以及如何为我的班级提供交换功能,我尝试实现交换功能,如后面接受的答案选项2(具有调用成员函数的自由函数)而不是直接友好前链接中的自由功能.
但是以下不编译
#include <iostream>
// Uncommenting the following two lines won't change the state of affairs
// class Bar;
// void swap(Bar &, Bar &);
class Bar {
public:
Bar(unsigned int bottles=0) : bottles(bottles) { enforce(); } // (1)
Bar(Bar const & b) : bottles(b.bottles) { enforce(); } // (1)
Bar & operator=(Bar const & b) {
// bottles = b.bottles;
// enforce();
// Copy and swap idiom (maybe overkill in this example)
Bar tmp(b); // …
Run Code Online (Sandbox Code Playgroud) c++ copy-constructor assignment-operator copy-and-swap c++11
是否有可能/如下面的示例中那样通过一系列运算符进行迭代?
a, b = 5, 7
for op in (+, -, *, /):
print(a, str(op), b, a op b)
Run Code Online (Sandbox Code Playgroud)
一种可能的用例是在某种抽象数据类型上重载各种运算符的情况下对这些运算符的实现进行测试。
在任何级别的后代类中,显式标记为虚拟是否都会覆盖?
class Base {
// ...
protected:
virtual void to_be_derived() const; // First level to introduce this virtual function
};
class LevelOne : public Base {
// ...
protected:
// virtual??
void to_be_derived() const;
};
class LevelTwo : public levelOne {
// ...
protected:
// virtual??
void to_be_derived() const;
};
Run Code Online (Sandbox Code Playgroud)
我没有看到Prefixing虚拟关键字覆盖了我的问题的覆盖方法.特别是,其中一个答案已更新,以反映c ++ 11的当前用法,特别override
是我不知道的关键字!
编辑:我宁愿接受关于post-c ++ 11代码的链接问题的另一个答案.