Home > Net >  ValueError: Index data must be 1-dimensional
ValueError: Index data must be 1-dimensional

Time:05-12

I am trying to make a dataframe with RANDOM FOREST actual . prediction value and all the information from X_test.

This is my code:

def makeDataframe(self):
    self.df.index = self.X_test.reset_index()
    self.df['type'] = self.y_test
    self.df['prediction'] = self.y_pred
    self.df['ax'] = self.X_test['ax']
    self.df['ay'] = self.X_test['ay']
    self.df['az'] = self.X_test['az']
    print(self.df.head(10))

I want to make a dataframe like this:

type  prediction. ax ay az

But I am stuck with this error:

ValueError: Index data must be 1-dimensional

Any idea to solve this error. Durther, I want to confirm that my prediction index and x_test dataset index are matched.

Can I get some help?

Thank you

CodePudding user response:

You are getting the above error because reset_index() will return a DataFrame and not a 1-dimensional data you can replace it with self.X_test.index.values or To create a DataFrame you can use a Dictionary like this:

def makeDataframe(self):
    data = {}
    data.update({'index': self.X.index.values})
    data.update({'type': self.y_test.values})
    data.update({'prediction': self.y_pred})
    data.update({'ax': self.X_test['ax']})
    data.update({'ay': self.X_test['ay']})
    data.update({'az': self.X_test['az']}) 
    self.df = pd.DataFrame(data)
    print(self.df.head(10))
  • Related