SVR Model --Feature Scaling - Expected 2D array, got 1D array instead

2024/10/15 21:15:10

I am trying to understand what is wrong with the code below. I know that the Y variable is 1D array and expected to be 2D array and need to reshape the structure but that code was working previously fine with a warning.

# Importing the librariesimport numpy as npimport matplotlib.pyplot as pltimport pandas as pd# Importing the datasetdataset = pd.read_csv('Position_Salaries.csv')X = dataset.iloc[:, 1:2].valuesy = dataset.iloc[:, 2].values# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)

ValueError: Expected 2D array, got a 1D array instead:
array=[  45000.   50000.   60000.   80000.  110000.  150000.  200000.  300000.500000. 1000000.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
Answer

The solution is in the error message:

Reshape your data either using array.reshape(-1, 1) if your data has
a single feature or array.reshape(1, -1) if it contains a single sample.

Since you're passing in a single feature (not a single sample), try:

y = sc_y.fit_transform(y.reshape(-1, 1))
https://en.xdnf.cn/q/69235.html

Related Q&A

How to find the version of jupyter notebook from within the notebook

I wish to return the version of Jupyter Notebook from within a cell of a notebook. For example, to get the python version, I run: from platform import python_version python_version()or to get the panda…

Python logging - multiple modules

Im working on a small python project that has the following structure -project -- logs-- project__init.py__classA.pyclassB.pyutils.py-- main.pyIve set up the logging configuration in __init.py__ under …

Can you search backwards from an offset using a Python regular expression?

Given a string, and a character offset within that string, can I search backwards using a Python regular expression?The actual problem Im trying to solve is to get a matching phrase at a particular of…

Django AttributeError: Form object has no attribute _errors

Im overriding the init method in my form andthis is now returning an error TransactionForm object has no attribute _errors. I would expect this to work because Ive included super in my init, however pe…

Add new keys to a dictionary while incrementing existing values

I am processing a CSV file and counting the unique values of column 4. So far I have coded this three ways. One uses "if key in dictionary", the second traps the KeyError and the third uses &…

ImportError: cannot import name aiplatform from google.cloud (unknown location)

I was wondering where that error comes from. The package has to be installed additionally to google.cloud

What does : TypeError: cannot concatenate str and list objects mean?

What does this error mean?TypeError: cannot concatenate str and list objectsHeres part of the code:for j in (90.,52.62263.,26.5651.,10.8123.):if j == 90.:z = (0.)elif j == 52.62263.:z = (0., 72., 144.…

How do I create a fixed-length, mutable array of Python objects in Cython?

I need to have an array of python objects to be used in creating a trie datastructure. I need a structure that will be fixed-length like a tuple and mutable like a list. I dont want to use a list bec…

How to install atari-py in Windows 10? [duplicate]

This question already has answers here:OpenAI Gym Atari on Windows(5 answers)Closed 3 years ago.I tried to install lib pack atari-py, and can not find any clear information, most of them wrote that it …

Using pyplot to create grids of plots

I am new to python and having some difficulties with plotting using pyplot. My goal is to plot a grid of plots in-line (%pylab inline) in Juypter Notebook.I programmed a function plot_CV which plots cr…