Note make sure that Venue Categories column has same number of elements for each row of data, else a new problem will arise again. Train_data,test_data,train_labels,test_labels = train_test_split(data,labels,test_size=0.20) Return np.array( for col in other_features]+categories for i in range(len(categories))]), np.array(df.values)ĭff = labelencoder.fit_transform(dff)ĭata, labels = make_data(dff, 'Venue', 'Venue Categories') I managed to make it work, by combining the city column with the venue categories column into a 2D (numpy) array which can be used by the RandomForestClassifier of sklearn.įrom sklearn.preprocessing import LabelEncoderįrom sklearn.ensemble import RandomForestClassifierįrom sklearn.model_selection import train_test_splitĭef make_data(df, target_column='Venue', categories_column='Venue Categories'):Ĭategories = df.values This will looks weird, I want to make it global, means there should not need to touch the code if we may increase the number of columns. I want to add more columns, then I again write everything for each feature just like shown below, if i want to add type column and owner column city = dff.valuesĬategories = dff.valuesĭata = np.array(, owner, type categories) for i in range(len(city))]) Let's say, if I want to increase the number of features. I am passing this data to my machine learning model, but model.fit is not accepting the input, My code is shown below, that I am trying, labelencoder = LabelEncoder()ĭff=labelencoder.fit_transform(dff) WaterFront Austria Īeronaut Marvilles Īeronaut Paris Numpy.I have the columns in my Data Frame as shown below: Venue city Venue Categories an() #Fail, can't convert a tuple into a numpy maxlen50 maxfeatures20000 classifier Sequential () classifier.add (Embedding (maxfeatures, 100, maskzeroTrue)) classifier.add (LSTM (200, dropout0.3, recurrentdropout0.3, returnsequencesFalse)) classifier.add (Dense (1, activation'softmax')) pile (loss 'sparsecategoricalcrossentropy', optimizer'adam',metrics '. Numpy.array() #Fail, can't convert a tuple into a numpy Print(np.array(, dtype=object))Ĭheck out the below examples for more use cases and best practices while working with numpy arrays. # Changing the dtype as object and having multiple data type Solution – The solution of this is straightforward if you need either you declare only floating numbers inside an array or if you want both, then make sure that you change the dtype as an object instead of float as shown below. ValueError: could not convert string to float: 'Hello World' The other possibility where you get Value Error would be when you try to create an array with different types of elements for instance, consider the below example where we have an array with float and string mixed, which again throws valueerror: could not convert string to float. Solution – By creating the same dimensional array and having identical array elements in each array will solve the problem as shown below. ValueError: setting an array element with a sequence. Print(np.array(,, ], ,]], dtype=int))įile "c:\Projects\Tryouts\listindexerror.py", line 2, in If you look at the example, the numpy array is 2-dimensional, but at the later stage, we have mixed with single-dimensional array also, and hence Python detects this as an inhomogeneous shape that means the structure of the array varies, and hence Python throws value error. In this case, if the Numpy array is not in the sequence, you will get a Value Error. What is valueerror: setting an array element with a sequence?Ī ValueError occurs when a function receives an argument of the correct type, but the value of the type is invalid. In Python, if you are mainly working with numpy and creating a multi-dimensional array, you would have encountered valueerror: setting an array element with a sequence.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |