Data Prediction using Decision Tree of rpart

妖精的绣舞 提交于 2021-02-18 22:28:47

问题


I am using R to classify a data-frame called 'd' containing data structured like below:

initial dataset

The data has 576666 rows and the column "classLabel" has a factor of 3 levels: ONE, TWO, THREE.

I am making a decision tree using rpart:

fitTree = rpart(d$classLabel ~ d$tripduration + d$from_station_id + d$gender +  d$birthday)

And I want to predict the values for the "classLabel" for newdata:

newdata = data.frame( tripduration=c(345,244,543,311), 
                      from_station_id=c(60,28,100,56),
                      gender=c("Male","Female","Male","Male"),  
                      birthday=c(1972,1955,1964,1967) )

 p <- predict(fitTree, newdata)

I expect my result to be a matrix of 4 rows each with a probability of the three possible values for "classLabel" of newdata. But what I get as the result in p, is a dataframe of 576666 rows like below:

enter image description here

I also get the following warning when running the predict function:

Warning message:
'newdata' had 4 rows but variables found have 576666 rows 

Where am I doing wrong?!


回答1:


I think the problem is: you should add "type='class'"in the prediction code:

    predict(fitTree,newdata,type="class")

Try the following code. I take "iris" dataset in this example.

    > data(iris)
    > head(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  1          5.1         3.5          1.4         0.2  setosa
  2          4.9         3.0          1.4         0.2  setosa
  3          4.7         3.2          1.3         0.2  setosa
  4          4.6         3.1          1.5         0.2  setosa
  5          5.0         3.6          1.4         0.2  setosa
  6          5.4         3.9          1.7         0.4  setosa

  # model fitting
  > fitTree<-rpart(Species~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,iris)

  #prediction-one row data
  > newdata<-data.frame(Sepal.Length=7,Sepal.Width=4,Petal.Length=6,Petal.Width=2)
  > newdata
  Sepal.Length Sepal.Width Petal.Length Petal.Width
  1            7           4            6           2

 # perform prediction
  > predict(fitTree, newdata,type="class")
     1 
  virginica 
  Levels: setosa versicolor virginica

 #prediction-multiple-row data
 > newdata2<-data.frame(Sepal.Length=c(7,8,6,5),
 +                      Sepal.Width=c(4,3,2,4),
 +                      Petal.Length=c(6,3.4,5.6,6.3),
 +                      Petal.Width=c(2,3,4,2.3))

 > newdata2
  Sepal.Length Sepal.Width Petal.Length Petal.Width
   1            7           4          6.0         2.0
   2            8           3          3.4         3.0
   3            6           2          5.6         4.0
   4            5           4          6.3         2.3

# perform prediction
> predict(fitTree,newdata2,type="class")
      1         2         3         4 
 virginica virginica virginica virginica 
 Levels: setosa versicolor virginica


来源:https://stackoverflow.com/questions/29572906/data-prediction-using-decision-tree-of-rpart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!