Search code examples
python-3.xmachine-learningdeep-learningpycharmlogistic-regression

What is Coef_.T? Purpose of using T


Coef_ is used to find coefficients in linear equations in Python. But Coef_, which I could not find the answer to, was put at the end of .T. What is the .T function here?

for C, marker in zip([0.001, 1, 100], ['o', '^', 'v']):
 lr_l1 = LogisticRegression(C=C, penalty="l1").fit(X_train, y_train)
 print("Training accuracy of l1 logreg with C={:.3f}: {:.2f}".format(
 C, lr_l1.score(X_train, y_train)))
 print("Test accuracy of l1 logreg with C={:.3f}: {:.2f}".format(
 C, lr_l1.score(X_test, y_test)))
 plt.plot(lr_l1.coef_.T, marker, label="C={:.3f}".format(C))

Solution

  • ".T" method means Transpose which switches rows & columns

    if you have a matrix m:

    [1 2 3
    4 5 6
    7 8 9]

    Then m.T would be:

    [1 4 7
    2 5 8
    3 6 9]

    It looks like its used in this line:

    plt.plot(lr_l1.coef_.T,...)

    to make sure it plots the coefficients in an expected way. If the model was built from sklearn LogisticRegression, then you can review the docs here coef_ has shape (n_classes,n_features), so that means
    coef_.T has shape (n_features,n_classes)

    Here is a notebook that shows how this works

    enter image description here