我想用Julia语言编写大量数据.生成数据,然后将其存储在列表中.伪代码是:
f = open("test.csv", "w")
for i = 1:3
position = [j for j = i:(i + 10) ]
string_position = string(position)
n = length(string_position)
write(f, string_position[2:(n - 1)]*"\n")
end
close(f)
Run Code Online (Sandbox Code Playgroud)
但是,在每次迭代中获取字符串的长度然后删除字符串的第一个和最后一个元素似乎效率低下.
有更快的方法吗?
我想尽可能有效地模拟随机助行器在网络中的移动.下面我展示一个玩具模型,其中包含我迄今为止尝试的三种方法.我应该注意,在我的原始问题中,网络的边缘是固定的,但是边缘的权重可以被更新(即,邻居的列表是相同的但是权重可以改变).
using QuantEcon
using LightGraphs
using Distributions
using StatsBase
n = 700 #number of nodes
#setting an arbitrary network and its transition matrix
G_erdos = erdos_renyi(n, 15/n)
A_erdos = adjacency_matrix(G_erdos) + eye(n, n);
A_transition = A_erdos ./ sum(A_erdos, 2);
##Method 1
#using QuantEcon library
function QE_markov_draw(i::Int, A::Array{Float64,2})
d = DiscreteRV(A[i, :]);
return rand(d, 1)
end
##Method 2
#using a simple random draw
function matrix_draw(i::Int, A::Array{Float64,2}, choices::Array{Int64,1})
return sample(choices, Weights(A[i, :]))
end
##Method 3
# The matrix may be sparse. Therefore …Run Code Online (Sandbox Code Playgroud) 我想将矩阵 A 的每一行乘以相同的向量 v。例如
A =[1.0 3.0; 1.0 1.0]
v = [1.0, 2.0]
Run Code Online (Sandbox Code Playgroud)
我想输出
[1.0 6.0; 1.0 2.0]
Run Code Online (Sandbox Code Playgroud)
到目前为止我正在做:
(v.*A')'
Run Code Online (Sandbox Code Playgroud)
但我怀疑这在计算上是否高效,因为我将矩阵转置两次。
请注意,这个问题已为 Matlab 解答(https://uk.mathworks.com/matlabcentral/answers/243307-vector-matrix-multiplication-row-wise)
无法删除问题.请参考问题:根据字典值使用Basemap对国家/地区进行阴影状态
我想在每个墨西哥州绘制数据(特定年份的病人数).我正在使用jupyter笔记本.到目前为止,我已经看到了几个选项和教程,但似乎没有一个似乎明确解释如何绘制一个国家的地图.下面我解释一些我见过的选项/教程以及为什么它们没有工作(我只是认为教程不是很直接):
Bokeh(http://bokeh.pydata.org/en/latest/docs/gallery/texas.html).在教程中,由于us_counties在bokeh.sampledata中,因此绘制了德州州.但是我没有在抽样数据中找到其他国家.
mpl_toolkits.basemap(http://www.geophysique.be/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/).虽然我能够导入shapefile,但我无法运行from shapefile import ShapeFile(ImportError:无法导入名称ShapeFile).此外,我无法下载dbflib库.
文森特(为什么Python的文森特地图visuzalization不从数据帧映射数据?在说教程出现没有图像(即使我使用的命令)当我运行从答案的代码vincent.core.initialize_notebook()).
Plotly(https://plot.ly/python/choropleth-maps/).本教程绘制了美国从csv表导入信息的地图(没有其他可用国家的信息).如果想要策划另一个国家,是否可以制作表格?
探索了这四个选项我发现教程不是很清楚或不容易理解.我发现很难相信在python中绘制一个国家的地图很困难.我认为必须有一个比过去教程中解释的更简单的方法.
问题是:用python绘制某个国家(任何)地图的最简单(希望是简单的)方法是什么?
我安装了以下软件包:matplotlib,pyshp,mpl_toolkits.basemap,bokeh,pandas,numpy.我还从http://www.gadm.org/下载了墨西哥的地图
提前致谢.