<p dir="auto">When I make predictions on two samples, separately and appended to one another, I get different results.  For example, this is as simple as it get (both X_train and y_train are pandas DataFrames):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="model = linear_model.LinearRegression(normalize=True)
model.fit(X_train, y_train)"><pre class="notranslate"><code class="notranslate">model = linear_model.LinearRegression(normalize=True)
model.fit(X_train, y_train)
</code></pre></div>
<p dir="auto">But when I predict, I get inconsistent results.  Predicting for a and b separately:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="mode.predict(a)
--&gt; array([[ -4.91992866e+19],
           [ -9.44975142e+18],
           [ -6.28885430e+19],
           [ -5.99072700e+19],
           [ -9.97222845e+19]])

model.predict(b):
--&gt; array([[ 273408.]])"><pre class="notranslate"><code class="notranslate">mode.predict(a)
--&gt; array([[ -4.91992866e+19],
           [ -9.44975142e+18],
           [ -6.28885430e+19],
           [ -5.99072700e+19],
           [ -9.97222845e+19]])

model.predict(b):
--&gt; array([[ 273408.]])
</code></pre></div>
<p dir="auto">Predicting for the two together:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="model.predict(a.append(b))
--&gt; array([[  1.82526373e+17],
           [  1.82526373e+17],
           [  4.86912000e+05],
           [  2.41920000e+05],
           [ -1.45384048e+18],
           [  2.73408000e+05]])"><pre class="notranslate"><code class="notranslate">model.predict(a.append(b))
--&gt; array([[  1.82526373e+17],
           [  1.82526373e+17],
           [  4.86912000e+05],
           [  2.41920000e+05],
           [ -1.45384048e+18],
           [  2.73408000e+05]])
</code></pre></div>
<p dir="auto">Which is completely different, except for b, which is the same result.  This also seems to be model agnostic as far as I can tell (although all within linear regression).</p>
<h4 dir="auto">Description</h4>

<p dir="auto">Sometimes it is convenient to first build a model on a recent dataset, save it as a .pkl file and then apply the model to the new dataset. However, in the last project, my friends and I found that the results turned quite wired after applying the .pkl file on the new dataset. Actually, we implemented a binary classifier. We found the probability distribution turned from unimodal distribution to bimodal distribution. Finally, we found out the problem was that the column order of the new dataset was different from the old one. Thus the predictions were totally wrong.<br>
I have checked the source code and discovered that the fit function of sklean didn't save the column values during the process of model training. Thus there was no mean to check whether the column values were consistent during the processing of prediction. We thought it would be better if the column values could be saved during training and then be used to check the column values during predicting.</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#for simplification, consider a very simple case
from sklearn.datasets import load_iris
import pandas as pd

#make a dataframe
iris = load_iris()
X, y = iris.data[:-1,:], iris.target[:-1]
iris_pd = pd.DataFrame(X)
iris_pd.columns = iris.feature_names
iris_pd['target'] = y

from sklearn.cross_validation import train_test_split
train, test = train_test_split(iris_pd, test_size= 0.3)

feature_columns_train = ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
feature_columns_test = ['sepal length (cm)','sepal width (cm)','petal width (cm)','petal length (cm)']

from sklearn.linear_model import LogisticRegression
lg = LogisticRegression(n_jobs=4, random_state=123, verbose=0, penalty='l1', C=1.0)
lg.fit(train[feature_columns_train], train['target'])

