小编war*_*cat的帖子

colMeans(x, na.rm = TRUE) 中的错误:“x”在 KNN 分类中必须是数字

我有以下代码来尝试在分类模型中使用 knn:

library(dplyr)
library(e1071)
library(ggplot2)
library(nnet)
library(DMwR)
library(rpart.plot)
library(class)

dat         <- read.csv("C:/Users/Ze/Desktop/HallowSet/train.csv",header = T,stringsAsFactors = F)

needToSolve <- read.csv("C:/Users/Ze/Desktop/HallowSet/test.csv",header = T,stringsAsFactors = F)

dat$color <- factor(dat$color)
dat$type  <- factor(dat$type)

sp    <- sample(1:nrow(dat),0.7*nrow(dat))
train <- dat[sp,]
test  <- dat[-sp,]
full  <- rbind(train,test)



pre <-kNN(type ~ .,train ,test,k=3,norm=TRUE,type='class')
Run Code Online (Sandbox Code Playgroud)

但是当代码到达下一行时,我得到一个 colMeans(x, na.rm = TRUE) : 'x' 必须是数字,我不知道为什么会发生这种情况以及如何解决它,有人可以告诉我吗?谢谢提前。

字符串(完整):

'data.frame':   259 obs. of  12 variables:
$ id           : int  62 699 23 172 701 70 809 393 465 839 ...
$ bone_length  : num …
Run Code Online (Sandbox Code Playgroud)

runtime-error r knn

3
推荐指数
1
解决办法
1万
查看次数

所有带有向量&lt;int&gt;和回溯的排列c ++

我正在尝试生成向量的所有排列以训练回溯技术,但我的代码不适用于所有向量(按向量的大小工作)

我的代码:

#include <bits/stdc++.h>

using namespace std;

void permutations(int s,vector<int> v){
    if(s>=v.size())
        return;
    if( v.size()==0)
        cout<<endl;
    cout<<v[s]<<" ";
    vector<int> k = v;

    k.erase(k.begin()+s);

    for(int i =0;i<k.size();i++)
        return permutations(i,k);

}

int main(int argc, char const *argv[])
{
    vector<int> v = {1,2,3};
    for(int i =0;i<v.size();i++){
        permutations(i,v);
        cout<<endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我认为是因为当我的递归函数找到返回值时,它们会中断for的操作,但也许我错了,有人可以告诉我问题出在哪里,请问如何解决。

c++ vector permutation backtracking

2
推荐指数
1
解决办法
3911
查看次数

malloc和realloc分段错误

我正在使用linux并使用compiller gcc.我正在使用函数malloc和realloc尝试了解它是如何工作的.但是当我执行程序时给我分段错误.接下来我的代码:

#include<stdio.h>
#include<stdlib.h>

 int main(){
  register int cont=1;
  int i,n,*a;
  a=(int*)malloc(sizeof(int));
  scanf("%d",&n);
  while(n!=0){
   if(a!=NULL)
    a=(int*)realloc(a,cont*sizeof(int));
   else 
    goto exit;
   a[i]=n;
   scanf("%d",&n);
   cont++;
   i++;
 }

 for(i=0;i<cont;i++)
  printf("%d\n",a[i]);
 free(a);
 return 0;

 exit: printf("No memory\n");
 free(a);
 return -1;



}
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用,我的代码有什么问题?

c pointers memory-management

1
推荐指数
1
解决办法
391
查看次数

c ++矢量冒泡排序

我使用g ++ -std = c ++ 11 Sort.cpp来编译我的文件.

我的问题是冒泡排序不排序.

也许我按值传递矢量,但我不知道我的第一次尝试使用c ++,我选择使用矢量库.

我的代码是:

#include <iostream>
#include <vector>

using namespace std;

void bubbleSort(vector<int> a);

void printVector(vector<int> a);

int main(int argc, char const *argv[])
{
    vector<int> a{3,2,6,1};
    printVector(a);

    bubbleSort(a);
    printVector(a);
}

void bubbleSort(vector<int> a)
{
    bool swapp = true;
    while(swapp)
    {
        swapp = false;
        for (int i = 0; i < a.size()-1; i++)
        {
            if (a[i]>a[i+1] )
            {
                a[i] += a[i+1];
                a[i+1] = a[i] - a[i+1];
                a[i] -=a[i+1];
                swapp = true;
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c++ vector bubble-sort c++11

1
推荐指数
1
解决办法
2万
查看次数