我想在Google地图上清除标记.
marker.setVisible(false)
和之间有什么区别marker.setMap(null)
?
但我不知道,哪个是对的?
javascript google-maps clear google-maps-api-3 google-maps-markers
psycopg2
在Python脚本结束时不关闭连接有什么后果?例如,请考虑以下代码段:
import psycopg2
psycopg2.connect("dbname=test")
Run Code Online (Sandbox Code Playgroud)
该脚本打开一个连接,但最后不会关闭它.执行结束时连接是否仍然打开?如果是这样,是否存在未关闭连接的问题?
Python在模块中有Counter类collections
。它是一个用于计算可散列对象的类。例如:
cnt = Counter()
cnt['Anna'] += 3
cnt['John'] += 2
cnt['Anna'] += 4
print(cnt)
=> Counter({'Anna': 7, 'John': 2})
print(cnt['Mario'])
=> 0
Run Code Online (Sandbox Code Playgroud)
Counter
Ruby 中的等价物是什么?
编辑:
该Counter
级还提供了以下的数学运算和辅助方法:
c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
c + d
=> Counter({'a': 4, 'b': 3})
c - d
=> Counter({'a': 2})
c.most_common(1)
=> ['a']
Run Code Online (Sandbox Code Playgroud) 我正在使用python绘制条形图matplotlib.pyplot
.该图表将包含大量条形图,每个条形图都有自己的标签.因此,标签重叠,并且它们不再可读.我希望标签以对角线方式显示,以便它们不会覆盖,例如在此图像中.
这是我的代码:
import matplotlib.pyplot as plt
N =100
menMeans = range(N)
ind = range(N)
ticks = ind
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, align = 'center')
ax.set_xticks(ind)
ax.set_xticklabels( range(N) )
plt.show()
Run Code Online (Sandbox Code Playgroud)
标签如何以对角线显示?
比较Ruby中两个Time对象的日期的最佳方法是什么?
我有两个对象,例如:
time_1 = Time.new(2012,12,10,10,10)
time_2 = Time.new(2012,12,11,10,10)
Run Code Online (Sandbox Code Playgroud)
在此示例中,日期比较应返回false.
否则,相同的日期,但不同的时间,应返回true:
time_1 = Time.new(2012,12,10,10,10)
time_2 = Time.new(2012,12,10,11,10)
Run Code Online (Sandbox Code Playgroud)
我试图使用.to_date
它适用于DateTime
对象,但它不受支持Time
.
我对在Python中进行套索回归技术感兴趣。但是,我想为算法加权输入数据。您能否建议一些可以考虑输入权重来执行套索回归的库?
我发现这里是scikit-learn
提供加权岭回归,但不是加权套索回归。
我正在使用该库scikit-learn
对各个样本进行权重岭回归。这可以通过以下方式完成:esimator.fit(X, y, sample_weight=some_array)
。直观上,我预计权重越大,相应样本的相关性就越大。
但是,我在以下 2-D 示例中测试了上述方法:
from sklearn import linear_model
import numpy
import matplotlib.pyplot as plt
#Data
x= numpy.array([[0], [1],[2]])
y= numpy.array([[0], [2],[2]])
sample_weight = numpy.array([1,1, 1])
#Ridge regression
clf = linear_model.Ridge(alpha = 0.1)
clf.fit(x, y, sample_weight = sample_weight)
#Plot
xp = numpy.linspace(-1,3)
yp=list()
for x_i in xp:
yp.append(clf.predict(x_i)[0,0])
plt.plot(xp,yp)
plt.hold(True)
x = list(x)
y = list(y)
plt.plot(x,y,'or')
Run Code Online (Sandbox Code Playgroud)
我运行此代码,然后再次运行它,将第一个样本的权重加倍:
sample_weight = numpy.array([2,1, 1])
Run Code Online (Sandbox Code Playgroud)
所得的线条远离权重较大的样本。这是违反直觉的,因为我预计权重较大的样本具有较大的相关性。
我是否错误地使用了该库,或者其中有错误?
我Mysql2
用来查询数据库ruby
.我通过以下方式初始化连接:
client = Mysql2::Client.new(:host => "localhost", :database => 'mydb', :username => "root")
Run Code Online (Sandbox Code Playgroud)
查询成功完成后,如何关闭客户端连接?如果我不关闭它,我很快就会达到最大可能的打开连接数.
感谢@joonty:
client.close
考虑一个类似于以下数据的表
column_a (boolean) | column_order (integer)
TRUE | 1
NULL | 2
NULL | 3
TRUE | 4
NULL | 5
FALSE | 6
NULL | 7
Run Code Online (Sandbox Code Playgroud)
我想编写一个查询,根据指定的顺序将每个NULL
值替换为列的先前值中的column_a
最后一个非NULL
值column_order
.结果应如下所示:
column_a (boolean) | column_order (integer)
TRUE | 1
TRUE | 2
TRUE | 3
TRUE | 4
TRUE | 5
FALSE | 6
FALSE | 7
Run Code Online (Sandbox Code Playgroud)
为简单起见,我们可以假设第一个值永远不为空.如果连续NULL
值不超过一个,则以下情况有效:
SELECT
COALESCE(column_a, lag(column_a) OVER (ORDER BY column_order))
FROM test_table
ORDER BY column_order;
Run Code Online (Sandbox Code Playgroud)
但是,上述内容对于任意数量的连续NULL
值都不起作用.什么是能够实现上述结果的Postgres查询?是否存在可以很好地扩展到大量行的高效查询?
我在Ruby中使用MySQL2来查询数据库.检查查询结果是否为空的直接方法是什么?代码如下:
require 'mysql2'
client = Mysql2::Client.new(:host => "localhost", :username => "root")
results = client.query("SELECT * FROM users WHERE group='githubbers'")
Run Code Online (Sandbox Code Playgroud) 我有一个数据集(X,Y) - > Z.因此,我想在Python中学习2D输入和输出Z之间的映射.
我知道底层函数不是线性的,因此我无法应用线性回归.由于输入数据只是2D,我想使用双变量样条.我实现了以下示例:
import numpy
from scipy import interpolate
X = [1,2,1,2]
Y = [1,2,2,1]
Z = [1,2,1,2]
Y = numpy.array(Y)
X = numpy.array(X)
Z = numpy.array(Z)
tck = interpolate.bisplrep(X,Y,Z)#,kx=5,ky=2)
print interpolate.bisplev(1.5,1.5,tck)
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码引发了以下错误:
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/fitpack.py", line 850, in bisplrep
TypeError: m >= (kx+1)(ky+1) must hold
Run Code Online (Sandbox Code Playgroud)
的问题是,在scipy
实施的二元花键 要求网格数据作为输入,而不是对输入序列的正常阵列,如X = [X1,X2,...]和Y = [Y1,Y2,...].由于我可以使用的数据类型,我无法构建网格,因为输入数据不会定期分配.
如何使用不是网格的输入数据进行双变量样条曲线?
如果不可能,还有另一种方法可以在Python中进行2D样条/多项式拟合/非线性回归吗?
RedHat是我的操作系统。默认情况下,它运行python2.6
。但是,我需要安装python2.7
。我在这篇文章之后安装了它。现在,当我运行时:
$ python -V
Python 2.7.5
Run Code Online (Sandbox Code Playgroud)
然而
$ sudo python -V
Python 2.6.6
Run Code Online (Sandbox Code Playgroud)
因此,在安装库时会出现问题。我尝试mysql-connector
通过以下方式安装:
但是,在python 2.7版本上未安装mysql-connector模块。如果我运行:
$ python
>>> import mysql.connector
ImportError: No module named mysql.connector
Run Code Online (Sandbox Code Playgroud)
如何在python版本2.7上也安装mysql模块?
编辑
附加信息:
$ which pip
/usr/bin/pip
$ sudo which pip
/usr/bin/pip
$ which easy_install
/opt/rh/python27/root/usr/bin/easy_install
$ sudo which easy_install
/usr/bin/easy_install
/usr/bin/ $ ls -al pip
-rwxr-xr-x. 1 root root 281 Feb 7 11:07 pip
/opt/rh/python27/root/usr/bin $ ls -al …
Run Code Online (Sandbox Code Playgroud) python ×7
ruby ×4
regression ×3
mysql2 ×2
postgresql ×2
scikit-learn ×2
bar-chart ×1
charts ×1
clear ×1
client ×1
connection ×1
database ×1
date ×1
datetime ×1
google-maps ×1
javascript ×1
matplotlib ×1
mysql ×1
mysql-python ×1
numpy ×1
orange ×1
pip ×1
psycopg2 ×1
python-2.7 ×1
scikits ×1
scipy ×1
spline ×1
sql ×1
time ×1