pythonipython:机器学习片段(代码片段)

author author     2022-12-28     271

关键词:

# source: http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
import itertools

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          decimals=2,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

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

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    
    
# EXAMPLE: Plot normalized confusion matrix
cnf_matrix = confusion_matrix(test_labels_1d, pred)
class_names = [str(i) for i in range(0, 10)]

plt.figure(figsize=(6,6))
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')

plt.show()
import pandas as pd
# simple crosstab with totals
pd.crosstab(y, yHat, rownames=["actual"], colnames=["predicted"], margins=True)
# normalized cross tab, rounded to two decimals
(pd.crosstab(test_labels_1d, pred, rownames=["actual"], colnames=["predicted"], margins=False, normalize="index")*100).round(2)
from IPython.display import SVG
from keras.utils.visualize_util import model_to_dot

SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))

# and to save it directly to a file
from keras.utils.visualize_util import plot
plot(model, show_shapes=True, to_file='/tmp/model.png')

pythonipython_matplotlib_img.py(代码片段)

查看详情

《机器学习实战》-机器学习基础(代码片段)

目录机器学习基础什么是机器学习机器学习应用场景海量数据机器学习的重要性机器学习的基本术语监督学习和非监督学习监督学习:supervisedlearning非监督学习:unsupervisedlearning机器学习工具介绍Python非PythonNumPy函数库基础测试Nu... 查看详情

python机器学习有用的代码片段(代码片段)

查看详情

markdown机器学习(代码片段)

查看详情

text机器学习(代码片段)

查看详情

markdown机器学习入门(代码片段)

查看详情

pythonudacity:交易机器学习(代码片段)

查看详情

markdown机器学习笔记(代码片段)

查看详情

text机器学习的资源(代码片段)

查看详情

markdown机器学习人类:笔记(代码片段)

查看详情

机器学习基础教程笔记---机器学习概述(代码片段)

目录机器学习概述1.1人工智能概述1.1.1机器学习与人工智能、深度学习1.1.2机器学习、深度学习能做些什么1.1.3人工智能阶段课程安排1.2什么是机器学习1.2.1定义1.2.2解释1.2.3数据集构成1.3机器学习算法分类学习目标分析1.2中的例子... 查看详情

机器学习_决策树(代码片段)

       查看详情

python机器学习笔记(代码片段)

Python机器学习笔记一机器学习概述1.1人工智能概述1.1.1机器学习与人工智能、深度学习关系机器学习和人工智能、深度学习的关系机器学习是人工智能的一个实现途径深度学习是机器学习的一个方法发展而来达特茅斯会议(Da... 查看详情

python机器学习笔记(代码片段)

Python机器学习笔记一机器学习概述1.1人工智能概述1.1.1机器学习与人工智能、深度学习关系机器学习和人工智能、深度学习的关系机器学习是人工智能的一个实现途径深度学习是机器学习的一个方法发展而来达特茅斯会议(Da... 查看详情

机器学习介绍(代码片段)

机器学习Sitara机器学习工具包通过在所有Sitara设备(仅Arm、Arm+专用硬件加速器)上启用机器学习推理,将机器学习推向了最前沿。它是作为TI的处理器SDKLinux的一部分提供的,可以免费下载和使用。今天的Sitara... 查看详情

机器学习介绍(代码片段)

机器学习Sitara机器学习工具包通过在所有Sitara设备(仅Arm、Arm+专用硬件加速器)上启用机器学习推理,将机器学习推向了最前沿。它是作为TI的处理器SDKLinux的一部分提供的,可以免费下载和使用。今天的Sitara... 查看详情

机器学习笔记(代码片段)

大数据机器学习机器学习回归问题和分类问题导入数学函数库importnumpyasnp导入绘图模块importmatplotlib.pyplotasplt生成回归样本数据fromsklearn.datasetsimportmake_regressionX,y=make_regression(n_samples=100,n_features=1,n_informative 查看详情

机器学习_贝叶斯算法(代码片段)

     查看详情