首页 \ 问答 \ Redis Hyperloglog - PFCOUNT副作用(Redis Hyperloglog - PFCOUNT side effect)

Redis Hyperloglog - PFCOUNT副作用(Redis Hyperloglog - PFCOUNT side effect)

Redis最近发布了名为HyperLogLog的新数据结构。 它允许我们保留唯一对象的数量,并且只占用12k字节的大小。 我不明白的是,据说Redis的PFCOUNT命令在技术上是一个写命令。 为什么会这样?

注意:作为调用此函数的副作用,可能会修改HyperLogLog,因为最后8个字节编码最新的计算基数用于缓存目的。 所以PFCOUNT在技术上是一个写命令。


Redis recently released their new data structure called the HyperLogLog. It allows us to keep a count of unique objects and only takes up a size of 12k bytes. What I don't understand is that Redis's PFCOUNT command is said to be technically a write command. Why is this the case?

Note: as a side effect of calling this function, it is possible that the HyperLogLog is modified, since the last 8 bytes encode the latest computed cardinality for caching purposes. So PFCOUNT is technically a write command.


原文:https://stackoverflow.com/questions/23164374
更新时间:2023-12-06 07:12

最满意答案

什么etarion说是正确的。

要复制一列或一行,你必须写下:

Mat B = mat.col(i);
A.copyTo(B);

以下程序显示如何在OpenCV中执行PCA。 它会显示平均图像和前三个特征脸。 我在那里使用的图片可以从http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html获得

#include "cv.h"
#include "highgui.h"

using namespace std;
using namespace cv;

Mat normalize(const Mat& src) {
    Mat srcnorm;
    normalize(src, srcnorm, 0, 255, NORM_MINMAX, CV_8UC1);
    return srcnorm;
}

int main(int argc, char *argv[]) {
    vector<Mat> db;

    // load greyscale images (these are from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html)
    db.push_back(imread("s1/1.pgm",0));
    db.push_back(imread("s1/2.pgm",0));
    db.push_back(imread("s1/3.pgm",0));

    db.push_back(imread("s2/1.pgm",0));
    db.push_back(imread("s2/2.pgm",0));
    db.push_back(imread("s2/3.pgm",0));

    db.push_back(imread("s3/1.pgm",0));
    db.push_back(imread("s3/2.pgm",0));
    db.push_back(imread("s3/3.pgm",0));

    db.push_back(imread("s4/1.pgm",0));
    db.push_back(imread("s4/2.pgm",0));
    db.push_back(imread("s4/3.pgm",0));

    int total = db[0].rows * db[0].cols;

    // build matrix (column)
    Mat mat(total, db.size(), CV_32FC1);
    for(int i = 0; i < db.size(); i++) {
        Mat X = mat.col(i);
        db[i].reshape(1, total).col(0).convertTo(X, CV_32FC1, 1/255.);
    }

    // Change to the number of principal components you want:
    int numPrincipalComponents = 12;

    // Do the PCA:
    PCA pca(mat, Mat(), CV_PCA_DATA_AS_COL, numPrincipalComponents);

    // Create the Windows:
    namedWindow("avg", 1);
    namedWindow("pc1", 1);
    namedWindow("pc2", 1);
    namedWindow("pc3", 1);

    // Mean face:
    imshow("avg", pca.mean.reshape(1, db[0].rows));

    // First three eigenfaces:
    imshow("pc1", normalize(pca.eigenvectors.row(0)).reshape(1, db[0].rows));
    imshow("pc2", normalize(pca.eigenvectors.row(1)).reshape(1, db[0].rows));
    imshow("pc3", normalize(pca.eigenvectors.row(2)).reshape(1, db[0].rows));

    // Show the windows:
    waitKey(0);
}

如果你想按行构建矩阵(就像在你上面的原始问题中一样),用它来代替:

// build matrix
Mat mat(db.size(), total, CV_32FC1);
for(int i = 0; i < db.size(); i++) {
    Mat X = mat.row(i);
    db[i].reshape(1, 1).row(0).convertTo(X, CV_32FC1, 1/255.);
}

并将PCA中的标志设置为:

CV_PCA_DATA_AS_ROW

关于机器学习。 我用OpenCV C ++ API编写了一个关于机器学习的文档,其中包含大多数分类器的示例,其中包括支持向量机。 也许你可以在那里得到一些启发: http : //www.bytefish.de/pdf/machinelearning.pdf


What etarion said is correct.

To copy a column or row you always have to write:

Mat B = mat.col(i);
A.copyTo(B);

The following program shows how to perform a PCA in OpenCV. It'll show the mean image and the first three Eigenfaces. The images I used in there are available from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html:

#include "cv.h"
#include "highgui.h"

using namespace std;
using namespace cv;

Mat normalize(const Mat& src) {
    Mat srcnorm;
    normalize(src, srcnorm, 0, 255, NORM_MINMAX, CV_8UC1);
    return srcnorm;
}

