t中的错误(as(from,“ngCMatrix)):在为函数't'选择方法时评估参数'x'时出错:asM中的错误

时间:2016-01-04 11:08:17

标签: r matrix sparse-matrix knime

我在Knime中使用R代码,这是一个数据分析平台。我从一个块中取一个矩阵,该块从xls文件中读取一个表。该矩阵称为“my_matrix”。然后我想将apriori算法应用到这个矩阵的三列,但是我得到了上面的错误。有任何想法吗? 请注意,“sum(is.na(my_matrix))”输出[1] 0

> my_matrix = as(knime.in, "matrix")
> dput(head(my_matrix, 5))
structure(c("KS", "OH", "NJ", "OH", "OK", "128", "107", "137", 
" 84", " 75", "415", "415", "415", "408", "415", "noIP", "noIP", 
"noIP", "IP", "IP", "Vmail", "Vmail", "noVmail", "noVmail", "noVmail", 
"25", "26", " 0", " 0", " 0", "265.1", "161.6", "243.4", "299.4", 
"166.7", "110", "123", "114", " 71", "113", "45.07", "27.47", 
"41.38", "50.90", "28.34", "197.4", "195.5", "121.2", " 61.9", 
"148.3", " 99", "103", "110", " 88", "122", "16.78", "16.62", 
"10.30", " 5.26", "12.61", "244.7", "254.4", "162.6", "196.9", 
"186.9", " 91", "103", "104", " 89", "121", "11.01", "11.45", 
" 7.32", " 8.86", " 8.41", "10.0", "13.7", "12.2", " 6.6", "10.1", 
" 3", " 3", " 5", " 7", " 3", "2.70", "3.70", "3.29", "1.78", 
"2.73", "1", "1", "0", "2", "3", "notChurning", "notChurning", 
"notChurning", "notChurning", "notChurning"), .Dim = c(5L, 20L
), .Dimnames = list(c("Row0", "Row1", "Row2", "Row3", "Row4"), 
    c("State", "Account length", "Area code", "International plan", 
    "Voice mail plan", "Number vmail messages", "Total day minutes", 
    "Total day calls", "Total day charge", "Total eve minutes", 
    "Total eve calls", "Total eve charge", "Total night minutes", 
    "Total night calls", "Total night charge", "Total intl minutes", 
    "Total intl calls", "Total intl charge", "Customer service calls", 
    "Churn")))

my_matrix是一个表,其中的列是这种类型:(String,Integer,Integer,String,String,Integer,Double,Integer,Double,Double,Integer,Double,Double,Integer,Double,Double,Integer,Double ,整数,字符串)。表中有3300行。

    require(arules)
    #require(arulesViz)
    my_matrix= as(knime.in,"matrix");
    my_rows= nrow(my_matrix);
    my_cols= ncol(my_matrix);
    my_matrix = append(my_matrix[,4:5], my_matrix[,20])
    my_matrix <- array(my_matrix, dim=c(my_rows, 3))
    head(my_matrix, 2)
    my_cols = 3
    my_dedup_matrix= matrix(nrow=my_rows, ncol=my_cols);
    for (i in 1:my_rows) {
        m = my_matrix[i,];
        my_unique= unique(m);
        for (j in 1:my_cols) {
            my_dedup_matrix[i,j] = my_unique[j];
        }
    }
    my_new_data= apply(my_dedup_matrix, 1, function(x) x[!is.na(x)]);
    my_transactions = as(as.list(my_new_data), "transactions");
    summary(my_transactions)



## Mine association rules.
rules = apriori(my_transactions, parameter = list(supp= 0.1, conf= 0.8, target = "rules"));
summary(rules); # no rules found
inspect(rules); # no rules found
#inspect(rules[1:10])

我修改了我的代码,如下所示。我现在没有错误,但apriori算法没有规则:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        rbBookRating = (RatingBar) findViewById(R.id.rbRating);
        rbBookRating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });
        LayerDrawable stars = (LayerDrawable) rbBookRating.getProgressDrawable();
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(0).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
    }

1 个答案:

答案 0 :(得分:1)

您需要先将my_matrix转换为列表。 as(as.list(my_matrix),"transactions") 但是,我得到了一个不同的错误。

as(as.list(matrix_temp),"transactions")
transactions in sparse format with
 15 transactions (rows) and
 5 items (columns)