You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
8 Hrs
13 Min
27 Seg

Entrenamiento y evaluación de regresión logística multiclase

15/17
Resources

How to train a multiclass logistic regression model?

Logistic regression is one of the most widely used techniques in data classification. It allows to efficiently categorize a set of data into several classes, facilitating the understanding of the behavior of the data. In this sense, we are going to explain how to train a multiclass logistic regression model using LogisticRegression from the Scikit-learn Python library by using parameters such as solver, multi_class, and C, as well as iterating over different combinations to obtain the best possible model.

What steps are followed to create the model?

To begin, it is necessary to define the variables and parameters that will be used in training the model. The steps are:

  1. Define the model: We use LogisticRegression by specifying key parameters. An example is the random state to ensure repeatable results.

    from sklearn.linear_model import LogisticRegression
    logistic_regression_model = LogisticRegression( random_state=42,  solver='saga',  multi_class='multinomial', n_jobs=-1,  C=1.0)
  2. Create a function: To dynamically manage the parameters, we can create a function that accepts the parameters C, solver and multi_class.

    def logistic_model(C, solver, multi_class): return LogisticRegression(  C=C,  solver=solver,  multi_class=multi_class, n_jobs=-1, random_state=42 )
  3. Train the model: Once defined, train the model with the training data and make predictions.

    model = logistic_model(1, 'saga', 'multinomial')model.fit(X_train, y_train)predictions = model.predict(X_test)
  4. Evaluate results: It is crucial to evaluate the accuracy of the model using metrics such as the confusion matrix and the accuracy score.

    from sklearn.metrics import confusion_matrix, accuracy_score
    cm = confusion_matrix(y_test, predictions)accuracy = accuracy_score(y_test, predictions)print('Confusion Matrix:\n', cm)print('Accuracy:', accuracy)

How to improve the model?

A good practice to optimize the model is to try different combinations of solver and multi_class and see which one gives better results.

  1. Iterate over combinations: Use loops to iterate through possible values for multi_class and solver.

    multiclass_options = ['ovr', 'multinomial']solver_list = ['newton-cg', 'saga', 'liblinear', 'sag']
    best_score = 0best_params = {}
    for mc in multiclass_options: for solver in solver_list: try: model = logistic_model(1, solver, mc) model.fit(X_train, y_train) predictions = model.predict(X_test)
     accuracy = accuracy_score(y_test, predictions) if accuracy > best_score: best_score = accuracy best_params = {'solver': solver, 'multi_class':  mc}
     except Exception as e: # Handle exceptions for incompatible configurations continue
    print('Best Score:', best_score)print('Best Params:', best_params)
  2. Visualize the results: Use graphs to analyze the results obtained to select the most appropriate model.

    import matplotlib.pyplot as pltimport seaborn as sns
    sns.barplot(x=best_params.keys(),  y=best_params.values())plt.title('Scores with different solvers and multi_class options')plt.xticks(rotation=90)plt.show()

This process may seem exhaustive, but it is crucial to understand the performance of each configuration and select the best model for multi-classification.

Why is hyperparameter tuning important?

Adjusting the hyperparameters allows you to:

  • Obtain a more accurate model: by increasing the correct classification rate.
  • Improve computational efficiency: Adapting resources to the problem.
  • Increase the robustness of the model: Against noise and outliers.

The key to success in multiclass logistic regression lies in performing a thorough analysis of the results and adjusting the parameters appropriately. In this way, we can guarantee the implementation of a model that not only fulfills the classification task, but does so with a high degree of accuracy. Keep exploring and improving your models to achieve better performance in your machine learning projects!

Contributions 10

Questions 4

Sort by:

Want to see more contributions, questions and answers from the community?

Una forma más sencilla de optimizar los hipeparámetros del modelo de Regresión Lineal es usando GridSearchCV

Esto también se usa para otros modelos de regresión y clasificación, lo que hizo el profesor es basicamente lo que hace este modelo por dentro, un loop de varios ciclos for

En la figura de abajo se puede observar el mejor acuracy obtenido , nótese que debería de ser 0.93 y no 0.99 como se muestra en el video por un ligero error al darle una columna extra al set de X el cual explique en la clase anterior.

El modelo clf ya se encuentra entrenado con los mejores hiperparámetros por lo que yase encuentra listo para hacer predicciones

Con este código podemos hacer que se muestre el scoring en la grafica de barras, para ver mejor el accuracy:

fig, ax1 = plt.subplots(figsize=(10,5))

ax = sns.barplot(
        ax=ax1,
        x  = params,
        y = scores
        )
