code: # -*- coding: utf-8 -*-
  """
  Created on Tue Jul 24 14:38:20 2018
  dimension reduction with VarianceThreshold using sklearn.
  Feature selector that removes all low-variance features.
  @author: lenovo
  """
  from sklearn.feature_selection import VarianceThreshold
  import numpy as np
  #
  np.random.seed(1)
  X = np.random.randn(100, 10)
  X = np.hstack([X, np.zeros([100, 5])])
  #
  
  
  def featureSelection_variance(X, thrd):
      sel = VarianceThreshold(threshold=thrd)
      X_selected = sel.fit_transform(X)
      mask = sel.get_support()
      return X_selected, mask
  
  
  X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]]
  selector = VarianceThreshold()
  selector.fit_transform(X)
  selector.variances_
   import tensorflow as tf
  
  
  def masked_softmax_cross_entropy(preds, labels, mask):
      """Softmax cross-entropy loss with masking."""
      loss = tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=labels)    
      mask = tf.cast(mask, dtype=tf.float32)
      mask /= tf.reduce_mean(mask)
      loss *= mask
      return tf.reduce_mean(loss)
  
  def sigmoid_cross_entropy(preds, labels):
      """Softmax cross-entropy loss with masking."""
      loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=preds, labels=labels) 
      return tf.reduce_mean(loss)
  
  def softmax_cross_entropy(preds, labels):
      """Softmax cross-entropy loss with masking."""
      loss = tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=labels) 
      return tf.reduce_mean(loss)
  
  def masked_accuracy(preds, labels, mask):
      """Accuracy with masking."""
      correct_prediction = tf.equal(tf.argmax(preds, 1), tf.argmax(labels, 1))
      accuracy_all = tf.cast(correct_prediction, tf.float32)
      mask = tf.cast(mask, dtype=tf.float32)
      mask /= tf.reduce_mean(mask)
      accuracy_all *= mask
      return tf.reduce_mean(accuracy_all)
  
  def inductive_multiaccuracy(preds, labels):
      """Accuracy with masking."""
  
      correct_prediction = tf.equal(tf.argmax(preds, 1), tf.argmax(labels, 1))  
      return tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  
  
  def inductive_accuracy(preds, labels):
      """Accuracy with masking."""
  
      predicted = tf.nn.sigmoid(preds)
      correct_pred = tf.equal(tf.round(predicted), labels)
      accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))    
      return accuracy
  
apis: numpy.random.randn numpy.random.seed numpy.zeros sklearn.feature_selection.VarianceThreshold
apis: tensorflow.nn.sigmoid_cross_entropy_with_logits tensorflow.nn.sigmoid tensorflow.nn.softmax_cross_entropy_with_logits tensorflow.round tensorflow.reduce_mean tensorflow.cast tensorflow.argmax