所以我试图编写一个python函数来返回一个名为Mielke-Berry R值的度量.度量标准计算如下:

我编写的当前代码有效,但由于等式中的和的总和,我唯一能想到解决它的问题是在Python中使用嵌套的for循环,这非常慢......
以下是我的代码:
def mb_r(forecasted_array, observed_array):
"""Returns the Mielke-Berry R value."""
assert len(observed_array) == len(forecasted_array)
y = forecasted_array.tolist()
x = observed_array.tolist()
total = 0
for i in range(len(y)):
for j in range(len(y)):
total = total + abs(y[j] - x[i])
total = np.array([total])
return 1 - (mae(forecasted_array, observed_array) * forecasted_array.size ** 2 / total[0])
Run Code Online (Sandbox Code Playgroud)
我将输入数组转换为列表的原因是因为我听说(尚未测试)使用python for循环索引一个numpy数组非常慢.
我觉得可能有某种numpy功能可以更快地解决这个问题,任何人都知道什么?
所以我一直在尝试优化一些从一些数组数据计算统计误差度量的代码.该指标称为连续排名概率分数(CRPS).
我一直在使用Numba来尝试加速计算中所需的双循环,但是我一直遇到这个numpy.vstack函数的问题.根据我从这里的文档中理解,vstack()应该支持该函数,但是当我运行以下代码时,我得到一个错误.
def crps_hersbach_numba(obs, fcst_ens, remove_neg=False, remove_zero=False):
"""Calculate the the continuous ranked probability score (CRPS) as per equation 25-27 in
Hersbach et al. (2000)
Parameters
----------
obs: 1D ndarry
Array of observations for each start date
fcst_ens: 2D ndarray
Array of ensemble forecast of dimension n x M, where n = number of start dates and
M = number of ensemble members.
remove_neg: bool
If True, when a negative value is found at the …Run Code Online (Sandbox Code Playgroud) 所以我有一个看起来像这样的数据框:
#1 #2
1980-01-01 11.6985 126.0
1980-01-02 43.6431 134.0
1980-01-03 54.9089 130.0
1980-01-04 63.1225 126.0
1980-01-05 72.4399 120.0
Run Code Online (Sandbox Code Playgroud)
我想要做的是将第一列 (11.6985) 的第一行向下移动 1 行,然后将第一列 (72.4399) 的最后一行移动到第一行第一列,如下所示:
#1 #2
1980-01-01 72.4399 126.0
1980-01-02 11.6985 134.0
1980-01-03 43.6431 130.0
1980-01-04 54.9089 126.0
1980-01-05 63.1225 120.0
Run Code Online (Sandbox Code Playgroud)
这个想法是我想使用这些数据框为每个班次找到一个 R^2 值,所以我需要使用所有数据,否则它可能不起作用。我曾尝试使用pandas.Dataframe.shift():
print(data)
#Output
1980-01-01 11.6985 126.0
1980-01-02 43.6431 134.0
1980-01-03 54.9089 130.0
1980-01-04 63.1225 126.0
1980-01-05 72.4399 120.0
print(data.shift(1,axis = 0))
1980-01-01 NaN NaN
1980-01-02 11.6985 126.0
1980-01-03 43.6431 134.0
1980-01-04 54.9089 130.0
1980-01-05 …Run Code Online (Sandbox Code Playgroud) 所以我试图索引NetCDF文件以获取某个网格单元格中的流量数据.我使用的NetCDF文件具有以下特征:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF3_CLASSIC data model, file format NETCDF3):
CDI: Climate Data Interface version 1.6.4 (http://code.zmaw.de/projects/cdi)
Conventions: CF-1.4
dimensions(sizes): lon(3600), lat(1800), time(31)
variables(dimensions): float64 lon(lon), float64 lat(lat), float64 time(time), float32 dis(time,lat,lon)
Run Code Online (Sandbox Code Playgroud)
我有35年以上的这些数据,我正在尝试从单个网格获取数据并创建一个时间序列来比较它做不同模型的预测.我目前用于从网格单元格中提取数据的代码如下所示.
from netCDF4 import Dataset
import numpy as np
root_grp = Dataset(r'C:\Users\wadear\Desktop\ERAIland_daily_dis_198001.nc')
dis = root_grp.variables['dis']
lat = np.round(root_grp.variables['lat'][:], decimals=2).tolist()
lon = np.round(root_grp.variables['lon'][:], decimals=2).tolist()
time = root_grp.variables['time'].shape[0]
lat_index = lat.index(27.95)
lon_index = lon.index(83.55)
for i in range(time):
print(dis[i][lat_index][lon_index])
Run Code Online (Sandbox Code Playgroud)
现在这感觉非常慢,并且需要很长时间才能在35年以上的时间内完成这项工作,并且在执行多个不同的网格单元时,所需的时间将真正增加.
是否有工具通过更快的I/O或索引来加速此过程?
谢谢!
我一直在做一个项目,我想用引导进度条显示给定预算年度目标的每月进度。我想在进度条中用垂直线显示目标(见图)。我已经把我一直在做的和我想能够做的。
我拥有的:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 46.573611111111106%" aria-valuenow="46.573611111111106" aria-valuemin="0" aria-valuemax="100"></div>
</div>Run Code Online (Sandbox Code Playgroud)
我想完成的事情:
因此,我正在尝试读取大量包含水文数据的相对较大的netCDF文件。NetCDF文件全部如下所示:
<xarray.Dataset>
Dimensions: (feature_id: 2729077, reference_time: 1, time: 1)
Coordinates:
* time (time) datetime64[ns] 1993-01-11T21:00:00
* reference_time (reference_time) datetime64[ns] 1993-01-01
* feature_id (feature_id) int32 101 179 181 183 185 843 845 847 849 ...
Data variables:
streamflow (feature_id) float64 dask.array<shape=(2729077,), chunksize=(50000,)>
q_lateral (feature_id) float64 dask.array<shape=(2729077,), chunksize=(50000,)>
velocity (feature_id) float64 dask.array<shape=(2729077,), chunksize=(50000,)>
qSfcLatRunoff (feature_id) float64 dask.array<shape=(2729077,), chunksize=(50000,)>
qBucket (feature_id) float64 dask.array<shape=(2729077,), chunksize=(50000,)>
qBtmVertRunoff (feature_id) float64 dask.array<shape=(2729077,), chunksize=(50000,)>
Attributes:
featureType: timeSeries
proj4: +proj=longlat +datum=NAD83 +no_defs
model_initialization_time: 1993-01-01_00:00:00
station_dimension: feature_id
model_output_valid_time: 1993-01-11_21:00:00
stream_order_output: …Run Code Online (Sandbox Code Playgroud) python ×6
python-3.x ×3
netcdf ×2
numpy ×2
pandas ×2
arrays ×1
bootstrap-4 ×1
css ×1
dask ×1
indexing ×1
matplotlib ×1
numba ×1
optimization ×1
plot ×1