上下文
我正在尝试对我自己的数据使用此交叉验证article中的方法(从csv导入,没有缺失值,全部是插值的,没有缺失,有些为0,有些为正数范围的负数正范围)。由于使用shift进行偏移,初始数据缺少页眉和页脚行,但请使用train_test_split函数中的[1:,] [:-1]来处理。
以任何方式尝试将代码包含在我自己的数据中,都会引发错误。我可以使用train_test_split函数将数据拆分为其他大多数函数,并且我怀疑错误与数据的结构方式有关吗?
link转换为csv
读为
input_file = "parsed.csv"
df = pd.read_csv(input_file, header = 0)
x = df.loc[0:,[
...
]]
...
model_testing = sm.OLS(model_training.predict(X_test),y_test,missing='drop').fit()
我最初尝试过。
clf = svm.SVC(kernel='linear', C=1).fit(X_train,y_train)
哪个抛出错误
/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
---------------------------------------------------------------------------
ValueError
Traceback (most recent call last)
<ipython-input-435-eae5045a136b> in <module>
因此,我查看了URL中的原始代码,发现形状参数与我的不同。因此,我尝试“压平”它们(1d),但出现了另一个错误。
代码
X_train, X_test, y_train, y_test = train_test_split(set1.loc[1:,][:-1], yFutureYield.loc[1:,][:-1], test_size=0.25)
model_testing = sm.OLS(model_training.predict(X_test),y_test,missing='drop').fit()
print(X_train.shape)
print(y_train.shape)
print(X_train.head(5))
print(y_train.head(5))
clf = svm.SVC(kernel='linear', C=1).fit(np.array(X_train).flatten(),
np.array(y_train).flatten())
生产
(285, 47)
(285, 1)
CSUSHPISA CUUR0000SETB01 DCOILBRENTEU RECPROUSM156N CPIHOSNS \
149 96.004 98.600 15.863182 0.02 164.100
272 148.031 220.542 67.646190 0.34 217.178
171 111.653 132.800 25.657143 32.02 175.400
187 123.831 120.900 26.651364 0.52 181.700
309 143.607 322.934 111.710870 0.02 223.708
CPALTT01USM661S PAYNSA CUUR0000SEHA CPIAUCSL LNS12300060 ... \
149 70.037170 130150 177.100 166.000 81.4 ...
272 91.074058 130589 248.965 215.861 75.1 ...
171 74.425041 132349 190.200 176.400 80.9 ...
187 76.154875 130356 200.200 180.500 79.3 ...
309 97.730543 135649 262.707 231.638 76.1 ...
CUUR0000SEHA CPIAUCSL LNS12300060 GS5 CUUR0000SETA01 \
149 31293.570000 27556.000000 6625.96 31.6064 20363.250000
272 61999.504985 46506.173145 5677.56 6.0909 18043.950080
171 36061.920000 31064.040000 6577.17 22.0864 20377.560000
187 39999.960000 32490.000000 6272.63 12.5349 19154.470000
309 68677.126647 53511.852570 5783.60 0.4757 20697.980975
CPILFESL CPILFENS PCECTPICTM CSUSHPINSA CSUSHPINSA
149 31169.900000 31187.560000 4.2025 96.393 -0.010515
272 48271.560320 48341.204652 4.2025 149.631 0.006909
171 34187.970000 34391.680000 4.2025 111.248 -0.007727
187 36404.550000 36347.300000 4.2025 124.729 -0.008424
309 53287.764816 53373.875280 4.2025 143.977 0.002688
[5 rows x 47 columns]
CSUSHPINSA
149 0.008579
272 -0.006950
171 0.008584
187 0.006125
309 -0.000042
---------------------------------------------------------------------------
错误
ValueError Traceback (most recent call last)
<ipython-input-433-9ef18f8c2bef> in <module>
90 #np.array(y_train).flatten()
91 #dir(model_training)
---> 92 clf = svm.SVC(kernel='linear', C=1).fit(np.array(X_train).flatten(), np.array(y_train).flatten())
93
94 #deltas
/opt/conda/lib/python3.6/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
147 self._sparse = sparse and not callable(self.kernel)
148
--> 149 X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
150 y = self._validate_targets(y)
151
/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
571 X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
572 ensure_2d, allow_nd, ensure_min_samples,
--> 573 ensure_min_features, warn_on_dtype, estimator)
574 if multi_output:
575 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
439 "Reshape your data either using array.reshape(-1, 1) if "
440 "your data has a single feature or array.reshape(1, -1) "
--> 441 "if it contains a single sample.".format(array))
442 array = np.atleast_2d(array)
443 # To ensure that array flags are maintained
ValueError: Expected 2D array, got 1D array instead:
array=[ 9.60040000e+01 9.86000000e+01 1.58631818e+01 ..., 4.20250000e+00
1.56299000e+02 -7.67852077e-03].
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.
答案 0 :(得分:1)
使用:
clf = svm.SVR(kernel='linear', C=1).fit(X_train,y_train.values.flatten())