Python Forum
RandomForest --ValueError: setting an array element with a sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RandomForest --ValueError: setting an array element with a sequence
#1
I am completely new to RandomForest and Machine Learning. Some help will be appreciated! Thank you!

Example of DataSet
**ID    |sentiment |  review                          | source   |**
'5'     |0         | lousy movie                      | twitter  |
'6'     |1         | excellent acting                 | website  |
'7'     |0         | bad script, but wonderful actors | feedback |
I create Bag-of-word (BOW) for review

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier

file_location = 'C:/Desktop/test.xlsx'
xlsx=pd.ExcelFile(file_location, engine='openpyxl')
df=xlsx.parse('Sheet1',header=0) 

bow=df['review']
Y_train=df['sentiment']

vect = CountVectorizer()
bow = vect.fit_transform(bow)
I created another df and added both BOW and Review as columns
Is this correct? Can I add the sparse matrix of bow into a df?
df1 = pd.DataFrame(bow)
df1['source']=df['source']

X_train=df1.values
print(X_train)
ouput of print(X_train)
[[<1x16 sparse matrix of type '<class 'numpy.int64'>'
        with 6 stored elements in Compressed Sparse Row format>
  'twitter']
 [<1x16 sparse matrix of type '<class 'numpy.int64'>'
        with 5 stored elements in Compressed Sparse Row format>
  'website']
 [<1x16 sparse matrix of type '<class 'numpy.int64'>'
        with 2 stored elements in Compressed Sparse Row format>
  'feedback']
Train the RandomForest Model

forest = RandomForestClassifier(n_estimators = 100) 
forest = forest.fit( X_train, Y_train)
Error

ValueError: setting an array element with a sequence
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequence to sequence AI advice tomharvey 0 491 Apr-24-2022, 05:06 AM
Last Post: tomharvey
  ValueError: Found array with 0 samples marcellam 1 3,789 Apr-22-2020, 04:12 PM
Last Post: jefsummers
  I am trying to change the value of an element in a record array ingu 1 1,597 Jan-14-2020, 01:30 PM
Last Post: perfringo
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 4,677 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  ValueError: could not broadcast input array from shape (75) into shape (25) route2sabya 0 5,936 Mar-14-2019, 01:14 PM
Last Post: route2sabya
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 2,991 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  Convert element of list to integer(Two dimentional array) zorro_phu 3 3,156 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 13,610 Apr-02-2018, 12:03 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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