new_contents: import numpy as np
  from sklearn import svm
  from sklearn.metrics import *
  from sklearn import cross_validation
  from sklearn.cross_validation import KFold
  from BalanceData import *
  import matplotlib.pyplot as plt
  
  
  cParam = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
  kFold =10
  
  
  def ShuffleTrainFeatures(feats, labs):
      tempArr =  np.c_[feats.reshape(len(feats),-1), labs.reshape(len(labs), -1)]
      np.random.shuffle(tempArr)
      a2 = tempArr[:,:feats.size//len(feats)].reshape(feats.shape)
      b2 = tempArr[:,feats.size//len(feats):].reshape(labs.shape)
      
      return a2, b2
  
  
  def runkFoldCrossValidation(features, labels, model):
      #scores = {}
      scores =[]
      kf = KFold(len(features), kFold	, shuffle=True)
  
      for k, (train, test) in enumerate(kf):
          model.C = cParam[k]
          model.fit(features[train], labels[train])
          score = model.score(features[test], labels[test])
          print (k, model.C, score)
          scores.append(score)
      
  
  
      plt.figure(figsize=(4, 3))
      plt.semilogx(cParam, scores)
      # plot error lines showing +/- std. errors of the scores
      plt.semilogx(cParam, np.array(scores) + np.array(scores.std()) / np.sqrt(len(features)),
                   'b--')
      plt.semilogx(cParam, np.array(scores) - np.array(scores.std()) / np.sqrt(len(features)),
                   'b--')
      plt.ylabel('CV score')
      plt.xlabel('alpha')
      plt.axhline(np.max(scores), linestyle='--', color='.5')
      index = getMaxIndex(scores)
      return index
          
  
  def getMaxIndex(scores):
      maxVal =0
      maxIndex = 0
      for c in range(len(scores)):
        if maxVal < scores[c]:
            maxVal = scores[c]
            maxIndex = c
      print maxIndex
      return maxIndex
  
  
  
  rootdir = os.getcwd()
  if not os.path.exists('sklearnTry'):
          os.makedirs('sklearnTry')
  newdir = os.path.join(rootdir,'sklearnTry')
  fout = open(os.path.join(newdir,'SVMOut'),'w+')
  
  train_features, train_labels, test_features, test_labels, test_keys = GetData() 
  train_features, train_labels = ShuffleTrainFeatures(train_features, train_labels)
  
  model = svm.SVC(decision_function_shape ='ovr', class_weight='balanced'	)
  c = runkFoldCrossValidation(train_features, train_labels, model)
  #c =0.8
  model.set_params( C = c)
  model.fit(train_features, train_labels)
  pred = model.predict(test_features)
  predictProb = model.decision_function(test_features)
  train_acc = (model.score(train_features, train_labels))
  
  
  
  line = str(train_acc )+"\n"
  print train_acc
  fout.write(line)
  
  
  
  for key in range(len(test_keys)):
      line = test_keys[key]+"\t"
      for f in range(len(predictProb[key])):
        line +="%i:%f\t" % (f+1 , predictProb[key][f])
      line += "\n"
      fout.write(line)
  fout.close()
      
      
  accuracy = accuracy_score(test_labels, pred)
  print confusion_matrix(test_labels, pred)
  
  print accuracy
  
  
subject: Create ScikitSVM.py