int main(int argc, char *argv[]) {
    vector<Mat> db;

    // load greyscale images (these are from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html)
    db.push_back(imread("s1/1.pgm",0));
    db.push_back(imread("s1/2.pgm",0));
    db.push_back(imread("s1/3.pgm",0));

    db.push_back(imread("s2/1.pgm",0));
    db.push_back(imread("s2/2.pgm",0));
    db.push_back(imread("s2/3.pgm",0));

    db.push_back(imread("s3/1.pgm",0));
    db.push_back(imread("s3/2.pgm",0));
    db.push_back(imread("s3/3.pgm",0));

    db.push_back(imread("s4/1.pgm",0));
    db.push_back(imread("s4/2.pgm",0));
    db.push_back(imread("s4/3.pgm",0));

    int total = db[0].rows * db[0].cols;

    // build matrix (column)
    Mat mat(total, db.size(), CV_32FC1);
    for(int i = 0; i < db.size(); i++) {
        Mat X = mat.col(i);
        db[i].reshape(1, total).col(0).convertTo(X, CV_32FC1, 1/255.);
    }

    // Change to the number of principal components you want:
    int numPrincipalComponents = 12;

    // Do the PCA:
    PCA pca(mat, Mat(), CV_PCA_DATA_AS_COL, numPrincipalComponents);

    // Create the Windows:
    namedWindow("avg", 1);
    namedWindow("pc1", 1);
    namedWindow("pc2", 1);
    namedWindow("pc3", 1);

    // Mean face:
    imshow("avg", pca.mean.reshape(1, db[0].rows));

    // First three eigenfaces:
    imshow("pc1", normalize(pca.eigenvectors.row(0)).reshape(1, db[0].rows));
    imshow("pc2", normalize(pca.eigenvectors.row(1)).reshape(1, db[0].rows));
    imshow("pc3", normalize(pca.eigenvectors.row(2)).reshape(1, db[0].rows));

    // Show the windows:
    waitKey(0);
}

and if you want to build the matrix by row (like in your original question above) use this instead:

// build matrix
Mat mat(db.size(), total, CV_32FC1);
for(int i = 0; i < db.size(); i++) {
    Mat X = mat.row(i);
    db[i].reshape(1, 1).row(0).convertTo(X, CV_32FC1, 1/255.);
}

and set the flag in the PCA to:

CV_PCA_DATA_AS_ROW

Regarding machine learning. I wrote a document on machine learning with the OpenCV C++ API that has examples for most of the classifiers, including Support Vector Machines. Maybe you can get some inspiration there: http://www.bytefish.de/pdf/machinelearning.pdf.

相关问答

更多
  • 您可以使用2级SVM获得分数,如果您通过RAW_OUTPUT预测: // svm.cpp, SVMImpl::predict(...) , line 1917 bool returnDFVal = (flags & RAW_OUTPUT) != 0; // svm.cpp, PredictBody::operator(), line 1896, float result = returnDFVal && class_count == 2 ? (float)sum ...
  • 与opencv3.0,它是绝对不同的,但并不难: Ptr svm = ml::SVM::create(); // edit: the params struct got removed, // we use setter/getter now: svm->setType(ml::SVM::C_SVC); svm->setKernel(ml::SVM::POLY); svm->setGamma(3); Mat trainData; // one row per feature Mat l ...
  • 我最近不得不处理这个问题,这就是我最后所做的工作,以使SVM能够处理图像。 为了在一组图像上训练SVM,首先必须构建SVM的训练矩阵。 该矩阵如下所述:矩阵的每一行对应于一个图像,并且该行中的每个元素对应于该类的一个特征 - 在这种情况下,该特定点处的像素的颜色。 由于您的图像是2D,您将需要将它们转换为1D矩阵。 每行的长度将是图像的面积(请注意,图像的大小必须相同)。 假设你想在5个不同的图像上训练SVM,每个图像是4x3像素。 首先你必须初始化训练矩阵。 矩阵中的行数为5,列数为图像的面积,4 * 3 ...
  • 是的,你可以做PCA + SVM,有些人可能认为PCA不是最好用的功能,或者SVM不是最好的分类算法。 但是,嘿,有一个好的开始比坐在周围更好。 要使用OpenCV进行PCA,请尝试类似的方法(我还没有验证代码,只是为了让您了解): import os import cv2 import numpy as np # Construct the input matrix in_matrix = None for f in os.listdir('dirpath'): # Read the ima ...
  • 许多事情从OpenCV 2.4改为OpenCV 3.0 。 其中,机器学习模块不向后兼容。 这是针对SVM的OpenCV 教程代码 ,OpenCV 3.0的更新: #include #include #include "opencv2/imgcodecs.hpp" #include #include using namespace cv; usin ...
  • 我通过重写上面的代码解决了这个问题 Ptr svm; HOGDescriptor my_hog; ... // Load the trained SVM. svm = StatModel::load( "model.yml" ); // Set the trained svm to my_hog vector< float > hog_detector; get_svm_detector( svm, hog_detector ...
  • 什么etarion说是正确的。 要复制一列或一行,你必须写下: Mat B = mat.col(i); A.copyTo(B); 以下程序显示如何在OpenCV中执行PCA。 它会显示平均图像和前三个特征脸。 我在那里使用的图片可以从http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html获得 : #include "cv.h" #include "highgui.h" using namespace std; using nam ...
  • 假设你正确地得到了矩阵,每行代表一个样本,你能做的就像湖泊建议的那样: Cv::Mat anger, disgust; // Load the data into anger and disgust ... // Make sure anger.cols == disgust.cols // Combine your features from different classes into one big matrix int numPostives = anger.rows, numNegatives ...
  • 以下是在OpenCV4Android中训练SVM的示例。 trainData是一个MatOfFloat ,其形式取决于您用于获取特征向量的方法。 为了trainData ,我使用Core.hconcat()将数据集的每个元素的特征向量连接成一个Mat 。 Mat responses = new Mat(1, sizeOfDataset, CvType.CV_32F); responses.put(0, 0, labelArray); // labelArray is a float[] of labels ...
  • 试试这个链接,SVM的OpenCV文档提供了一个小例子 http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html try this link , OpenCV documentation of the SVM provides a small example http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introducti ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的