prob1 = lg.predict_proba(test[feature_columns_train])
prob2 = lg.predict_proba(test[feature_columns_test])"><pre class="notranslate"><span class="pl-c">#for simplification, consider a very simple case</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">datasets</span> <span class="pl-k">import</span> <span class="pl-s1">load_iris</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-c">#make a dataframe</span>
<span class="pl-s1">iris</span> <span class="pl-c1">=</span> <span class="pl-en">load_iris</span>()
<span class="pl-v">X</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">iris</span>.<span class="pl-s1">data</span>[:<span class="pl-c1">-</span><span class="pl-c1">1</span>,:], <span class="pl-s1">iris</span>.<span class="pl-s1">target</span>[:<span class="pl-c1">-</span><span class="pl-c1">1</span>]
<span class="pl-s1">iris_pd</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-v">X</span>)
<span class="pl-s1">iris_pd</span>.<span class="pl-s1">columns</span> <span class="pl-c1">=</span> <span class="pl-s1">iris</span>.<span class="pl-s1">feature_names</span>
<span class="pl-s1">iris_pd</span>[<span class="pl-s">'target'</span>] <span class="pl-c1">=</span> <span class="pl-s1">y</span>

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">cross_validation</span> <span class="pl-k">import</span> <span class="pl-s1">train_test_split</span>
<span class="pl-s1">train</span>, <span class="pl-s1">test</span> <span class="pl-c1">=</span> <span class="pl-en">train_test_split</span>(<span class="pl-s1">iris_pd</span>, <span class="pl-s1">test_size</span><span class="pl-c1">=</span> <span class="pl-c1">0.3</span>)

<span class="pl-s1">feature_columns_train</span> <span class="pl-c1">=</span> [<span class="pl-s">'sepal length (cm)'</span>,<span class="pl-s">'sepal width (cm)'</span>,<span class="pl-s">'petal length (cm)'</span>,<span class="pl-s">'petal width (cm)'</span>]
<span class="pl-s1">feature_columns_test</span> <span class="pl-c1">=</span> [<span class="pl-s">'sepal length (cm)'</span>,<span class="pl-s">'sepal width (cm)'</span>,<span class="pl-s">'petal width (cm)'</span>,<span class="pl-s">'petal length (cm)'</span>]

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">linear_model</span> <span class="pl-k">import</span> <span class="pl-v">LogisticRegression</span>
<span class="pl-s1">lg</span> <span class="pl-c1">=</span> <span class="pl-v">LogisticRegression</span>(<span class="pl-s1">n_jobs</span><span class="pl-c1">=</span><span class="pl-c1">4</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">123</span>, <span class="pl-s1">verbose</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">penalty</span><span class="pl-c1">=</span><span class="pl-s">'l1'</span>, <span class="pl-v">C</span><span class="pl-c1">=</span><span class="pl-c1">1.0</span>)
<span class="pl-s1">lg</span>.<span class="pl-en">fit</span>(<span class="pl-s1">train</span>[<span class="pl-s1">feature_columns_train</span>], <span class="pl-s1">train</span>[<span class="pl-s">'target'</span>])

<span class="pl-s1">prob1</span> <span class="pl-c1">=</span> <span class="pl-s1">lg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_train</span>])
<span class="pl-s1">prob2</span> <span class="pl-c1">=</span> <span class="pl-s1">lg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_test</span>])</pre></div>
<h4 dir="auto">Expected Results</h4>

<p dir="auto">Because feature_columns_test is different from feature_columns_train, it is not surprised that prob1 is totally different from prob2 and prob1 should be the right result.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="prob1[:5] = 
array([[  3.89507414e-04,   3.20099743e-01,   6.79510750e-01],
         [  4.63256526e-04,   4.65385156e-01,   5.34151587e-01],
         [  8.79704318e-01,   1.20295572e-01,   1.10268420e-07],
         [  7.80611983e-01,   2.19385827e-01,   2.19046022e-06],
         [  2.78898454e-02,   7.77243988e-01,   1.94866167e-01]])"><pre class="notranslate"><span class="pl-s1">prob1</span>[:<span class="pl-c1">5</span>] <span class="pl-c1">=</span> 
<span class="pl-en">array</span>([[  <span class="pl-c1">3.89507414e-04</span>,   <span class="pl-c1">3.20099743e-01</span>,   <span class="pl-c1">6.79510750e-01</span>],
         [  <span class="pl-c1">4.63256526e-04</span>,   <span class="pl-c1">4.65385156e-01</span>,   <span class="pl-c1">5.34151587e-01</span>],
         [  <span class="pl-c1">8.79704318e-01</span>,   <span class="pl-c1">1.20295572e-01</span>,   <span class="pl-c1">1.10268420e-07</span>],
         [  <span class="pl-c1">7.80611983e-01</span>,   <span class="pl-c1">2.19385827e-01</span>,   <span class="pl-c1">2.19046022e-06</span>],
         [  <span class="pl-c1">2.78898454e-02</span>,   <span class="pl-c1">7.77243988e-01</span>,   <span class="pl-c1">1.94866167e-01</span>]])</pre></div>
