# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# +
from __future__ import print_function
from distutils.version import LooseVersion as Version
import sys

OK = '\x1b[42m[ OK ]\x1b[0m'
FAIL = "\x1b[41m[FAIL]\x1b[0m"

try:
    import importlib
except ImportError:
    print(FAIL, "Python version 3.9 is required,"
                " but %s is installed." % sys.version)

def import_version(pkg, min_ver, fail_msg=""):
    mod = None
    try:
        mod = importlib.import_module(pkg)
        if pkg in {'PIL'}:
            ver = mod.VERSION
        else:
            ver = mod.__version__
        if Version(ver) == min_ver:
            print(OK, "%s version %s is installed."
                  % (lib, min_ver))
        else:
            print(FAIL, "%s version %s is required, but %s installed."
                  % (lib, min_ver, ver))    
    except ImportError:
        print(FAIL, '%s not installed. %s' % (pkg, fail_msg))
    return mod


# first check the python version
pyversion = Version(sys.version)
if pyversion >= "3.9":
    print(OK, "Python version is %s" % sys.version)
elif pyversion < "3.9":
    print(FAIL, "Python version 3.9 is required,"
                " but %s is installed." % sys.version)
else:
    print(FAIL, "Unknown Python version: %s" % sys.version)

    
print()
requirements = {'numpy': "1.21.1", 'matplotlib': "3.4.2",'sklearn': "0.24.2", 
                'pandas': "1.3.1",'xgboost': "1.3.3", 'shap': "0.39.0"}

# now the dependencies
for lib, required_version in list(requirements.items()):
    import_version(lib, required_version)

# +
## import packages
import numpy as np
import pandas as pd
import warnings
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.special import jn
from IPython.display import display, clear_output
import time

warnings.filterwarnings('ignore')
# %matplotlib inline

## model prediction
from sklearn import linear_model

from sklearn import preprocessing
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
## Reduced features
from sklearn.decomposition import PCA,FastICA,FactorAnalysis,SparsePCA

import lightgbm as lgb
import xgboost as xgb

## Param serach and evaluation
from sklearn.model_selection import GridSearchCV,cross_val_score,StratifiedKFold,train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error

# +

Train_data = pd.read_csv(r'../data/used_car_train.csv',sep=' ')
Test_data = pd.read_csv(r'../data/used_car_testB.csv',sep=' ')

print('Train data shape:',Train_data.shape)
print('Test data shape:',Test_data.shape)

# -

Train_data.head()

Train_data.info()

Train_data.columns

Test_data.info()

Train_data.describe()

Test_data.describe()

numerical_cols = Train_data.select_dtypes(exclude = 'object').columns
print(numerical_cols)

categorical_cols = Train_data.select_dtypes(include = 'object').columns
print(categorical_cols)

# +
## 选择特征列
feature_cols = [col for col in numerical_cols if col not in ['SaleID','name','regDate','creatDate','price','model','brand','regionCode','seller']]
feature_cols = [col for col in feature_cols if 'Type' not in col]

## 提前特征列，标签列构造训练样本和测试样本
X_data = Train_data[feature_cols]
Y_data = Train_data['price']

X_test  = Test_data[feature_cols]

print('X train shape:',X_data.shape)
print('X test shape:',X_test.shape)
# -

plt.hist(Y_data)
plt.show()
plt.close()

X_data = X_data.fillna(-1)
X_test = X_test.fillna(-1)

# +
# ## xgb-Model
xgr = xgb.XGBRegressor(n_estimators=2, max_depth=5)

scores_train = []
scores = []


xgr.fit(X_data,Y_data)
pred_train_xgb=xgr.predict(X_data)

score_train = r2_score(Y_data,pred_train_xgb)
scores_train.append(score_train)

print('Train r2:',score_train)



# +
# ## xgb-Model
xgr = xgb.XGBRegressor(n_estimators=2,max_depth = 8)

scores_train = []
scores = []


xgr.fit(X_data,Y_data)
pred_train_xgb=xgr.predict(X_data)

score_train = r2_score(Y_data,pred_train_xgb)
scores_train.append(score_train)

print('Train r2:',np.mean(score_train))
# -

a= ([0.530133664028601,0.5588657862577329])
np.std(a)

np.mean(a)



StarcoderdataJupyterScriptsDedupFiltered