当我used x = dataset.iloc[:,1:2].values
时,稍后在我的代码中
import matplotlib.pyplot as plt
import numpy as np
dataset = pd.read_csv('Position_Salaries.csv')
x = dataset.iloc[:,1:2].values #look here please
y = dataset.iloc[:,-1].values
from sklearn.svm import SVR
sv_regressor = SVR(kernel='rbf')
所以当我改用x = dataset.iloc[:,1].values
时,我收到一个错误说
"预期的 2D 数组,而是获得 1D 数组">
在sv_regresso行中
错误出在 w 行sv_regressor
,这就是我标记sklearn
不同之处在于,有了dataset.iloc[:,1:2]
,您将获得DataFrame
,而有了dataset.iloc[:,-1]
,您将获得Series
。当您将属性values
与DataFrame
一起使用时,您将获得 2d ndarray,使用Series
将获得 1d ndarray。请考虑以下示例:
A B C
0 0 2 0
1 1 0 0
2 1 2 1
系列:
type(df.iloc[:, -1])
# pandas.core.series.Series
df.iloc[:, -1].values.shape
# (3,)
数据帧:
type(df.iloc[:, -1:])
# pandas.core.frame.DataFrame
df.iloc[:, -1:].values.shape
# (3, 1)
机器学习中的一种常见技巧是一步获得目标变量作为 2D ndarray。
它几乎是一样的,dataset.iloc[:,1:2]
给你一个二维数据帧(从 1 到 2 列(,dataset.iloc[:,1]
给你一个熊猫系列(1-d((从第 1 列(。