<h4 dir="auto">Actual Results</h4>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="prob2[:5] = 
array([[  4.36321678e-01,   2.25057553e-04,   5.63453265e-01],
         [  4.92513658e-01,   1.76391882e-05,   5.07468703e-01],
         [  9.92946715e-01,   7.05167151e-03,   1.61346947e-06],
         [  9.83726756e-01,   1.62387090e-02,   3.45348884e-05],
         [  5.01392274e-01,   5.37144591e-04,   4.98070581e-01]])"><pre class="notranslate"><span class="pl-s1">prob2</span>[:<span class="pl-c1">5</span>] <span class="pl-c1">=</span> 
<span class="pl-en">array</span>([[  <span class="pl-c1">4.36321678e-01</span>,   <span class="pl-c1">2.25057553e-04</span>,   <span class="pl-c1">5.63453265e-01</span>],
         [  <span class="pl-c1">4.92513658e-01</span>,   <span class="pl-c1">1.76391882e-05</span>,   <span class="pl-c1">5.07468703e-01</span>],
         [  <span class="pl-c1">9.92946715e-01</span>,   <span class="pl-c1">7.05167151e-03</span>,   <span class="pl-c1">1.61346947e-06</span>],
         [  <span class="pl-c1">9.83726756e-01</span>,   <span class="pl-c1">1.62387090e-02</span>,   <span class="pl-c1">3.45348884e-05</span>],
         [  <span class="pl-c1">5.01392274e-01</span>,   <span class="pl-c1">5.37144591e-04</span>,   <span class="pl-c1">4.98070581e-01</span>]])</pre></div>
<h4 dir="auto">Versions</h4>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Linux-2.6.32-642.1.1.el6.x86_64-x86_64-with-redhat-6.7-Santiago
('Python', '2.7.11 |Anaconda 2.4.1 (64-bit)| (default, Dec  6 2015, 18:08:32) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]')
('NumPy', '1.10.1')
('SciPy', '0.16.0')
('Scikit-Learn', '0.17')"><pre class="notranslate"><span class="pl-v">Linux</span><span class="pl-c1">-</span><span class="pl-c1">2.6</span>.<span class="pl-c1">32</span><span class="pl-c1">-</span><span class="pl-c1">642.1</span>.<span class="pl-c1">1.</span><span class="pl-s1">el6</span>.<span class="pl-s1">x86_64</span><span class="pl-c1">-</span><span class="pl-s1">x86_64</span><span class="pl-c1">-</span><span class="pl-s1">with</span><span class="pl-c1">-</span><span class="pl-s1">redhat</span><span class="pl-c1">-</span><span class="pl-c1">6.7</span><span class="pl-c1">-</span><span class="pl-v">Santiago</span>
(<span class="pl-s">'Python'</span>, <span class="pl-s">'2.7.11 |Anaconda 2.4.1 (64-bit)| (default, Dec  6 2015, 18:08:32) <span class="pl-cce">\n</span>[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'</span>)
(<span class="pl-s">'NumPy'</span>, <span class="pl-s">'1.10.1'</span>)
(<span class="pl-s">'SciPy'</span>, <span class="pl-s">'0.16.0'</span>)
(<span class="pl-s">'Scikit-Learn'</span>, <span class="pl-s">'0.17'</span>)</pre></div>

