I have a problem in Anaconda Spyder (Python).
Object type array can not be seen under Windows 10 in the variable explorer. If I click on X or Y, I see an error:
object arrays are currently not supported.
I have Win 10 Home 64bit (i7-4710HQ) and Python 3.5.2 | Anaconda 4.2.0 (64-bit) [MSC v.1900 64 bit (AMD64)]
(Spyder developer here) Support for object arrays will be added in Spyder 4, to be released in 2019.
A good example is here
import numpy as np import pandas as pd import matplotlib.pyplot as plt dataset = pd.read_csv('Salary_Data.csv') #in your case right name of your file X=dataset.iloc[:,:-1].values #this will convert dataframe to object df = pd.DataFrame(X)
You can view data in dataframe this converts arrray to dataframe .
And the variable explorer accepts the dataframe. The above is correct and checked code
I used the same without
It worked for me.
x = dataset.iloc[:, :-1] y = dataset.iloc[:,3]
Solution: Downgrade the version of spyder to 3.2.0
You can do this by going to anaconda-navigator.
If you are following the Udemy Course on Machine Learning, probably the instructor is using an older version of spyder and it is working for him. In the newer versions like 3.2.8, it is not working but can be incorporated in the coming versions in the future.
I have analyzed the code until the point that could be failing for you.
It seems that array editor of Spyder doesn’t support showing arrays of mixed types (object arrays).
Here you can see the supported formats.
Something was confusing for me the first time that I used it: you receive the same editor when you click on a Dataset that when you click on an array variable.
In the case of a variable of type array, you receive an ArrayEditor widget. I think that call is made here.
But in the case of a variable of type DataFrame, you receive a DataFrameEditor. I think that call is made here
The problem is that both widgets look more or less the same, so one tend to think that receive the same result in both cases, but the DataFrameEditor allows mixed types and the ArrayEditor not.
You can try to inspect the array variables in the IPython console until the support is finally released in Spyder for the proper Widgets.
Use the following code:
dataset = pd.read_csv('Data.csv') X = pd.DataFrame(dataset.iloc[:, :-1].values)
This is because the array has more than one data-type so it can’t show an object with more than one datatype because it can’t select a single type.. But if it has only one datatype the type is ‘float64’ so it can be seen.
As long as your variables type are not same and in variable explorer you see this as object, it means variable needs to be converted to same type in your case.
You can fix it by using fit_transform():
Here is related part of the code for in that tutorial:
from sklearn.preprocessing import LabelEncoder , OneHotEncoder labelencoder_X_1 = LabelEncoder() X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1]) labelencoder_X_2 = LabelEncoder() X[:, 2] = labelencoder_X_1.fit_transform(X[:, 2]) onehotencoder = OneHotEncoder(categorical_features = ) X = onehotencoder.fit_transform(X).toarray()
It is yet not supported by Spyder but you can use IPyhon Console to print those values by directly typing variable name to it.
There are two things you can do to bypass the variable viewer in Spyder. You can either
A) use “print (X)” to reveal the contents of X, or
B) Simple use the IPython console by simply typing X and hit return. That too allow you to quickly reveal if the discussed ML functions are doing their job.
With the updated version of spyder you cannot see mixed arrays anymore using variable explorer. You can print the array instead in the console to inspect it.
I had a similar problem, because I insisted on using the exact format for the
y variable as for
x[: , 0] = labelencoder_x.fit_transform(x[:,0]),
and I used
y[:] = labelencoder_y.fit_transform(y[:]) *(taking into account the syntax for the fit transform for y)*
The above made the dtype for
y_train “object” which can’t be viewed on Spyder in the variable explorer.
When I used the exact line used by the instructor:
y = labelencoder_y.fit_transform(y).
The dtype changed to
int64 which can be viewed in the variable explorer.
This worked for me :
import pandas as pd labels = pd.read_csv('labels/labels.csv') # object arrays are currently not supported exception breeds = labels.breed.unique() # Supported Version # working fine breeds = pd.DataFrame(labels.breed.unique())
X = pd.DataFrame(X)
to convert X object to dataframe which can be checked in spyder also without the error.
Worked for me!
This is because the data are not encoded. All the categorical data should be “encoded”.
After looking at the data in variable explorer of your sypder(https://i.stack.imgur.com/uApwt.jpg), it is clear that
X contains data about some country (like [France, 44.0, 72000]), so the name of the country should be encoded and similarly
y contains “Yes” or “No”, so it should also be encoded
Add the following code after line 21, you will be able to see the object array
# Encoding categorical data from sklearn.preprocessing import LabelEncoder, OneHotEncoder ''' To prevent the machine learning equations from thinking (if there are more than one country) that one country is greater than another, use the concept of dummy variables ''' labelencoder_X = LabelEncoder() X[:, 0] = labelencoder_X.fit_transform(X[:, 0]) onehotencoder = OneHotEncoder(categorical_features = ) X = onehotencoder.fit_transform(X).toarray() ''' Since y is dependent variable, the machine learning model will know that its a category, so we are going to use only the LableEncoder() ''' labelencoder_y = LabelEncoder() y = labelencoder_y.fit_transform(y)
I had the same problem. Issue was the line
Which does not assign the data back to the X array. Instead, the following line should fix the issue:
If data is of same type example int or float, it will show in variable explorer, otherwise it doesn’t support if data has string and int for example.
But there is solution to check data, you can do it in IPython Console.
I have actually worked through this course, and the instructor never opens the object in the variable viewer. I went back to check and he actually does try, and runs into the exact same issue as you. When viewing the dataset, he views it in the console, not in the variable viewer.
As mentioned above, you can convert the whole thing to a dataframe which you then can open in the variable viewer:
dataset = pd.read_csv("data.csv") dataframe = pd.DataFrame(dataset)
You should now be able to view the data in the variable viewer, along with the categorical variables, as you like.
At the time I only viewed the dataset myself in the console by entering the name of the imported data, but this is also still a working approach on the newest version of Spyder.