title: How to compute labels from predicted values of regression model?
body: <p>I am working on a Machine Learning Project. I need to create two python scripts: </p>
  
  <p>1) a classifier<br>
  2) Produce a text file of labels using that classifier.  </p>
  
  <p>I am just saving the model in the first script. Then, in the second script, I am applying that model to a different dataset containing text to produce predicted labels (ham, or spam) and saving those predicted labels in a text file. </p>
  
  <p>Basically I have a list of text with labels, ham or spam. </p>
  
  <p>I created a classifier using the Linear Regression Model. I had two different files of training data (<code>texts_training</code>, and <code>labels_training</code>), so I loaded my training data into variables called texts and labels. And then, I worked on the classifier. This is what I have for the classifier:</p>
  
  <pre><code>#classifier.py 
  def features (words):
       fe = np.ndarrary ((len(tweets), 56) 
            for t, text in enumerate (words):
                 if "money" in text:
                     money = 1
                 else:
                     money = 0
  
                 ...(55 more features)
  
                 fe = [i:] = [money, ...]
           return fe
  fe = features (words)
  
  feat.shape
  &gt;&gt;&gt;(1000, 56)
  
  import sklearn
  X = fe
  label = preprocessing.LabelEncoder()
  label.fit(labels)
  label = lab.transform(labels)
  y.shape
  &gt;&gt;&gt;(1000,)
  
  
  from sklearn.cross_validation import train_test_split
  X_train, X_test, y_train, y_test = train_test_split (X,y, random_state = 4)
  
  from sklearn.preprocessing import StandardScaler
  scaler = preprocessing.StandardScaler().fit(X_train)
  
  
  #Model
  from sklearn.linear_model import LinearRegression
  clf = LinearRegression()
  clf = lreg.fit(X, y) 
  
  import pickle
  f = open ("clf.pkl", "w")
  pickle.dump ((clf, f)
  f.close ()
  </code></pre>
  
  <p>Now, I was loading this into a different script but both scripts are saved in the same folder. This script basically has to use that classifier to save the labels produced in a txt. </p>
  
  <pre><code>system.py
  
      def features (words):
       fe = np.ndarrary ((len(tweets), 56) 
            for t, text in enumerate (words):
                 if "money" in text:
                     money = 1
                 else:
                     money = 0
  
                 ...(55 more features)
  
                 feat = [t, :] = [money, ...]
           return fe
  fe = features (words)
  
  X = feat
  from sklearn import preprocessing
  label = preprocessing.LabelEncoder()
  label.fit(labels)
  label = label.transform(labels) 
  y = label
  from sklearn.preprocessing import StandardScaler
  scaler = preprocessing.StandardScaler().fit(X)
  
  
  
  import pickle
  #class_output = pickle.load (open('clf.pkl', 'r'))
  loaded_model = pickle.load (open('clf.pkl', 'r'))
  
  class_output = loaded_model.predict (X)
  
  **print class_output
  &gt;&gt;&gt;array([ 0.06140778,  0.053107  ,  0.14343903, ...,  0.05701325,
      0.18738435, -0.08788421])**
  
  f = open ("labels_produced.txt", "w")
  for output in class_output:
      if output ==0:
          f.write ("ham\n")
      else:
          f.write("spam\n")
  f.close()
  </code></pre>
  
  <p>However, how do I compute spam or ham for the new data set as none of the values in the class_output are equal to 0. My features were set to be either 0 or 1. </p>
  
  <p>I am a beginner learner, I have been struggling with this all day today. I do not understand why I get this error and how to fix it. If someone helps, I would really appreciate it. </p>