<h2 dir="auto">The probable solution</h2>
<p dir="auto">I also implement a very simple solution. Hope this would help. :)</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class SafeLogisticRegression(LogisticRegression):
    def fit(self, X, y, sample_weight=None):
        self.columns = X.columns
        LogisticRegression.fit(self, X, y, sample_weight=None)
    def predict_proba(self, X):
        new_columns = list(X.columns)
        old_columns = list(self.columns)
        if new_columns != old_columns:
            if len(new_columns) == len(old_columns):
                try:
                    X = X[old_columns]
                    print &quot;The order of columns has changed. Fixed.&quot;
                except:
                    raise ValueError('The columns has changed. Please check.')
            else:
                raise ValueError('The number of columns has changed.')
        return LogisticRegression.predict_proba(self, X)"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">SafeLogisticRegression</span>(<span class="pl-v">LogisticRegression</span>):
    <span class="pl-k">def</span> <span class="pl-en">fit</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>, <span class="pl-s1">y</span>, <span class="pl-s1">sample_weight</span><span class="pl-c1">=</span><span class="pl-c1">None</span>):
        <span class="pl-s1">self</span>.<span class="pl-s1">columns</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>.<span class="pl-s1">columns</span>
        <span class="pl-v">LogisticRegression</span>.<span class="pl-en">fit</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>, <span class="pl-s1">y</span>, <span class="pl-s1">sample_weight</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)
    <span class="pl-k">def</span> <span class="pl-en">predict_proba</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>):
        <span class="pl-s1">new_columns</span> <span class="pl-c1">=</span> <span class="pl-en">list</span>(<span class="pl-v">X</span>.<span class="pl-s1">columns</span>)
        <span class="pl-s1">old_columns</span> <span class="pl-c1">=</span> <span class="pl-en">list</span>(<span class="pl-s1">self</span>.<span class="pl-s1">columns</span>)
        <span class="pl-k">if</span> <span class="pl-s1">new_columns</span> <span class="pl-c1">!=</span> <span class="pl-s1">old_columns</span>:
            <span class="pl-k">if</span> <span class="pl-en">len</span>(<span class="pl-s1">new_columns</span>) <span class="pl-c1">==</span> <span class="pl-en">len</span>(<span class="pl-s1">old_columns</span>):
                <span class="pl-k">try</span>:
                    <span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-v">X</span>[<span class="pl-s1">old_columns</span>]
                    <span class="pl-k">print</span> <span class="pl-s">"The order of columns has changed. Fixed."</span>
                <span class="pl-k">except</span>:
                    <span class="pl-k">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'The columns has changed. Please check.'</span>)
            <span class="pl-k">else</span>:
                <span class="pl-k">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'The number of columns has changed.'</span>)
        <span class="pl-k">return</span> <span class="pl-v">LogisticRegression</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>)</pre></div>
<p dir="auto">Then apply this new class:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="slg = SafeLogisticRegression(n_jobs=4, random_state=123, verbose=0, penalty='l1', C=1.0)
slg.fit(train[feature_columns_train], train['target']) "><pre class="notranslate"><span class="pl-s1">slg</span> <span class="pl-c1">=</span> <span class="pl-v">SafeLogisticRegression</span>(<span class="pl-s1">n_jobs</span><span class="pl-c1">=</span><span class="pl-c1">4</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">123</span>, <span class="pl-s1">verbose</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">penalty</span><span class="pl-c1">=</span><span class="pl-s">'l1'</span>, <span class="pl-v">C</span><span class="pl-c1">=</span><span class="pl-c1">1.0</span>)
<span class="pl-s1">slg</span>.<span class="pl-en">fit</span>(<span class="pl-s1">train</span>[<span class="pl-s1">feature_columns_train</span>], <span class="pl-s1">train</span>[<span class="pl-s">'target'</span>]) </pre></div>
<h4 dir="auto">Test one: if the column order is changed</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="prob1 = slg.predict_proba(test[feature_columns_train])
prob2 = slg.predict_proba(test[feature_columns_test])

#The order of columns has changed. Fixed."><pre class="notranslate"><span class="pl-s1">prob1</span> <span class="pl-c1">=</span> <span class="pl-s1">slg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_train</span>])
<span class="pl-s1">prob2</span> <span class="pl-c1">=</span> <span class="pl-s1">slg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_test</span>])

<span class="pl-c">#The order of columns has changed. Fixed.</span></pre></div>
<p dir="auto">Result for test one:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="prob1[:5] =
array([[  3.89507414e-04,   3.20099743e-01,   6.79510750e-01],
       [  4.63256526e-04,   4.65385156e-01,   5.34151587e-01],
       [  8.79704318e-01,   1.20295572e-01,   1.10268420e-07],
       [  7.80611983e-01,   2.19385827e-01,   2.19046022e-06],
       [  2.78898454e-02,   7.77243988e-01,   1.94866167e-01]])

prob2[:5] =
array([[  3.89507414e-04,   3.20099743e-01,   6.79510750e-01],
       [  4.63256526e-04,   4.65385156e-01,   5.34151587e-01],
       [  8.79704318e-01,   1.20295572e-01,   1.10268420e-07],
       [  7.80611983e-01,   2.19385827e-01,   2.19046022e-06],
       [  2.78898454e-02,   7.77243988e-01,   1.94866167e-01]])"><pre class="notranslate"><span class="pl-s1">prob1</span>[:<span class="pl-c1">5</span>] <span class="pl-c1">=</span>
<span class="pl-en">array</span>([[  <span class="pl-c1">3.89507414e-04</span>,   <span class="pl-c1">3.20099743e-01</span>,   <span class="pl-c1">6.79510750e-01</span>],
       [  <span class="pl-c1">4.63256526e-04</span>,   <span class="pl-c1">4.65385156e-01</span>,   <span class="pl-c1">5.34151587e-01</span>],
       [  <span class="pl-c1">8.79704318e-01</span>,   <span class="pl-c1">1.20295572e-01</span>,   <span class="pl-c1">1.10268420e-07</span>],
       [  <span class="pl-c1">7.80611983e-01</span>,   <span class="pl-c1">2.19385827e-01</span>,   <span class="pl-c1">2.19046022e-06</span>],
       [  <span class="pl-c1">2.78898454e-02</span>,   <span class="pl-c1">7.77243988e-01</span>,   <span class="pl-c1">1.94866167e-01</span>]])

<span class="pl-s1">prob2</span>[:<span class="pl-c1">5</span>] <span class="pl-c1">=</span>
<span class="pl-en">array</span>([[  <span class="pl-c1">3.89507414e-04</span>,   <span class="pl-c1">3.20099743e-01</span>,   <span class="pl-c1">6.79510750e-01</span>],
       [  <span class="pl-c1">4.63256526e-04</span>,   <span class="pl-c1">4.65385156e-01</span>,   <span class="pl-c1">5.34151587e-01</span>],
       [  <span class="pl-c1">8.79704318e-01</span>,   <span class="pl-c1">1.20295572e-01</span>,   <span class="pl-c1">1.10268420e-07</span>],
       [  <span class="pl-c1">7.80611983e-01</span>,   <span class="pl-c1">2.19385827e-01</span>,   <span class="pl-c1">2.19046022e-06</span>],
       [  <span class="pl-c1">2.78898454e-02</span>,   <span class="pl-c1">7.77243988e-01</span>,   <span class="pl-c1">1.94866167e-01</span>]])</pre></div>
<h4 dir="auto">Test two: if the columns are different (different columns)</h4>
<p dir="auto">Simulate by changing one of the column names</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="prob3 = slg.predict_proba(test[feature_columns_train].rename(columns={'sepal width (cm)': 'sepal wid (cm)'}))"><pre class="notranslate"><span class="pl-s1">prob3</span> <span class="pl-c1">=</span> <span class="pl-s1">slg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_train</span>].<span class="pl-en">rename</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>{<span class="pl-s">'sepal width (cm)'</span>: <span class="pl-s">'sepal wid (cm)'</span>}))</pre></div>
<p dir="auto">error message:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-47-84cea68536fe&gt; in &lt;module&gt;()
----&gt; 1 prob3 = slg.predict_proba(test[feature_columns_train].rename(columns={'sepal width (cm)': 'sepal wid (cm)'}))

&lt;ipython-input-21-c3000b030a21&gt; in predict_proba(self, X)
     12                     print &quot;The order of columns has changed. Fixed.&quot;
     13                 except:
