Example #1
0
def class_2c_2f(thetas,threshold):
  """
  Computes the classification map for a discrimination problem 
  with 2 classes and 2 features.
  thetas is a dictionary of length = 1 (2 classes) 
  and each independent theta vector is of length = 3 (2 features).
  """
  from LR_functions import g
  pas = .01
  x1 = np.arange(-1,1,pas)
  x2 = np.arange(-1,1,pas)
  x1, x2 = np.meshgrid(x1,x2)

  probas = g(thetas[1][0]+thetas[1][1]*x1+thetas[1][2]*x2)
  classi = probas.copy()
  classi[classi>=threshold[1]] = 1
  classi[classi!=1] = 0

  return x1, x2, probas, classi
Example #2
0
def class_multi_2f(thetas):
  """
  Computes the classification map for a discrimination problem 
  with more than 2 classes and only 2 features.
  thetas is a dictionary of length = 3 (3 classes) 
  and each independent theta vector is of length = 3 (2 features).
  """
  from LR_functions import g
  pas = .01
  x1 = np.arange(-1,1,pas)
  x2 = np.arange(-1,1,pas)
  x1, x2 = np.meshgrid(x1,x2)

  probas = []
  for i in sorted(thetas):
    probas.append(g(thetas[i][0]+thetas[i][1]*x1+thetas[i][2]*x2))
  probas = np.array(probas)

  return x1, x2, np.max(probas,axis=0), np.argmax(probas,axis=0)
Example #3
0
def class_multi_2f(thetas):
    """
  Computes the classification map for a discrimination problem 
  with more than 2 classes and only 2 features.
  thetas is a dictionary of length = 3 (3 classes) 
  and each independent theta vector is of length = 3 (2 features).
  """
    from LR_functions import g
    pas = .01
    x1 = np.arange(-1, 1, pas)
    x2 = np.arange(-1, 1, pas)
    x1, x2 = np.meshgrid(x1, x2)

    probas = []
    for i in sorted(thetas):
        probas.append(g(thetas[i][0] + thetas[i][1] * x1 + thetas[i][2] * x2))
    probas = np.array(probas)

    return x1, x2, np.max(probas, axis=0), np.argmax(probas, axis=0)
Example #4
0
def class_2c_2f(thetas, threshold):
    """
  Computes the classification map for a discrimination problem 
  with 2 classes and 2 features.
  thetas is a dictionary of length = 1 (2 classes) 
  and each independent theta vector is of length = 3 (2 features).
  """
    from LR_functions import g
    pas = .01
    x1 = np.arange(-1, 1, pas)
    x2 = np.arange(-1, 1, pas)
    x1, x2 = np.meshgrid(x1, x2)

    probas = g(thetas[1][0] + thetas[1][1] * x1 + thetas[1][2] * x2)
    classi = probas.copy()
    classi[classi >= threshold[1]] = 1
    classi[classi != 1] = 0

    return x1, x2, probas, classi
