cannot concatenate object of type '<class 'numpy.ndarray'>'; only Series and DataFrame objs are valid

Ctrl7

I am intending to visualise the data using a pairplot after using StandardScaler, But my code is producing the following error

    raise TypeError(msg)
TypeError: cannot concatenate object of type '<class 'numpy.ndarray'>'; only Series and DataFrame objs are valid

Full code

from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler 

iris = sns.load_dataset('iris')
X = iris.drop(columns='species')
y = iris['species']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)

X_train=StandardScaler().fit_transform(X_train)

sns.pairplot(data=pd.concat([X_train, y_train], axis=1), hue=y_train.name)

plt.show()
Adarsh Wase

After using StandardScaler, your X_train (which was a pd.DataFrame before) has become a numpy.ndarray, so that's why you cannot concat X_train and y_train. Because X_train is a NumPy array and y_train is a Pandas DataFrame

To use concat, both X_train and y_train has to be a Pandas DataFrame, so convert X_train to a DataFrame using this code.

X_train = StandardScaler().fit_transform(X_train)

X_train = pd.DataFrame(X_train, columns = X.columns)
sns.pairplot(data=pd.concat([X_train, y_train], axis=1), hue=y_train.name)
plt.show()

It will work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid

TypeError: cannot concatenate object of type '<class 'yfinance.ticker.Options'>'; only Series and DataFrame objs are valid

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid I got this error

Tulipy Cannot convert Series to numpy.ndarray

class 'numpy.int32' wanted, but type() function only shows: numpy.ndarray

Pandas Series to Numpy ndarray

cannot concatenate object of type '<class 'list'>' when convering from df.append to pd.concat

TypeError: unhashable type: 'numpy.ndarray' when trying to plot a DataFrame

Adding Numpy ndarray into dataframe

numpy ndarray to pandas dataframe

Change several columns of a dataframe to integer type. Error cannot convert the series to <class 'int'>

numpy.ndarray object's name only converted to a string

PyTorch and TensorFlow object detection - evaluate - object of type <class 'numpy.float64'> cannot be safely interpreted as an integer

confused ,why Object[] objs (type of objs[i] is int[]) cant cast into int[][]

Invalid property specified for object of type plotly.graph_objs.Bar

Argument of type "Series[Dtype]" cannot be assigned to parameter of type "DataFrame"

Append Numpy ndarray to pandas dataframe

Type hint for NumPy ndarray dtype?

AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__' on import numpy 1.15.4

Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) on ImageDataGenerator in Keras

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) with tensorflow CNN

TensorFlow Dataset: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray). in trying to predict tesla stock

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) with array size exceeding 4000

Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) error

Convert pandas nested Series into Numpy ndarray

Polars Series.to_numpy() does not return ndarray