首页 \ 问答 \ 使用稀疏矩阵与numpy数组(Using a sparse matrix versus numpy array)

使用稀疏矩阵与numpy数组(Using a sparse matrix versus numpy array)

我在Python中创建了一些带有字数的numpy数组:行是文档,列是对字X的计数。如果我有很多零计数,人们建议在处理这些时进一步使用稀疏矩阵,例如在分类器中。 当将一个numpy阵列与一个稀疏矩阵进行匹配时,它似乎没有什么区别。 所以我想知道三件事情:

  • 维基百科

    稀疏矩阵是大多数元素为零的矩阵

    这是确定何时使用稀疏矩阵格式的适当方法 - 只要> 50%的值为零? 或者是否有意义,以防万一?

  • 稀疏矩阵在像我这样的任务中有多少帮助性能,特别是与一个numpy数组或标准列表相比较?
  • 到目前为止,我将数据收集到一个numpy数组中,然后转换为Scipy中的csr_matrix。 这是做到这一点的正确方法吗? 我无法弄清楚如何从头开始构建一个稀疏矩阵,这可能是不可能的。

任何帮助深表感谢!


I am creating some numpy arrays with word counts in Python: rows are documents, columns are counts for word X. If I have a lot of zero counts, people suggest using sparse matrices when processing these further, e.g. in a classifier. When feeding a numpy array versus a sparse matrix into the Scikit logistic regression classifier, it did not seem to make much of a difference, however. So I was wondering about three things:

  • Wikipedia says

    a sparse matrix is a matrix in which most of the elements are zero

    Is that an appropriate way to determine when to use a sparse matrix format - as soon as > 50 % of the values are zero? Or does it make sense to use just in case?

  • How much does a sparse matrix help performance in a task like mine, especially compared to a numpy array or a standard list?
  • So far, I collect my data into a numpy array, then convert into the csr_matrix in Scipy. Is that the right way to do it? I could not figure out how to build a sparse matrix from the ground up, and that might be impossible.

Any help is much appreciated!


原文:https://stackoverflow.com/questions/36969886
更新时间:2022-08-20 07:08

最满意答案

你发送数据的方式是错误的,

data: {"data","1234"}
        ^      ^
        name    value

为了把这一切都转化为创意,

$.ajax({
    type: "POST",
    url: "/check-cust.php",
    data: {data:"1234"},// this the line in which you have error.
    success: function(data) {
        if (data == "") {
            console.log("success, but no return");
        } else {
            alert(data); // show response from the php script.
        }
    },
    error: function() {
        alert("not working");
    }
});

然后在PHP文件中,

$temp_cust_id = "1234";

$data = $_POST['data'];

if ($data == $temp_cust_id) {
    echo "it works";// it will echo this for current output.
} else {
    echo "it doesnt work";
}

the way you are sending data is wrong,

data: {"data","1234"}
        ^      ^
        name    value

To put it all togeather,

$.ajax({
    type: "POST",
    url: "/check-cust.php",
    data: {data:"1234"},// this the line in which you have error.
    success: function(data) {
        if (data == "") {
            console.log("success, but no return");
        } else {
            alert(data); // show response from the php script.
        }
    },
    error: function() {
        alert("not working");
    }
});

Then in php file,

$temp_cust_id = "1234";

$data = $_POST['data'];

if ($data == $temp_cust_id) {
    echo "it works";// it will echo this for current output.
} else {
    echo "it doesnt work";
}

相关问答

更多
  • 我不完全确定,但你似乎在问如何在没有JavaScript的情况下执行AJAX请求。 你不能这样做 - 没有JavaScript就不可能使用XmlHttpRequest对象。 根据传说,这就是AJAX名称中“J”的起源。 听起来你需要使用REST来调用特定的API。 RESTful状态允许您使用Web服务根据预定义的API返回特定数据。 数据可以XML或JSON格式返回。 使用Google为您提供的任何密钥,您可以使用PHP的cURL实现轻松完成此操作。 有关PHP API和示例代码的链接,请参阅Google ...
  • 更改要使用的AJAX调用: data: $('form#posting').serialize() + '&action=' + el,\ 然后使用访问参数 $title = $_POST['title']; 你正在这样做的方式,表单数据在POST数据中嵌套了一个级别,所以你必须这样做: $data = parse_str($_POST['data']); $title = $data['title']; Change the AJAX call to use: data: $('form#posti ...
  • 将所有复选框放在表单中,然后在表单上尝试$(“#form”)。serialize()。 第二件事,我认为它会发送复选框仅作为检查。 我想这可能会对你有所帮助。 我不明白的第三件事是为什么有重复的复选框,我的意思是所有都是两次,具有相同的名称和价值? Put all checkboxes in a form and try $("#form").serialize() on form. And 2nd thing, I think it will send checkboxes as on only whic ...
  • 在您的php文件中,您正在访问错误的密钥。 访问右键如下: $headcode = $_POST['idheadcode']; $location = $_POST['idlocation']; 也不要使用不推荐使用的mysql扩展。 我建议你开始使用pdo或mysqli学习预备语句 另一个建议是,在开发环境中,始终启用错误报告,如下所示 error_reporting(E_ALL);//place at the top of the script In your php file you are ...
  • 尝试 if (!$this->auth->cek_login()) { $data['status'] = false; $data['errtype'] = 'login'; }else{ //Add this else here i.e if valid login if ($this->mo_order->tambahBelanja() === TRUE) { if ($this->input->post('ajax') <> '1') ...
  • 你发送数据的方式是错误的, data: {"data","1234"} ^ ^ name value 为了把这一切都转化为创意, $.ajax({ type: "POST", url: "/check-cust.php", data: {data:"1234"},// this the line in which you have error. success: function(data) { if (dat ...
  • 这不起作用,因为您没有将查询中的值放入引号中。 这应该有助于防止你被黑客攻击: mysql_query("INSERT INTO tracking ( username, lessonid ) VALUES ( '".mysql_real_escape_string($username)."', '".mysql_real_escape_string($lessonid)."' )"); This cannot work, since you're not putting values in your q ...
  • 最简单的方法就是设置你的jquery.ajax来期待一个json的回报: $.ajax({ method: "POST", dataType: "JSON", /** others attributes **/ 之后,将您的返回转换为json并在PHP脚本中进行打印(仅此而已) //a better approach is return the column name (fetch_assoc) echo json_encode($row); 现在,你的回报可以用作j ...
  • 你真的不应该检查是否存在提交按钮值。 如您所见,对前端进行一些轻微的外观修改,比如用什么样的按钮来提交表单,不应该对后端产生任何影响。 提交按钮及其值与处理表单无关。 您真正要在服务器上检查的是,如果请求是POST请求,还是设置了要使用的值,或者两者都是: if ($_SERVER['REQUEST_METHOD'] == 'POST') … if (isset($_POST['name'], $_POST['email'], $_POST['mobile'])) … 最合理的是: if ($_SERVE ...
  • 我必须看到你的服务器端PHP代码,因为你的JavaScript代码看起来不错。 我认为它应该工作。 试试这样重写它,但是: