<jupyter_start><jupyter_text><NAME> MpGlue Preparing data and testing model parameters --- Ranking and optimizing---<jupyter_code>import mpglue as gl

CL = gl.classification()
# Print help documentation for the Chi-square 
#   test to rank feature importance.
print(help(CL.rank_feas))
samples = '../testing/data/08N_points_merged.txt'

# Sample the data
CL.split_samples(samples)

# Rank the explanatory variables with a chi-square test
CL.rank_feas(rank_method='chi2', top_feas=0.2)
# Rank the explanatory variables with a Random Forest 
#   model and compare results.

# First, construct a Random Forest model.
CL.construct_model(classifier_info={'classifier': 'rf'})

# Use the RF model to rank feature importance.
CL.rank_feas(rank_method='rf', top_feas=0.2)<jupyter_output>11:37:01:INFO:4340:classification._train_model:  Training a RF model with 4,783 samples and 41 variables ...<jupyter_text>--- Optimizing parameters---<jupyter_code>print help(CL.optimize_parameters)
# Find the optimum parameters for a Random Forest,
#   using the default parameter list.
CL.optimize_parameters(samples, 
                       classifier_info={'classifier': 'rf'}, 
                       use_xy=True)<jupyter_output>Finding the best paramaters for a RF model ...<jupyter_text>--- Testing---<jupyter_code>emat = gl.error_matrix()

print(help(emat))
import numpy as np

# Create some random data
test_array = np.random.randn(100, 2).astype('uint8')

emat.get_stats(po_array=test_array)
print(dir(emat))
print(emat.accuracy)
print(emat.e_matrix)
print(emat.kappa_score)
print(emat.report)
print(emat.write_stats('datasets/my_report.txt'))<jupyter_output><empty_output><jupyter_text>--- Models in MpGlue---<jupyter_code># Check the available models.
print(CL.model_options())
# Load the samples
CL.split_samples(samples)
# Construct a Random Forest model with 100 trees.
CL.construct_model(classifier_info={'classifier': 'rf',
                                    'n_estimators': 100})

# The model is stored in `model`.
print(CL.model)
# Construct an extremely randomized Random Forest 
#   with 100 trees.
CL.construct_model(classifier_info={'classifier': 'ex-rf',
                                    'n_estimators': 100})

print(CL.model)
# Construct a boosted extremely randomized Random Forest with 
#   100 trees and 10 trials (boosts).
# CL.construct_model(classifier_info={'classifier': 'ab-ex-rf',
#                                     'n_estimators': 100,
#                                     'trials': 10})

# print(CL.model)<jupyter_output><empty_output><jupyter_text>* We trained one model in the examples above.* MpGlue also supports training ensemble models through Scikit-learn's `VotingClassifier` module.* To train a voting classifier, simply provide a list of classifiers instead of a string.<jupyter_code># Construct an ensemble voting model and 
#   save the model to file.
CL.construct_model(classifier_info={'classifier': ['rf', 'ex-rf', 'bayes', 'qda', 'nn'],
                                    'n_estimators': 100,
                                    'trials': 5},
                   output_model='voting_model.model')<jupyter_output>21:15:28:INFO:4466:classification._set_model:  Fitting a rf model ...
21:15:28:INFO:4466:classification._set_model:  Fitting a ex-rf model ...
21:15:28:INFO:4466:classification._set_model:  Fitting a bayes model ...
21:15:28:INFO:4466:classification._set_model:  Fitting a qda model ...
21:15:28:INFO:4466:classification._set_model:  Fitting a nn model ...
21:15:29:INFO:4466:classification._set_model:  Fitting a lightgbm model ...
21:15:29:INFO:5020:classification._train_model:  The model has already been trained as a voting model.
21:15:29:INFO:5184:classification._train_model:  Saving model to file ...
21:15:30:INFO:6659:classification.test_accuracy:  Getting test accuracy ...<jupyter_text>--- Making predictions on an image---<jupyter_code># Load the voting model.
CL.construct_model(input_model='voting_model.model')
# Apply the model to an image.
CL.predict('input_image.tif',
           'output_image.tif')

# *Note that the model could also be loaded with `predict`.
# This syntax would not require the `construct_model` method.
CL.predict('input_image.tif',
           'output_image.tif',
           input_model='voting_model.model')
# Apply the model to an image, adjusting the block size.
CL.predict('input_image.tif',
           'output_image.tif',
           row_block_size=2048,
           col_block_size=2048)
# Apply the model to an image, adjusting the number of parallel jobs.
CL.predict('input_image.tif',
           'output_image.tif',
           n_jobs=4,       # model parallel jobs
           n_jobs_vars=4)  # image band reading parallel jobs
# Apply the model to an image, and then apply
#   posterior probability label relaxation.
CL.predict('input_image.tif',
           'output_image.tif',
           relax_probabilities=True)<jupyter_output><empty_output><jupyter_text>* The default for `predict` is to apply predictions block by block, reading from one image and writing to one image.* However, sometimes the input image might be very large, making block writes to the output slow.* `predict` can write to individual blocks instead of to one image.* In the example below, individual blocks will be written as `/output_image_00001.tif`, `/output_image_00002.tif`, etc.<jupyter_code># Apply the model to an image, writing to individual blocks.
CL.predict('input_image.tif',
           'output_image.tif',
           write2blocks=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1086290758421906, 'fraction_numerical': 0.029724373986669068, 'mean_word_length': 3.798617113223855, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '121750', 'license': 'Apache-2.0', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1664, 'n_words': 471, 'path': 'mpglue/notebooks/models.ipynb', 'repo_name': 'siu-panh/mapeo-uso-del-suelo', 'size': '5560'}