我必须将数字ID
转换为我知道维度的矩阵i
的索引j
和M
。表达式如下:
s = shape(M)
j = (ID - 1) % s[0]
i = np.int((ID - 1 - j) / s[0])
我必须保存来自数据框df
的值,每种类型我都会重复以下内容:
M = np.zeros((m, n))
s = shape(M)
for k in df.index:
ID = df['Id'][k]
y = (ID - 1) % s[0]
x = np.int((ID - 1 - y) / s[0])
M[x,y] = M[x,y] + df['R'][k]
我想知道有一种方法可以避免每次循环并节省计算时间。
答案 0 :(得分:0)
基本上,您可以M
Series
R
MultiIndex
i
使用j
,.unstack()
使用import numpy as np
import pandas as pd
M = pd.DataFrame(index=range(4), columns=range(4))
s = M.shape
df = pd.DataFrame({
'ID': np.arange(4*4),
'R': np.random.rand(4*4),
})
df['j'] = df.ID % s[0]
df['i'] = np.floor(df.ID / s[0]).astype(np.int)
#In [26]: q.df
Out[26]:
# ID R j i
# 0 0 0.847475 0 0
# 1 1 0.343867 1 0
# 2 2 0.806913 2 0
# 3 3 0.105036 3 0
# 4 4 0.170887 0 1
# 5 5 0.782607 1 1
# 6 6 0.377907 2 1
# 7 7 0.586738 3 1
# 8 8 0.895262 0 2
# 9 9 0.290358 1 2
# 10 10 0.109228 2 2
# 11 11 0.423267 3 2
# 12 12 0.454464 0 3
# 13 13 0.604296 1 3
# 14 14 0.603850 2 3
# 15 15 0.974695 3 3
df.set_index(['i', 'j'], inplace=True)
M = df['R'].unstack()
# In [30]: q.M
# Out[30]:
# j 0 1 2 3
# i
# 0 0.975895 0.506286 0.055459 0.551988
# 1 0.846078 0.114153 0.981231 0.681130
# 2 0.142864 0.050597 0.323655 0.918745
# 3 0.734922 0.951795 0.209542 0.547859
直接获取矩阵jQuery(function ($) {
loading = false;
$('#menu_left').on('click', 'a', function (e) {
e.preventDefault();
if (!loading) {
loading = true;
var data = {
action: 'glevouir_ajax_load',
nonce: glevouirajaxload.nonce,
pageid: $(this).parent().attr("id")
};
$.post(glevouirajaxload.url, data, function (res) {
if (res.success) {
$("#content").slideUp("fast", function () {
$("#content").html(res.data);
$(this).slideDown("fast", function () {
// Animation complete.
});
});
}
});
loading = false;
}
});
});
}}:
function glevouir_scripts()
{
$args = array(
'nonce' => wp_create_nonce('cleavir-ajax-load-nonce'),
'url' => admin_url('admin-ajax.php')
);
wp_enqueue_script('glevouir-ajax-load', get_stylesheet_directory_uri() . '/js/ajax-load.js', array('jquery'), '1.0', true);
wp_localize_script('glevouir-ajax-load', 'glevouirajaxload', $args);
}
add_action('wp_enqueue_scripts', 'glevouir_scripts');
function glevouir_ajax_load()
{
$page_id = esc_attr($_POST['pageid']);
ob_start();
if ( function_exists( 'do_shortcode ' ) ) {
echo "do_shortcode exists";
if(is_object($post_obj))
echo do_shortcode($post_obj->post_content);
} else {
echo "do_shortcode doesn't exist"; // this gets echoed
}
$attr = array(
"link" => "file",
"columns" => 4,
"ids" => "98,97,96"
);
if ( function_exists( 'gallery_shortcode ' ) ) {
echo "gallery_shortcode exists";
echo gallery_shortcode($attr);
} else {
echo "gallery_shortcode doesn't exist"; // this gets echoed
}
$data = ob_get_clean();
wp_send_json_success($data);
wp_die();
}
add_action('wp_ajax_glevouir_ajax_load', 'glevouir_ajax_load');
add_action('wp_ajax_nopriv_glevouir_ajax_load', 'glevouir_ajax_load');
答案 1 :(得分:0)
在我按照您的程序后,我的数据框df
。
df.head(10):
Old1 ID R
i j
11 36 1169 0 0.0
12 32 1268 1 0.0
34 1270 2 0.0
35 1271 3 2.0
36 1272 4 0.0
37 1273 5 0.0
40 1276 6 0.0
41 1277 7 0.0
13 32 1371 8 7.0
33 1372 9 0.0
是否可以将这些值与矩阵相关联,即
M[12][35]=2.0
M[13][32]=7.0
等等。
这就是我所做的:
tmp = list(dftmp.index)
tmp = pd.DataFrame(tmp)
tmp.columns = ('a','b')
M[tmp.a, tmp.b] = dftmp.I