Get feature names python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get feature names from a trained model, python #5275

get feature names from a trained model, python #5275

Comments

Hi i have a pre trained XGBoost CLassifier. I want to find out the name of features/the name of Dataframe columns with which it was trained to i can prepare a table with those features for my use. Can anyone tell me how this can be done. And would be nice if i could get the datatype that the feature expects(eg. int, float or str)

from xgboost import XGBClassifier clf = XGBClassifier(learning_rate =0.5, n_estimators=207, max_depth=2, min_child_weight=1, gamma=0.8, subsample=0.8, colsample_bytree=0.8, reg_alpha=0.001, objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27) 

So how do i get the feature names from this? Any hints would be appriciated

The text was updated successfully, but these errors were encountered:

Читайте также:  Pdf to clean html

@trivialfis I have updated my question. Sorry i forgot to add details the first time

No problem. Call get_booster() from clf . Then you have access to the Booster object, which contains the feature_names .

@trivialfis It works. Thanks. But it seems that instead of the original names, they have been converted to ‘f0′,’f1′,’f2’ etc. Any way to resture them to their original names?

Could you provide a self contained script? With pandas and cuDF, XGBoost should collect the feature names from training matrix. Also what’s your XGBoost version?

Unfortunately i cannot provide the script scince my company does not allow data shring online . but i can tell you my procedure. My XGBoost version is 0.90

df = pd.read_csv('mycsv.csv', encoding='ISO-8859-1', sep=',') #read csv X = df5.drop(['Z'], axis=1) #create training variables y = df5['Z'] #create target variables X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20 #Test Train split X_train_n = X_train.values #convert to np array X_test_n = X_test.values #convert to np array clf = XGBClassifier(learning_rate =0.5, n_estimators=207, max_depth=2, min_child_weight=1, gamma=0.8, subsample=0.8, colsample_bytree=0.8, reg_alpha=0.001, objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27) 

The procedure above was done by someone else. My job is to get the feature names from the classifier

clf = xg.get_booster() print(clf.feature_names) 
[‘f0’, ‘f1’, ‘f2’, ‘f3’, ‘f4’, ‘f5’, ‘f6’, ‘f7’, ‘f8’, ‘f9’, ‘f10’, ‘f11’, ‘f12’, ‘f13’, ‘f14’, ‘f15’]

Источник

Оцените статью