Framework for commonly used functions

1, confusion matrix

import itertools
def plot_condusion_matrix(cm,classes,
                          title = 'Confusion matrix',
                          cmap = plt.cm.Blues):
    plt.imshow(cm,interpolation='nearest',cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks,classes,rotation=0)
    plt.yticks(tick_marks,classes)

    thresh = cm.max()/2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

 

2, the visual tree

def dec_tree(model,feature_names,tagret_names)
    import os
    os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
    dot_data = \
        tree.export_graphviz(
            model, # 模型
            out_file=None,
class_names=target_names, feature_names
=feature_names, #特征名字 filled=True, impurity=False, rounded=True ) import pydotplus graph = pydotplus.graph_from_dot_data(dot_data) #graph.get_nodes()[7].set_fillcolor('#FFF2DD') from IPython.display import Image Image(graph.create_png()) graph.write_jpg('graph_jpg') #写入ipg文件

 3, the model training, and continued need to read through the data set small quantities of data samples, where the definition of a function that returns each random samples batch_size features and labels

DEF data_iter (batch_size, the Feature, Labels): 
    num_example = len (the Feature) 
    indices = List (the Range (num_example)) 
    random.shuffle (indices) # make reading sample is random 
    for i in the Range (0, num_example, batch_size ): 
        J = nd.array (indices [I: min (I + batch_size, num_example)]) # effect of increasing a min function is: when the last piece of data is smaller than the length batch_size time, the entire data may be returned directly, to prevent the error 
        the yield feature.take (J), labels.take (J)   # Take function returns the element corresponding to the index

 

Guess you like

Origin www.cnblogs.com/lmcltj/p/10970711.html