我写了以下脚本:
import numpy
d = numpy.array([[1089, 1093]])
e = numpy.array([[1000, 4443]])
answer = numpy.exp(-3 * d)
answer1 = numpy.exp(-3 * e)
res = answer.sum()/answer1.sum()
print res
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个结果并且发生了错误:
nan
C:\Users\Desktop\test.py:16: RuntimeWarning: invalid value encountered in double_scalars
res = answer.sum()/answer1.sum()
Run Code Online (Sandbox Code Playgroud)
似乎输入元素太小,以至于python将它们变成零,但实际上除法有其结果.
如何解决这类问题?
我在下面写了一个python脚本:
import numpy as np
arr = np.arange(6).reshape(2, 3)
arr[arr==0]=['nan']
print arr
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误:
Traceback (most recent call last):
File "C:\Users\Desktop\test.py", line 4, in <module>
arr[arr==0]=['nan']
ValueError: invalid literal for long() with base 10: 'nan'
[Finished in 0.2s with exit code 1]
Run Code Online (Sandbox Code Playgroud)
如何用nan替换NumPy数组中的零?
我在postgresql中使用driving_distance来查找所有节点之间的距离,这是我在pyscripter中的python脚本,
import sys
#set up psycopg2 environment
import psycopg2
#driving_distance module
query = """
select *
from driving_distance ($$
select
gid as id,
start_id::int4 as source,
end_id::int4 as target,
shape_leng::double precision as cost
from network
$$, %s, %s, %s, %s
)
;"""
#make connection between python and postgresql
conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = '****'")
cur = conn.cursor()
#count rows in the table
cur.execute("select count(*) from network")
result = cur.fetchone()
k …Run Code Online (Sandbox Code Playgroud) 我编写了以下脚本,以numpy数组形式从CSV文件加载数据:
import numpy
sample = numpy.genfromtxt('sample_cor.csv', delimiter = ',')
print sample
Run Code Online (Sandbox Code Playgroud)
这个样本看起来像:
[[ 259463.392 2737830.062 ]
[ 255791.4823 2742050.772 ]
[ 249552.4949 2746152.328 ]
[ 247925.1228 2746422.143 ]
[ 262030.4697 2728966.229 ]
[ 260462.1936 2731412.856 ]
[ 260644.0281 2735003.027 ]
[ 268588.7974 2732835.097 ]]
Run Code Online (Sandbox Code Playgroud)
现在我想将这个数组中的每一行提取为逗号字符串,例如,我希望第1行转换为259463.392,2737830.062第2行255791.4823,2742050.772,依此类推.
我试过下面的代码:
ss = numpy.array_str(sample[0])
print ss
print type(ss)
Run Code Online (Sandbox Code Playgroud)
得到的结果可能不是我想要的,
[ 259463.392 2737830.062]
<type 'str'>
Run Code Online (Sandbox Code Playgroud)
(我用的coords = '259467.2,2737833.87',并得到这是我想要的字符串形式:
259467.2,2737833.87)
如何将numpy数组中的元素转换为带逗号的字符串?
我使用psycopg2连接postgresql和python,这是我的脚本,
import sys
#set up psycopg2 environment
import psycopg2
#driving_distance module
query = """
select *
from driving_distance ($$
select
gid as id,
start_id::int4 as source,
end_id::int4 as target,
shape_leng::double precision as cost
from network
$$, %s, %s, %s, %s
)
"""
#make connection between python and postgresql
conn = psycopg2.connect("dbname = 'TC_routing' user = 'postgres' host = 'localhost' password = '****'")
cur = conn.cursor()
#count rows in the table
cur.execute("select count(*) from network")
result = cur.fetchone()
k = result[0] …Run Code Online (Sandbox Code Playgroud) 我使用qgis 导入了一个名为tc_bf25的 shapefile ,以下是我在pyscripter中键入的python脚本,
import sys
import psycopg2
conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = '****'")
cur = conn.cursor()
query = """
ALTER TABLE tc_bf25 ADD COLUMN source integer;
ALTER TABLE tc_bf25 ADD COLUMN target integer;
SELECT assign_vertex_id('tc_bf25', 0.0001, 'the_geom', 'gid')
;"""
cur.execute(query)
query = """
CREATE OR REPLACE VIEW tc_bf25_ext AS
SELECT *, startpoint(the_geom), endpoint(the_geom)
FROM tc_bf25
;"""
cur.execute(query)
query = """
CREATE TABLE node1 AS
SELECT row_number() OVER (ORDER BY foo.p)::integer …Run Code Online (Sandbox Code Playgroud) 我认为这是一个基本的python问题,但我无法找出原因.
我有一个名为tc_500的文件,其中包含一些我要编辑的CSV文件,因此我更改目录以编辑这些文件.
import sys, os
os.chdir('C:\Users\Heinz\Desktop\tc_500')
print os.getcwd()
Run Code Online (Sandbox Code Playgroud)
但事实证明这个错误,
>>>
Traceback (most recent call last):
File "C:\Users\Heinz\Desktop\python_test\any_test.py", line 13, in <module>
os.chdir('C:\Users\Heinz\Desktop\tc_500')
WindowsError: [Error 123] ????????????????????: 'C:\\Users\\Heinz\\Desktop\tc_500'
Run Code Online (Sandbox Code Playgroud)
如果我改变这样的代码,
import sys, os
os.chdir('C:\Users\Heinz\Desktop\TC_500')
print os.getcwd()
Run Code Online (Sandbox Code Playgroud)
它可以运行没有任何错误.
为什么我在使用大写而不是文件的真实名称时不会出错?
我得到一个包含多个数组的列表,我写了下面的代码试着看看这些数组的shape [0],
for i in xrange(len(list)):
k = list[i].shape[0]
print k
Run Code Online (Sandbox Code Playgroud)
输出是正确的,但我想检查这些形状[0],也就是说,如果它们相同,则函数将继续,否则,如果它们不是相同的数字,则函数会中断.这该怎么做?随意给我建议,非常感谢.
我创建了一个名为'ab'的列表,其中包含3个不同的数组,并使用错误和异常代码来检查形状[0]:
ab = [np.array([[1,2,3],[1,2,3]]),
np.array([[1,2,3]]),
np.array([[1,2,3],[1,2,3],[0,1,2],[0,9,9]])]
for i in xrange(len(ab)):
k = ab[i].shape[0]
print k
try:
all(x.shape[0]==ab[0].shape[0] for x in ab)
print 'True'
except ValueError:
print 'False'
Run Code Online (Sandbox Code Playgroud)
但产出是:
2
1
4
True
Run Code Online (Sandbox Code Playgroud)
输出是错误的,我在哪里犯了错误?