ax.set_title('Beans Accuracy')
ax.set_xticklabels(labels = ax.get_xticklabels(), rotation=66)
ax.bar_label(ax.containers[0])
plt.show()

pensando que habia overfiting use cross validation para verificar:

from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score

kfold = KFold(n_splits=5)

results = cross_val_score(model, X_test, y_test, cv=kfold)
print(results.mean())

0.9863479638430714

el score es muy similar, lo que nos indicaria que no hay Overfiting

🔍 Acercamiento para ver mejor la diferencia:

Overfited but good class…

😄

Me puse a iterar y evitar que salgan las alertas. La verdad es que visualmente rompen mucho la vista. ```python import warnings warnings.filterwarnings("ignore") multiclass = ['ovr', 'multinomial'] solver_list = ['liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga'] scores = [] params = [] for i in multiclass: for j in solver_list: try: model = LogisticRegression(multi_class=i, solver=j, max_iter=1000) model.fit(X_train, y_train) predictions = model.predict(X_test) params.append(i + ' + ' + j) accuracy = accuracy_score(y_test, predictions) scores.append(accuracy) print(f'Model: {i} + {j} | Accuracy: {accuracy:.4f}') except Exception as e: print(f'Error with model: {i} + {j} | {e}') warnings.filterwarnings("default") ``` ![](https://static.platzi.com/media/user_upload/image-5b39be8a-303a-4b60-8cc9-ff6eb8441b82.jpg)
Predicción para nuevos valores (aleatorios dentro del rango) + guardar el modelo final: ```js # rangos perimeter = [524.736000, 1985.37] EquivDiameter= [161.243764, 569.374358] Solidity = [0.919246, 0.994677] Roundness = [0.489618, 0.990685] ShapeFactor1 = [0.002778, 0.010451] ShapeFactor2 = [0.000564, 0.003665] list_cols = [perimeter, EquivDiameter, Solidity, Roundness, ShapeFactor1, ShapeFactor2] dict_cols = {} #valores random dentro del rango de cada columna for i in range(len(df_bean_over.columns)-1): # sin la predictora :O dict_cols[df_bean_over.columns[i]] = np.random.uniform(list_cols[i][0], list_cols[i][1]) new_data = pd.DataFrame(dict_cols, index= [0]) st_new_data = scaler.transform(new_data) # escalamiento pred_new_data = best_model.predict(st_new_data) # predicción prob_new_data = best_model.predict_proba(st_new_data) # probabilidad prob_new_data # probabilidad de cada clase dict_prob = {} for i in range(len(best_model.classes_)): dict_prob[best_model.classes_[i]] = prob_new_data[0][i] categories= ['BARBUNYA', 'BOMBAY', 'CALI', 'DERMASON', 'HOROZ', 'SEKER', 'SIRA'] encoding_nums = [1, 2, 3, 4, 5, 6, 7] print(f'probabilities:\n{dict_prob}\n\n', #probabilidad de que el dato pertenezca a cada 'Class' f'prediction: {pred_new_data}-> {categories[np.where(encoding_nums == pred_new_data)[0][0]]}') #predicción # guardamos el modelo def save_model(model, scaler, name): joblib.dump(model, name + '.pkl') joblib.dump(scaler, name + '_scaler.pkl') print('model saved :D') #save_model(model= best_model, scaler= scaler, name= 'logistic_regression_dry_bean') ```
Por qué obtenemos 0.99 de accuracy? Bueno en la clase anterior escribimos: ```js df_dea = X_over df_dea['Class'] = y_over ``` Debido a esto implícitamente estamos agregando la columna objetivo ('Class') en la variable predictora (X\_over). Es decir estamos incluyendo nuestra variable objetivo como variable predictora, la forma de solucionar esto es cambiando el código mencionado por: ```python df_dea = X_over.copy(deep = True) df_dea['Class'] = y_over ```
Por qué obtenemos 0.99 de accuracy? Bueno en la clase anterior escribimos: ```python df_dea = X_over df_dea['Class'] = y_over ```Debido a esto implícitamente estamos agregando la columna objetivo ('Class') en la variable predictora (X\_over)![]()![](https://imgur.com/a/XX3qD0z)![](https://imgur.com/a/XX3qD0z)!\[]\(https://imgur.com/a/XX3qD0z)Es decir estamos incluyendo nuestra variable objetivo como variable predictora, la forma de solucionar esto es cambiando el código mencionado por: df\_dea = X\_over.copy(deep = True) df\_dea\['Class'] = y\_over```python df_dea = X_over.copy(deep = True) df_dea['Class'] = y_over ``` ![](file:///C:/Users/Angel/Desktop/Captura%20de%20pantalla%202024-07-09%20194944.jpg)![]()