python logistic regression - patsy design matrix and categorical data
<p>Quite new to python and machine learning. </p>

<p>I am trying to build a logistic regression model. I have worked in R to gain lambda and used cross-validation to find the best model and am now moving it into python.  </p>

<p>Here I have created a design matrix and made it sparse. Then ran the logistic regression. It seems to be working. </p>

<p>My question is, since I have stated my term item_number is a category how to I know which has become the dummy variable? And how do I know which coefficient goes with each category name?</p>

<pre><code>from patsy import dmatrices
from sklearn.linear_model import LogisticRegression
from sklearn import preprocessing


def train_model (data, frm, Rlambda):
    y, X = dmatrices(frm , data, return_type="matrix")
    y = np.ravel(y)

    scaler = sklearn.preprocessing.MaxAbsScaler(copy=False)
    X_trans = scaler.fit_transform(X)

    model = LogisticRegression(penalty ='l2', C=1/Rlambda)
    model = model.fit(X_trans, y)

frm = 'purchase ~ price + C(item_number)'
Rlambda = 0.01
model, train_score = train_model(data1,frm,Rlambda)
</code></pre>

<p>First I will fix an error with your code and then I will answer your question.</p>

<p>Your code:
Your <code>train_model</code> function won't return what you think it returns. Currently, it doesn't return anything, and you want it to return both your model and the training score. When you fit a model, you need to define what you mean by the training score - the model won't return anything to you by default. For now let's just return the model that you trained.</p>

<p>So you should update your <code>train_model</code> function as follows:</p>

<pre><code>def train_model (data, frm, Rlambda):
    y, X = dmatrices(frm , data, return_type="matrix")
    y = np.ravel(y)

    scaler = sklearn.preprocessing.MaxAbsScaler(copy=False)
    X_trans = scaler.fit_transform(X)

    model = LogisticRegression(penalty ='l2', C=1/Rlambda)

    # model.fit() operates in-place
    model.fit(X_trans, y)

    return model
</code></pre>

<p>Now when you want to determine what variables correspond to, <code>model.coef_</code> returns you all the coefficients in the decision function, of size <code>(n_classes, n_features)</code>. The order of the coefficients correspond to the order that your features were passed into the <code>.fit()</code> method. So in your case, <code>X_trans</code> is the design matrix of size <code>(n_samples, n_features)</code>, so each of the coefficients in <code>model.coef_</code> exactly correspond to the coefficients for each of the <code>n_features</code> in <code>X</code> in the same order they are presented in <code>X</code>.</p>

python scikit-learn patsy