我正在处理n个回归问题。我有10个自变量。我使用svr。尽管使用网格搜索进行功能选择和调整SVR参数,但我还是获得了15%的巨大Mape。因此,我试图删除异常值,但是删除它们后,我无法将数据分开。我的问题是离群值会影响回归的准确性吗?
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import Normalizer
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
import pandas as pd
from sklearn import preprocessing
features=pd.read_csv('selectedData.csv')
target = features['SYSLoad']
features= features.drop('SYSLoad', axis = 1)
from scipy import stats
import numpy as np
z = np.abs(stats.zscore(features))
print(z)
threshold = 3
print(np.where(z > 3))
features2 = features[(z < 3).all(axis=1)]
from sklearn.model_selection import train_test_split
train_input, test_input, train_target, test_target = train_test_split(features2, target, test_size = 0.25, random_state = 42)
执行以下代码时,我会收到此错误。
"样本:%r"%[int(l)的长度])
valueerror:发现的输入变量,数量不一致 样本:[33352,35064]"
您会遇到错误,因为 target
变量的长度与features
相等(大概是35064),因此是:
target = features['SYSLoad']
您的features2
变量的长度较小(大概是33352),即是features
的 subset ,因此是:
features2 = features[(z < 3).all(axis=1)]
和您的train_test_split
合理地抱怨您的功能的长度&amp;标签不相等。
因此,您还应相应地将target
子集,并在train_test_split
中使用此target2
:
target2 = target[(z < 3).all(axis=1)]
train_input, test_input, train_target, test_target = train_test_split(features2, target2, test_size = 0.25, random_state = 42)