我在来自Kaggle的泰坦尼克号数据集上拟合了CatBoost模型:
train_df = pd.read_csv('input/train.csv')
test_df = pd.read_csv('input/test.csv')
train_df.fillna(-999, inplace=True)
test_df.fillna(-999, inplace=True)
x = train_df.drop('Survived', axis=1)
y = train_df.Survived
cate_features_index = np.where(x.dtypes != float)[0]
xtrain, xtest, ytrain, ytest = train_test_split(x, y, train_size=.85, random_state=1234)
model = CatBoostClassifier(eval_metric='Accuracy', use_best_model=True, random_seed=42)
model.fit(xtrain, ytrain, cat_features=cate_features_index, eval_set=(xtest, ytest))
如果我的对象具有与train_df
相同的功能顺序,则代码可以正常工作
{
"PassengerId": "892",
"Pclass": "3",
"Name": "Kelly, Mr. James",
"Sex": "female",
"Age": "34.5",
"SibSp": "0",
"Parch": "0",
"Ticket": "330911",
"Fare": "7.8292",
"Cabin": "",
"Embarked": "Q",
}
但是如果我改变顺序,例如:
{
"Age": "34.5",
"Cabin": "",
"Embarked": "Q",
"Fare": "7.8292",
"Name": "Kelly, Mr. James",
"Parch": "0",
"PassengerId": "892",
"Pclass": "3",
"Sex": "female",
"SibSp": "0",
"Ticket": "330911"
}
我得到一个错误
_catboost.CatBoostError: Bad value for num_feature[non_default_doc_idx=0,feature_idx=4]="Kelly, Mr. James": Cannot convert 'b'Kelly, Mr. James'' to float
是否有可能在没有所需特征顺序的情况下拟合模型?
发布于 2021-05-08 14:03:00
您可以使用分类要素的名称而不是其索引来指定分类要素,在这种情况下,它们在数据框中的顺序将无关紧要。
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from catboost import CatBoostClassifier
# import the data
df_train = pd.read_csv('train.csv')
df_test = pd.read_csv('test.csv')
# extract the features and target
X = df_train.drop('Survived', axis=1)
y = df_train['Survived']
# extract the names of the categorical features
cat_features = X.columns[np.where(X.dtypes != float)[0]].values.tolist()
print(cat_features)
# ['PassengerId', 'Pclass', 'Name', 'Sex', 'SibSp', 'Parch', 'Ticket', 'Cabin', 'Embarked']
# make sure that the categorical features are encoded as strings
X[cat_features] = X[cat_features].astype(str)
# split the data
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.85, random_state=1234)
# train the model
model = CatBoostClassifier(eval_metric='Accuracy', use_best_model=True, random_seed=1234)
model.fit(X_train, y_train, cat_features=cat_features, eval_set=(X_valid, y_valid))
print('Best Iteration: {}'.format(model.best_iteration_))
print('Training Accuracy: {:.2%}'.format(model.best_score_['learn']['Accuracy']))
print('Validation Accuracy: {:.2%}'.format(model.best_score_['validation']['Accuracy']))
# Best Iteration: 347
# Training Accuracy: 96.96%
# Validation Accuracy: 85.07%
# generate the model predictions
df_test[cat_features] = df_test[cat_features].astype(str)
y_pred = model.predict(df_test)
print(y_pred)
# [0 0 0 0 0 0 1 0 1 . . . 0 1 0 1 1 0 0 1 0 0 1]
https://stackoverflow.com/questions/67414615
复制相似问题