使用PCA进行回归问题时模型精度降低。

huangapple go评论61阅读模式
英文:

reduction of model accuracy while using PCA for a regression problem

问题

我正在尝试构建一个预测航班票价的问题。我的数据集包含了一些分类变量,如舱位、小时、星期几、月份等。我使用了多种算法,如XGBoost和人工神经网络(ANN)来拟合模型。

最初,我对这些变量进行了独热编码,导致了总共90个变量。当我尝试为这个数据拟合模型时,训练的R2分数很高,约为0.90,但测试分数相对较低(0.6)。

我尝试对时间变量进行了正弦和余弦变换,这导致总共只有27个变量。使用这些变量,训练准确度下降到0.83,但测试分数增加到0.70。

我曾考虑过我的变量可能很稀疏,尝试进行PCA,但这大大降低了训练集和测试集的性能。

因此,我有一些关于这个问题的问题。

  1. 为什么PCA没有帮助,反而严重降低了我的模型性能?
  2. 有关如何提高我的模型性能的建议吗?
# 你的Python代码部分

谢谢。

英文:

I am trying to build a prection problem to predict the fare of flights. My data set has several catergorical variables like class,hour,day of week, day of month, month of year etc. I am using multiple algorithms like xgboost, ANN to fit the model

Intially I have one hot encoded these variables, which led to total of 90 variables, when I tried to fit a model for this data, training r2_score was high around .90 and test score was relatively very low(0.6).

I have used sine and cosine transformation for temporal variables, this led to a total of only 27 variables. With this training accuracy has dropped to .83 but test score is increased to .70

I was thinking that my variables are sparse and tried doing a PCA, but this drastically reduced the performance both on train set and test set.

So I have few questions regarding the same.

  1. Why is PCA not helping and inturn reducing the performance of my model so badly
  2. Any suggestions on how to improve my model performance?

code


from xgboost import XGBRegressor
import pandas as pd
import matplotlib.pyplot as plt

dataset = pd.read_excel('Airline Dataset1.xlsx',sheet_name='Airline Dataset1')

dataset = dataset.drop(columns = ['SL. No.'])
dataset['time'] = dataset['time'] - 24

import numpy as np
dataset['time'] = np.where(dataset['time']==24,0,dataset['time'])

cat_cols = ['demand', 'from_ind', 'to_ind']

cyc_cols = ['time','weekday','month','monthday']

def cyclic_encode(data,col,col_max):
    data[col + '_sin'] = np.sin(2*np.pi*data[col]/col_max)
    data[col + '_cos'] = np.cos(2*np.pi*data[col]/col_max)
    return data 

cyclic_encode(dataset,'time',23)
cyclic_encode(dataset,'weekday',6)
cyclic_encode(dataset,'month',11)
cyclic_encode(dataset,'monthday',31)

dataset = dataset.drop(columns=cyc_cols)


ohe_dataset = pd.get_dummies(dataset,columns = cat_cols , drop_first=True)
X = ohe_dataset.iloc[:,:-1]
y = ohe_dataset.iloc[:,27:28]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train_us, X_test_us, y_train_us, y_test_us = train_test_split(X, y, test_size = 0.2, random_state = 0)


# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_Y = StandardScaler()
X_train = sc_X.fit_transform(X_train_us)
X_test = sc_X.transform(X_test_us)

y_train = sc_Y.fit_transform(y_train_us)
y_test = sc_Y.transform(y_test_us)


#Applying PCA
from sklearn.decomposition import PCA
pca = PCA(n_components = 2)

X_train = pca.fit_transform(X_train,y_train)
X_test = pca.transform(X_test)
explained_variance = pca.explained_variance_ratio_

regressor = XGBRegressor()
model = regressor.fit(X_train,y_train)

# Predicting the Test & Train set with regressor built
y_pred = regressor.predict(X_test)
y_pred = sc_Y.inverse_transform(y_pred)
y_pred_train = regressor.predict(X_train)
y_pred_train = sc_Y.inverse_transform(y_pred_train)
y_train = sc_Y.inverse_transform(y_train)
y_test = sc_Y.inverse_transform(y_test)


#calculate r2_score
from sklearn.metrics import r2_score
score_train = r2_score(y_train,y_pred_train)
score_test = r2_score(y_test,y_pred)

Thanks

答案1

得分: 1

你不真的需要PCA来解决这样小维度的问题。决策树在有成千上万个变量的情况下表现非常出色。

以下是一些您可以尝试的方法:

  1. 传递一个监控列表,训练直到在验证集上不再过拟合。链接
  2. 尝试所有正弦余弦转换以及其他一次性编码,并建立一个模型(同时使用监控列表)。
  3. 寻找更多的因果数据。仅仅季节性模式不会引起机票价格波动。首先,您可以为节日、假期和重要日期添加标志。还要进行与这些日期的距离的特征工程。天气数据也很容易找到并添加。

PCA通常在您具有极高维度的情况下或者所涉及的算法在高维数据上表现不佳的情况下有所帮助,比如基因组数据或kNN等。

英文:

You dont really need PCA for such small dimensional problem. Decision trees perform very well even with thousands of variables.

Here are few things you can try

  1. Pass a watchlist and train up until you are not overfitting on validation set. https://github.com/dmlc/xgboost/blob/2d95b9a4b6d87e9f630c59995403988dee390c20/demo/guide-python/basic_walkthrough.py#L64
  2. try all sine cosine transformations and other one hot encoding together and make a model (along with watchlist)
  3. Looks for more causal data. Just seasonal patterns does not cause air fare fluctuations. For starting you can add flags for festivals, holidays, important dates. Also do feature engineering for proximities to these days. Weather data is also easy to find and add.

PCA usually help in cases where you have extreme dimensionality like genome data or algorithm involved doesnt do well in high dimensional data like kNN etc.

huangapple
  • 本文由 发表于 2020年1月6日 23:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定