Skip to content

Commit

Permalink
[python] use f-strings for concatenation in examples/python-guide/adv…
Browse files Browse the repository at this point in the history
…anced_example.py (#4386)

* Improved the syntax of the fstrings

* Improved the strings to fstrings

* Reverted back the white space.

* Update examples/python-guide/advanced_example.py

Co-authored-by: Nikita Titov <[email protected]>

Co-authored-by: Nikita Titov <[email protected]>
  • Loading branch information
sayantan1410 and StrikerRUS authored Jun 23, 2021
1 parent bd21efe commit 4720889
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions examples/python-guide/advanced_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}

# generate feature names
feature_name = ['feature_' + str(col) for col in range(num_feature)]
feature_name = [f'feature_{col}' for col in range(num_feature)]

print('Starting training...')
# feature_name and categorical_feature
Expand All @@ -56,7 +56,7 @@

print('Finished first 10 rounds...')
# check feature name
print('7th feature name is:', lgb_train.feature_name[6])
print(f'7th feature name is: {lgb_train.feature_name[6]}')

print('Saving model...')
# save model to file
Expand All @@ -70,18 +70,19 @@
json.dump(model_json, f, indent=4)

# feature names
print('Feature names:', gbm.feature_name())
print(f'Feature names: {gbm.feature_name()}')

# feature importances
print('Feature importances:', list(gbm.feature_importance()))
print(f'Feature importances: {list(gbm.feature_importance())}')

print('Loading model to predict...')
# load model to predict
bst = lgb.Booster(model_file='model.txt')
# can only predict with the best iteration (or the saving iteration)
y_pred = bst.predict(X_test)
# eval with loaded model
print("The rmse of loaded model's prediction is:", mean_squared_error(y_test, y_pred) ** 0.5)
rmse_loaded_model = mean_squared_error(y_test, y_pred) ** 0.5
print(f"The RMSE of loaded model's prediction is: {rmse_loaded_model}")

print('Dumping and loading model with pickle...')
# dump model with pickle
Expand All @@ -93,7 +94,8 @@
# can predict with any iteration when loaded in pickle way
y_pred = pkl_bst.predict(X_test, num_iteration=7)
# eval with loaded model
print("The RMSE of pickled model's prediction is:", mean_squared_error(y_test, y_pred) ** 0.5)
rmse_pickled_model = mean_squared_error(y_test, y_pred) ** 0.5
print(f"The RMSE of pickled model's prediction is: {rmse_pickled_model}")

# continue training
# init_model accepts:
Expand Down Expand Up @@ -187,8 +189,7 @@ def accuracy(preds, train_data):
feval=[binary_error, accuracy],
valid_sets=lgb_eval)

print('Finished 50 - 60 rounds with self-defined objective function '
'and multiple self-defined eval metrics...')
print('Finished 50 - 60 rounds with self-defined objective function and multiple self-defined eval metrics...')

print('Starting a new training job...')

Expand Down

0 comments on commit 4720889

Please sign in to comment.