I'm using sci-kit learn linear regression algorithm. While scaling Y target feature with:
Ys = scaler.fit_transform(Y)
I got
ValueError: Expected 2D array, got 1D array instead:
After that I reshaped using:
Ys = scaler.fit_transform(Y.reshape(-1,1))
But got error again:
AttributeError: 'Series' object has no attribute 'reshape'
So I checked pandas.Series documentation page and it says:
reshape(*args, **kwargs) Deprecated since version 0.19.0.
score:132
Solution was linked on reshaped method on documentation page.
Insted of Y.reshape(-1,1)
you need to use:
Y.values.reshape(-1,1)
Similar question
- Series object has no attribute 'strip'
- Object pandas has no attribute name Series
- pandas plot time series ['numpy.ndarray' object has no attribute 'find']
- Getting AttributeError 'Workbook' object has no attribute 'add_worksheet' - while writing data frame to excel sheet
- Pandas' series contains AttributeError: 'Series' object has no attribute 'contains'
- Series object has no split attribute - reading in data from text file
- Getting attribute error: Series object has no attribute 'explode'
- AttributeError: 'Series' object has no attribute 'reshape'
- Data-frame Object has no Attribute
- 'module' object has no attribute 'DataFrame'
score:0
Using MinMaxScaler to transform the Series to Dataframe worked on my end.
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
Y = scaler.fit_transform(pd.DataFrame(y))
score:4
You cannot reshape a pandas series, so you need to perform the operation on a numpy array. As others have suggested, you can use y.values.reshape(-1, 1)
, but if you want to impress your friends, you can use:
y.values[Ellipsis, None]
Which is equivalent to:
y.values[..., None]
It basically means all dimensions as they where, then a new dimension for the last one. Here's a fully working example:
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
y = pd.Series(np.random.rand(5))
0 0.497165
1 0.818659
2 0.327064
3 0.772548
4 0.095715
dtype: float64
scaler = StandardScaler()
scaler.fit_transform(y.values[Ellipsis, None])
array([[-0.019],
[ 1.165],
[-0.645],
[ 0.995],
[-1.496]])
score:27
The solution is indeed to do:
Y.values.reshape(-1,1)
This extracts a numpy array with the values of your pandas Series object and then reshapes it to a 2D array.
The reason you need to do this is that pandas Series objects are by design one dimensional. Another solution if you would like to stay within the pandas library would be to convert the Series to a DataFrame which would then be 2D:
Y = pd.Series([1,2,3,1,2,3,4,32,2,3,42,3])
scaler = StandardScaler()
Ys = scaler.fit_transform(pd.DataFrame(Y))
Credit To: stackoverflow.com
Related Query
- AttributeError: 'DataFrame' object has no attribute
- 'DataFrame' object has no attribute 'as_matrix
- AttributeError: 'ElementTree' object has no attribute 'getiterator' when trying to import excel file
- Pandas to_sql to sqlite returns 'Engine' object has no attribute 'cursor'
- AttributeError: 'Series' object has no attribute 'as_matrix' Why is it error?
- AtributeError: 'module' object has no attribute 'plt' - Seaborn
- Error: float object has no attribute notnull
- Pandas - 'Series' object has no attribute 'colNames' when using apply()
- pyspark error: AttributeError: 'SparkSession' object has no attribute 'parallelize'
- AttributeError: 'Series' object has no attribute 'iterrows'
- Bokeh: AttributeError: 'DataFrame' object has no attribute 'tolist'
- DataFrame object has no attribute 'sort_values'
- How to fix AttributeError: 'Series' object has no attribute 'to_numpy'
- How to solve the Attribute error 'float' object has no attribute 'split' in python?
- Unpickling dictionary that holds pandas dataframes throws AttributeError: 'Dataframe' object has no attribute '_data'
- str.contains pandas returns 'str' object has no attribute 'contains'
- AttributeError: 'str' object has no attribute 'view' in Seaborn , Scatterplot
- pandas - 'dataframe' object has no attribute 'str'
- Error in reading stock data : 'DatetimeProperties' object has no attribute 'weekday_name' and 'NoneType' object has no attribute 'to_csv'
- "DataFrame" object has no attribute 'reshape'
More Query from same tag
- Set cross section of pandas MultiIndex to DataFrame from addition of other cross sections
- Applying value to all members in Pandas pivot level
- Test if Pandas column is datetime type
- pandas dataframe multiline query
- Detect Outliers across all columns of Pandas Dataframe
- Reading data from csv file with different number of columns and mixed datatype - python
- Python code to return element value in dataframe based on another dataframe
- how to create a new columns with random values in pyspark?
- Combining partially overlapping data frames on both axes pandas
- Mapping values to dataframe from a different dataframe in Pandas
- Cooccurence matrix from pandas dataframe
- Convert a column of datetime and strings to period in pandas
- Converting into date-time format in pandas?
- Pandas dataframe drop by column
- Map Counter Object to DataFrame to create new column