Example #5
0
def plot_hyp_func_1f(x,y,theta,method,threshold=None,x_ok=None,x_bad=None,th_comp=None,cmat_test=[],cmat_svm=[],cmat_train=[]):

  num_t = np.unique(y.NumType.values.ravel())
  num_t = map(int,list(num_t))
  str_t = np.unique(y.Type.values.ravel())
  str_t = map(str,list(str_t))

  fig = plt.figure(figsize=(12,7))
  fig.set_facecolor('white')

  left, bottom = .1, .1
  width, height = .5, .8
  width_h, height_h = .25, .35
  left_h, bottom_h = left+width+.05, bottom+height_h+.1
  rect_1 = [left, bottom, width, height]
  rect_2 = [left_h, bottom_h, width_h, height_h]
  rect_3 = [left_h, bottom, width_h, height_h]

  ax1 = plt.axes(rect_1)
  ax2 = plt.axes(rect_2)
  ax3 = plt.axes(rect_3)

  x1 = x[y.NumType.values.ravel()==num_t[0]].values[:,0]
  x2 = x[y.NumType.values.ravel()==num_t[1]].values[:,0]

  nn,b,p = ax1.hist([x1,x2],25,normed=True,histtype='stepfilled',alpha=.2,color=('b','g'),label=str_t)

  from LR_functions import g
  # Plot the hypothesis function
  syn = np.arange(-1,1,0.01)
  hyp = g(theta[1][0]+theta[1][1]*syn)
  norm = np.mean([np.max(nn[0]),np.max(nn[1])])
  ax1.plot(syn,norm*hyp,'y-',lw=2,label='hypothesis')

  if threshold:
    thres = np.ones(len(hyp))*threshold[1]
    imin = np.argmin(np.abs(thres-hyp))
    t = syn[imin]
    ax1.plot([t,t],[0,np.max(nn)],'orange',lw=3.,label='decision')

  if th_comp:
    hyp_svm = g(th_comp[1][0]+th_comp[1][1]*syn)
    ax1.plot(syn,norm*hyp_svm,'magenta',lw=2.)

    thres = np.ones(len(hyp_svm))*.5
    imin = np.argmin(np.abs(thres-hyp_svm))
    t = syn[imin]
    ax1.plot([t,t],[0,np.max(nn)],'purple',lw=3.)

  if x_ok and x_bad:
    # plot well-classified events of the test set
    #nn, b, p = ax1.hist([x_ok],25,normed=True,color=('k'),histtype='step',fill=False,ls='dashed',lw=2,label=['Test Set'])
    # plot both well and badly-classified events of the test set
    nn, b, p = ax1.hist([x_ok,x_bad],25,normed=True,color=('k','r'),histtype='step',fill=False,ls='dashed',lw=2,label=['Test Set'])

  ax1.set_xlim([-1,1])
  ax1.legend(prop={'size':12})
  ax1.set_xlabel(x.columns[0])
  ax1.set_title(x.columns[0])

  from do_classification import plot_confusion_mat, dic_percent
  s = 12
  x_pos = .05
  y_pos = .95
  pas = .04
  if list(cmat_test):
    plot_confusion_mat(cmat_test,str_t,'Test',method,ax=ax3)
    p_test = dic_percent(cmat_test,str_t)
    ax1.text(x_pos,y_pos,method.upper(),size=s,transform=ax1.transAxes,color='orange')
    ax1.text(x_pos,y_pos-pas,"Test : %.2f%%"%p_test['global'],size=s,transform=ax1.transAxes)
    ax1.text(x_pos+.2,y_pos-pas,"(%.2f%%)"%(100-p_test['global']),size=s,color='red',transform=ax1.transAxes)
    
  if list(cmat_train) and not list(cmat_svm):
    plot_confusion_mat(cmat_train,str_t,'Training',method,ax=ax2)
    p_train = dic_percent(cmat_train,str_t)
    ax1.text(x_pos,y_pos-2*pas,"Training : %.2f%%"%p_train['global'],size=s,transform=ax1.transAxes)

  elif list(cmat_train) and list(cmat_svm):
    p_train = dic_percent(cmat_train,str_t)
    ax1.text(x_pos,y_pos-2*pas,"Training : ",size=s,transform=ax1.transAxes)
    ax1.text(x_pos+.15,y_pos-2*pas,"%s %s%%"%(str_t[0],p_train[(str_t[0],0)]),color='b',size=s,transform=ax1.transAxes)
    ax1.text(x_pos+.15,y_pos-3*pas,"%s %s%%"%(str_t[1],p_train[(str_t[1],1)]),color='g',size=s,transform=ax1.transAxes)

    plot_confusion_mat(cmat_svm,str_t,'Test','SVM',ax=ax2)
    p_svm = dic_percent(cmat_svm,str_t)
    ax1.text(x_pos,y_pos-4*pas,"SVM",size=s,transform=ax1.transAxes,color='purple')
    ax1.text(x_pos,y_pos-5*pas,"Test : %.2f%%"%p_svm['global'],size=s,transform=ax1.transAxes)

  elif not list(cmat_train) and list(cmat_svm):
    plot_confusion_mat(cmat_svm,str_t,'Test','SVM',ax=ax2)
    p_svm = dic_percent(cmat_svm,str_t)
    ax1.text(x_pos,y_pos-3*pas,"SVM",size=s,transform=ax1.transAxes,color='purple')
    ax1.text(x_pos,y_pos-4*pas,"Test : %.2f%%"%p_svm['global'],size=s,transform=ax1.transAxes)

  plt.figtext(.1,.93,'(a)')
  plt.figtext(.63,.93,'(b)')
  plt.figtext(.63,.48,'(c)')