1
votes

This is the code that I used . I'm trying to use a randomforestclassifier to classify the activity based on learner and dominant subject.

import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn import linear_model
from sklearn.ensemble import RandomForestClassifier
from sklearn import cross_validation
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import train_test_split
from sklearn.datasets import make_multilabel_classification
Data = pd.read_excel("F:\VIT material\Master thesis\DATASET.xlsx",names=['learner','Dominant_Subject','Activity'])
print(Data)
print(Data.columns)
Data.reshape(Data.columns.values)
print(Data)
number= LabelEncoder()
Data['learner']= number.fit_transform(Data['learner'].astype('str'))
Data['Dominant_Subject']=number.fit_transform(Data['Dominant_Subject'].astpye('str'))                                      
Data['Activity']= number.fit_transform(Data['Activity'].astype('str'))
print(Data)
print(Data.shape)
X = Data['learner']
print(X)
print(X.shape)
Y = Data['Dominant_Subject'] 
print(Y)
print(Y.shape)
print(len(X))
print(len(Y))
X_train = X[:-5]
X_test = X[-5:]
Y_train = Y[:-10]
Y_test = Y[-10:]
X_train, X_test, Y_train, Y_test=train_test_split(X,test_size=0.2,random_state=20)
print(X_train,X_test,Y_train,Y_test)
model = linear_model.LinearRegression() 
model.fit(X_train,Y_train)
print(model.fit())
clf = RandomForestClassifier(n_estimators=100, min_samples_split=2)
clf.fit(X_train,Y_train)
print(clf.fit())
predicted = clf.predict(X)
print(accuracy_score(predicted,Y))

The number of samples and labels is equal however I'm still getting the error that number of labels is not equal to number of samples.

Traceback of error : File "C:/Users/RAJIV MISHRA/PycharmProjects/mltutorialpractice/13.py", line 38, in clf.fit(X_train,Y_train)

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py", line 326, in fit

for i, t in enumerate(trees))

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in call while self.dispatch_one_batch(iterator):

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 608, in dispatch_one_batch

self._dispatch(tasks)

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 571, in _dispatch

job = self._backend.apply_async(batch, callback=cb)

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib_parallel_backends.py", line 109, in apply_async

result = ImmediateResult(func)

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib_parallel_backends.py", line 326, in init

self.results = batch()

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in call

return [func(*args, **kwargs) for func, args, kwargs in self.items]

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in

return [func(*args, **kwargs) for func, args, kwargs in self.items]

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py", line 120, in _parallel_build_trees

tree.fit(X, y, sample_weight=curr_sample_weight, check_input=False)

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\tree\tree.py", line 739, in fit

X_idx_sorted=X_idx_sorted)

File "C:\Users\RAJIV MISHRA\Anaconda3\lib\site-packages\sklearn\tree\tree.py", line 240, in fit

"number of samples=%d" % (len(y), n_samples))

ValueError: Number of labels=19 does not match number of samples=1

1
This is the traceback of the error :arpita mishra

1 Answers

1
votes

There are some issues that could be fixed inside this code. Assuming X.shape[0] == Y.shape[0]:-
1. The following code is unnecessary if you are using train_test_split

X_train = X[:-5]
X_test = X[-5:]
Y_train = Y[:-10]
Y_test = Y[-10:] 

The code has another problem also. The samples indexes do not match the labels indexes. My be the following can be used to fix this.

X_train = X[:-5]
X_test = X[-5:]
Y_train = Y[:-5]
Y_test = Y[-5:]     

2. If you are using train_test_split for spliting dataset into train and test sets you should pass both labels and samples.

X_train, X_test, Y_train, Y_test=train_test_split(X,Y,test_size=0.2,random_state=20)