Valueerror: Setting an Array Element with a Sequence

ValueError: setting an array element with a sequence

Possible reason 1: trying to create a jagged array

You may be creating an array from a list that isn't shaped like a multi-dimensional array:

numpy.array([[1, 2], [2, 3, 4]])         # wrong!
numpy.array([[1, 2], [2, [3, 4]]])       # wrong!

In these examples, the argument to numpy.array contains sequences of different lengths. Those will yield this error message because the input list is not shaped like a "box" that can be turned into a multidimensional array.

Possible reason 2: providing elements of incompatible types

For example, providing a string as an element in an array of type float:

numpy.array([1.2, "abc"], dtype=float)   # wrong!

If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which allows the array to hold arbitrary Python objects:

numpy.array([1.2, "abc"], dtype=object)

Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?

You're getting the error message

ValueError: setting an array element with a sequence.

because you're trying to set an array element with a sequence. I'm not trying to be cute, there -- the error message is trying to tell you exactly what the problem is. Don't think of it as a cryptic error, it's simply a phrase. What line is giving the problem?

kOUT[i]=func(TempLake[i],Z)

This line tries to set the ith element of kOUT to whatever func(TempLAke[i], Z) returns. Looking at the i=0 case:

In [39]: kOUT[0]
Out[39]: 0.0

In [40]: func(TempLake[0], Z)
Out[40]: array([ 0., 0., 0., 0.])

You're trying to load a 4-element array into kOUT[0] which only has a float. Hence, you're trying to set an array element (the left hand side, kOUT[i]) with a sequence (the right hand side, func(TempLake[i], Z)).

Probably func isn't doing what you want, but I'm not sure what you really wanted it to do (and don't forget you can usually use vectorized operations like A*B rather than looping in numpy.) That should explain the problem, anyway.

SKLearn ValueError: setting an array element with a sequence

You have this error because your data is not formatted correctly when you call the fit method.
Your input is a DataFrame (with one column) of list, but the fit method is expecting a numpy array.

It should work if you do instead:

X = np.array(train[features][0].tolist())
clf.fit(X, labels_train)

So X is an array with 4 examples, each with 3 features.

Matplot Numpy ValueError: setting an array element with a sequence

You must change your x and y types to arrays:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits import mplot3d
myData = pd.read_csv('counted-JOSAIC.csv', delimiter=',', skiprows=0,usecols=range(0,5))

item_list = list(myData.columns) #Names of Columns
item_list = item_list[1:]
print(item_list)
myData = np.array(myData) #Convert to numpy
keywords = np.asarray(myData[:,0]) #Get the Keywords
print(keywords)
data = np.asarray(myData[:,1:]) #remove Keywords from data
print(data.shape)
print(data)
##################################################################################
###x=keyword
###y=year
###z=freq
y=np.arange(len(keywords))
x=np.arange(len(item_list))
X, Y = np.meshgrid(x, y)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, data, 50, cmap='binary')
ax.set_yticklabels(keywords)
ax.set_xticklabels(item_list)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
plt.show()

This gives:
Sample Image

Obviously you must change colors and x and y accordingly to get your desired output.



Related Topics



Leave a reply



Submit