【发布时间】:2021-11-15 09:25:36
【问题描述】:
在练习支持向量回归模型时,我遇到了这个错误:
ValueError: Expected 2D array, got scalar array instead:
array=6.5.
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.
这是我的代码(Python 3.7)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 20 14:39:06 2021
@author: lulu
"""
# SVR
# simple learning regression
# Data preprocessing
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd # import data sets and manage data sets
# Importing dataset
dataset = pd.read_csv('/home/lulu/machineLearning/Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
# Splitting the dataset into the training set and test set
"""from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 1/3,random_state = 0)"""
# Feature scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_X.fit_transform(y)
# Fitting SVR to the training set
# Create your regressor here
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)
# Producing a new result
y_pred = regressor.predict(sc_X.transform(6.5))
# Visualizing the test VR results
plt.scatter(X, y, color='red')
plt.plot(X, regressor.predict(X),color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
我想预测一个新结果,因此由于 6.5 在某种程度上没有转换,我们实际上需要使用以下函数对其进行转换,但我不知道如何正确应用此函数
sc_X.transform
我想知道为什么我没有根据关于多元线性回归等的基础得到理想的结果。结果毫无意义,我无法得出结论。
【问题讨论】:
标签: python-3.x scikit-learn non-linear-regression