Python Forum
Index data must be 1-dimensional : Classifier with sklearn
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index data must be 1-dimensional : Classifier with sklearn
#1
Hi,
As a very very beginner, I'm trying to write a code for a random tree forest classifier, and I'm starting with a step to know which features are important for classification.
I found this helpful guide that I'm currently using. https://www.datacamp.com/community/tutor...ier-python.

However, I have a problem when I use this code with my data.
The code works fine (I have my classifier accuracy that is printed) until the last line where I have an error message that says : ValueError: Index data must be 1-dimensional

My data look like this (for the first lines)
Output:
LS SSA B Fe K Mg Mn S Ti Zn cytotoxicity_class 1,3 283 16 21 47 45 1 44 20 32 low 0,7 439 92 1008 201 304 13 136 34 12 low 0,5 692 97 589 708 182 6 421 108 8 high
Here is the code :
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
data=pd.read_excel(r"C:\Users\ASUS\Desktop\Dataset_cluster_cytotoxicity.xlsx")
X=data[['LS', 'SSA', 'B', 'Fe', 'K', 'Mg', 'Mn', 'S', 'Ti', 'Zn']]  # Features
y=data['cytotoxicity_class']  # Labels
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # 70% training and 30% test
#Import Random Forest Model
from sklearn.ensemble import RandomForestClassifier
#Create a Gaussian Classifier
clf=RandomForestClassifier(n_estimators=100)
#Train the model using the training sets y_pred=clf.predict(X_test)
clf.fit(X_train,y_train)
y_pred=clf.predict(X_test)
#Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
from sklearn.ensemble import RandomForestClassifier
#Create a Gaussian Classifier
clf=RandomForestClassifier(n_estimators=100)
#Train the model using the training sets y_pred=clf.predict(X_test)
clf.fit(X_train,y_train)
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False)
feature_imp = pd.Series(clf.feature_importances_,index=X).sort_values(ascending=False)
Thank you for your help !
Salma
Larz60+ write Apr-02-2021, 01:17 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Note: output tags are good for formatting input as well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Slicing a 2 dimensional array Scott 2 121 Jan-12-2022, 07:18 AM
Last Post: paul18fr
  Unable to import sklearn after installing any package ilango 0 308 Oct-25-2021, 07:03 AM
Last Post: ilango
  Problem using two-dimensional interpolation. Result looks bad player1682 4 522 Oct-12-2021, 09:27 AM
Last Post: player1682
  Installing auto-sklearn on Windows 10 Led_Zeppelin 1 1,141 Apr-15-2021, 08:02 PM
Last Post: bowlofred
  index of range, but data prints out mrc06405j 1 783 Mar-25-2021, 07:20 PM
Last Post: buran
  2 Dimensional Arrays Prithak 4 923 Mar-21-2021, 09:35 PM
Last Post: deanhystad
  Knowing the index of a data frame Ivannovix 1 1,009 May-01-2020, 02:51 PM
Last Post: klllmmm
  comparing 2 dimensional list glennford49 10 1,987 Mar-24-2020, 05:23 PM
Last Post: saikiran
  Python 2 dimensional array creations sharpe 1 956 Nov-24-2019, 10:33 AM
Last Post: ThomasL
  Fetching data from Sklearn zaki424160 2 2,869 Jul-09-2019, 05:20 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020