我尝试在Spyder上运行以下代码(Python 2.7.11):
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import tensorflow as tf
# settings
LEARNING_RATE = 1e-4
# set to 20000 on local environment to get 0.99 accuracy
TRAINING_ITERATIONS = 2000
DROPOUT = 0.5
BATCH_SIZE = 50
# set to 0 to train on all available data
VALIDATION_SIZE = 2000
# image number to output
IMAGE_TO_DISPLAY = 10
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误:
line 10
%matplotlib inline …Run Code Online (Sandbox Code Playgroud) 我想测试两个类型的对象是否Rc<Trait>包含具体类型的相同实例,因此我将指向内部对象的指针进行比较以Rc获得相等性.如果所有代码都驻留在同一个包中但在涉及多个包时失败,它似乎可以正常工作.
这是crate Rc::ptr_eq(mcve)的实现:
use std::rc::Rc;
pub trait ObjectInterface {}
pub type Object = Rc<ObjectInterface>;
pub type IntObject = Rc<i32>;
impl ObjectInterface for i32 {}
/// Test if two Objects refer to the same instance
pub fn is_same(left: &Object, right: &Object) -> bool {
let a = left.as_ref() as *const _;
let b = right.as_ref() as *const _;
let r = a == b;
println!("comparing: {:p} == {:p} -> {}", a, b, r);
r …Run Code Online (Sandbox Code Playgroud) 简而言之,问题是:有没有办法阻止Python查找当前范围之外的变量?
细节:
如果未在当前作用域中定义,Python会在外部作用域中查找变量定义.因此,在重构期间不小心时,这样的代码可能会破坏:
def line(x, a, b):
return a + x * b
a, b = 1, 1
y1 = line(1, a, b)
y2 = line(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
如果我重命名了函数参数,但忘了在函数体内重命名它们,代码仍会运行:
def line(x, a0, b0):
return a + x * b # not an error
a, b = 1, 1
y1 = line(1, a, b) # correct result by coincidence
y2 = line(1, 2, 3) # wrong result
Run Code Online (Sandbox Code Playgroud)
我知道从外部作用域中遮蔽名称是不好的做法.但是,为什么要这样做有几个原因:
有没有办法阻止Python查找当前范围之外的变量?(这样在第二个例子中访问a或 …
我正在尝试使用scikit-learn中包含的广义线性模型拟合方法拟合向量自回归(VAR)模型.线性模型的形式为y = X w,但系统矩阵X具有非常独特的结构:它是块对角线,并且所有块都是相同的.为了优化性能和内存消耗,模型可以表示为Y = BW,其中B是来自X的块,Y和W现在是矩阵而不是向量.LinearRegression,Ridge,RidgeCV,Lasso和ElasticNet类很容易接受后一种模型结构.但是,由于Y是二维的,因此适合LassoCV或ElasticNetCV失败.
我发现https://github.com/scikit-learn/scikit-learn/issues/2402 从这个讨论中,我假定LassoCV/ElasticNetCV的行为意图.除了手动实现交叉验证之外,有没有办法优化alpha/rho参数?
此外,scikit-learn中的贝叶斯回归技术也期望y是一维的.有没有办法解决?
注意:我使用scikit-learn 0.14(稳定)
python machine-learning linear-regression model-fitting scikit-learn
trait X {}
impl<T> X for T {}
impl X for u32 {} // conflicting implementation
Run Code Online (Sandbox Code Playgroud)
没有意外:X任何类型都实现了T,我们无法再次实现它u32.
令人惊讶的是,以下代码片段成功编译:
use std::fmt::Display;
pub trait Show {}
impl<T: Display> Show for T {}
impl Show for str {}
// These impls would cause "conflicting implementation" errors:
// impl<'a> Show for &'a str
// impl Show for String
fn main() {}
Run Code Online (Sandbox Code Playgroud)
我不希望这个代码编译,因为Display是为实现str,所以一般应IMPL实施Show的str,并与特定的IMPL冲突.
为什么不impl Show …
我们可以实现traits core::ops来定义我们类型的运算符的行为.特征本身用#[lang =...]属性注释,因此编译器知道哪些特征和操作符属于一起.
例如,Add基本类型的实现看起来像这样(宏从这里手动扩展和简化):
impl Add for i32 {
type Output = i32;
fn add(self, other: i32) -> i32 {
self + other
}
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,该实现在+内部使用运算符,可能会调用self.add(other),从而导致无限递归.显然,事情不会发生这种情况,因为像3 + 4(假设没有不断的折叠)这样的表达式可以完美地运行.
现在考虑这个特性的天真实现Add:
use std::ops::Add;
struct Foo;
impl Add for Foo {
type Output = Foo;
fn add(self, other: Foo) -> Foo {
self + other
}
}
fn main() {
let two_foo = Foo + Foo;
}
Run Code Online (Sandbox Code Playgroud)
编译器警告 …
最近,在我的项目中从Coveralls切换到Codecov覆盖率下降了几个百分点.这似乎是由于部分命中,在工作服中被视为命中,但在Codecov中被视为未命中.
这是一个代码示例:
class Struct(object): # hit
def __init__(self, content=None): # hit
if content is not None: # partial hit
self.content = content # missed
s = Struct() # hit
Run Code Online (Sandbox Code Playgroud)
据我所知,解释器完全评估了if语句.为什么它不算是一个打击呢?
我有一个形状512x512的数组,其中包含第i和第j位置0到100之间的数字.现在我想在其他地方选择数组[i,j] <25和零.我试过array = array[where(array<25)],它给了我一维数组,但我想要2D.请帮我解决这个问题.
我正在为OpenGL实现一个简单的GUI,主要是作为我自己的练习.我们的想法是拥有一个Gui类,其中每个实例都可以分配给不同的渲染目标(例如后缓冲区或纹理).GUI元素(小部件)仅分配给Gui类的一个实例.我想在GUI中存储元素是unique_ptr的典型用例.这是我想出的:
class Element {
public:
Element();
virtual ~Element();
static std::unique_ptr<Element> create_unique();
};
class Gui {
public:
typedef std::unique_ptr<Element> element_ptr;
Gui();
void addElement( element_ptr element );
void addElementRaw( Element* element );
private:
std::list<element_ptr> elements;
};
int main(void) {
Gui gui;
gui.addElementRaw( new Element() ); // 1
gui.addElement( Element::create_unique() ); // 2
gui.addElement( std::unique_ptr<Element>(new Element()) ); // 3
auto el = Element::create_unique();
gui.addElement( std::move(el) ); // 4
}
Run Code Online (Sandbox Code Playgroud)
我不希望GUI的潜在用户担心移动指针.但是,我想从API中明确说明GUI类拥有Element的所有权.
我对我的解决方案不满意.我想要的是(1)的简单性,同时从API清楚地知道gui现在拥有该元素.
我想使用matplotlib生成的图作为OpenGL中的纹理.我到目前为止遇到的matplotlib的OpenGL后端要么不成熟要么已经停止,所以我想避免它们.
我目前的方法是将数字保存到临时的.png文件中,我从中汇编纹理地图集.但是,我宁愿避免存储中间文件,而是直接从matplotlib获取像素数据.这有可能吗?
我正在寻找的答案是fig.canvas.print_to_buffer().乔的答案包含值得一试的其他选择.
python ×6
rust ×3
matplotlib ×2
c++ ×1
c++11 ×1
coverage.py ×1
ipython ×1
numpy ×1
opengl ×1
operators ×1
scikit-learn ×1
spyder ×1