---&gt; 14                     raise ValueError('The columns has changed. Please check.')
     15             else:
     16                 raise ValueError('The number of columns has changed.')

ValueError: The columns has changed. Please check."><pre class="notranslate"><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span>
<span class="pl-v">ValueError</span>                                <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">47</span><span class="pl-c1">-</span><span class="pl-c1">84</span><span class="pl-s1">cea68536fe</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-s1">prob3</span> <span class="pl-c1">=</span> <span class="pl-s1">slg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_train</span>].<span class="pl-en">rename</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>{<span class="pl-s">'sepal width (cm)'</span>: <span class="pl-s">'sepal wid (cm)'</span>}))

<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">21</span><span class="pl-c1">-</span><span class="pl-s1">c3000b030a21</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-en">predict_proba</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>)
     <span class="pl-c1">12</span>                     <span class="pl-k">print</span> <span class="pl-s">"The order of columns has changed. Fixed."</span>
     <span class="pl-c1">13</span>                 <span class="pl-s1">except</span>:
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">14</span>                     <span class="pl-s1">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'The columns has changed. Please check.'</span>)
     <span class="pl-c1">15</span>             <span class="pl-s1">else</span>:
     <span class="pl-c1">16</span>                 <span class="pl-k">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'The number of columns has changed.'</span>)

<span class="pl-v">ValueError</span>: <span class="pl-v">The</span> <span class="pl-s1">columns</span> <span class="pl-s1">has</span> <span class="pl-s1">changed</span>. <span class="pl-v">Please</span> <span class="pl-s1">check</span>.</pre></div>
<h4 dir="auto">Test three: if the number of columns changes</h4>
<p dir="auto">Simulate by dropping one column</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="prob4 = slg.predict_proba(test[feature_columns_train].drop(['sepal width (cm)'], axis=1))"><pre class="notranslate"><span class="pl-s1">prob4</span> <span class="pl-c1">=</span> <span class="pl-s1">slg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_train</span>].<span class="pl-en">drop</span>([<span class="pl-s">'sepal width (cm)'</span>], <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>))</pre></div>
<p dir="auto">error message:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-48-47c63ae1ac22&gt; in &lt;module&gt;()
----&gt; 1 prob4 = slg.predict_proba(test[feature_columns_train].drop(['sepal width (cm)'], axis=1))

&lt;ipython-input-21-c3000b030a21&gt; in predict_proba(self, X)
     14                     raise ValueError('The columns has changed. Please check.')
     15             else:
---&gt; 16                 raise ValueError('The number of columns has changed.')
     17         return LogisticRegression.predict_proba(self, X)

ValueError: The number of columns has changed."><pre class="notranslate"><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span>
<span class="pl-v">ValueError</span>                                <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">48</span><span class="pl-c1">-</span><span class="pl-c1">47</span><span class="pl-s1">c63ae1ac22</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-s1">prob4</span> <span class="pl-c1">=</span> <span class="pl-s1">slg</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">test</span>[<span class="pl-s1">feature_columns_train</span>].<span class="pl-en">drop</span>([<span class="pl-s">'sepal width (cm)'</span>], <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>))

<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">21</span><span class="pl-c1">-</span><span class="pl-s1">c3000b030a21</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-en">predict_proba</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>)
     <span class="pl-c1">14</span>                     <span class="pl-k">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'The columns has changed. Please check.'</span>)
     <span class="pl-c1">15</span>             <span class="pl-s1">else</span>:
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">16</span>                 <span class="pl-s1">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'The number of columns has changed.'</span>)
     <span class="pl-c1">17</span>         <span class="pl-k">return</span> <span class="pl-v">LogisticRegression</span>.<span class="pl-en">predict_proba</span>(<span class="pl-s1">self</span>, <span class="pl-v">X</span>)

<span class="pl-v">ValueError</span>: <span class="pl-v">The</span> <span class="pl-s1">number</span> <span class="pl-s1">of</span> <span class="pl-s1">columns</span> <span class="pl-s1">has</span> <span class="pl-s1">changed</span>.</pre></div>