机器学习可视化对比

# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
# from sklearn.lda import LDA
# from sklearn.qda import QDA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
h = .02  # step size in the mesh

names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Decision Tree",
         "Random Forest", "AdaBoost", "Naive Bayes", "LDA", "QDA"]
classifiers = [
    KNeighborsClassifier(3),
    SVC(kernel="linear", C=0.025),
    SVC(gamma=2, C=1),
    DecisionTreeClassifier(max_depth=5),
    RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
    AdaBoostClassifier(),
    GaussianNB(),
    LDA(),
    QDA()]

X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
                           random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)

datasets = [make_moons(noise=0.3, random_state=0),
            make_circles(noise=0.2, factor=0.5, random_state=1),
            linearly_separable
            ]

figure = plt.figure(figsize=(27, 9))
i = 1
# iterate over datasets
for ds in datasets:
    # preprocess dataset, split into training and test part
    X, y = ds
    X = StandardScaler().fit_transform(X)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4)

    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    # just plot the dataset first
    cm = plt.cm.RdBu
    cm_bright = ListedColormap(['#FF0000', '#0000FF'])
    ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
    # Plot the training points
    ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
    # and testing points
    ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xticks(())
    ax.set_yticks(())
    i += 1

    # iterate over classifiers
    for name, clf in zip(names, classifiers):
        ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
        clf.fit(X_train, y_train)
        score = clf.score(X_test, y_test)

        # Plot the decision boundary. For that, we will assign a color to each
        # point in the mesh [x_min, m_max]x[y_min, y_max].
        if hasattr(clf, "decision_function"):
            Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
        else:
            Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]

        # Put the result into a color plot
        Z = Z.reshape(xx.shape)
        ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)

        # Plot also the training points
        ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
        # and testing points
        ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
                   alpha=0.6)

        ax.set_xlim(xx.min(), xx.max())
        ax.set_ylim(yy.min(), yy.max())
        ax.set_xticks(())
        ax.set_yticks(())
        ax.set_title(name)
        ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
                size=15, horizontalalignment='right')
        i += 1

figure.subplots_adjust(left=.02, right=.98)
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 对于机器学习的预测结果可,可以使用Python中的Matplotlib和Seaborn等库进行绘图。 下面以一个简单的线性回归模型为例,来展示如何可它的预测结果: ```python import numpy as np import matplotlib.pyplot as plt import seaborn as sns # 生成一些随机数据 X = 2 * np.random.rand(100, 1) y = 4 + 3 * X + np.random.randn(100, 1) # 将数据可 sns.scatterplot(x=X.ravel(), y=y.ravel()) plt.xlabel('X') plt.ylabel('y') plt.show() # 训练线性回归模型 from sklearn.linear_model import LinearRegression lin_reg = LinearRegression() lin_reg.fit(X, y) # 对新数据进行预测 X_new = np.array([[0], [2]]) y_pred = lin_reg.predict(X_new) # 将预测结果可 sns.scatterplot(x=X.ravel(), y=y.ravel()) sns.lineplot(x=X_new.ravel(), y=y_pred.ravel(), color='red') plt.xlabel('X') plt.ylabel('y') plt.show() ``` 上述代码中,首先生成了一些随机数据并将其可。然后用线性回归模型对数据进行训练,并对新数据进行预测。最后将预测结果可,其中蓝色点表示原始数据,红色线表示预测结果。 通过这样的可,我们可以更加直观地了解模型的预测效果,从而更好地进行模型的调整和优。 ### 回答2: Python机器学习中的predict结果可,可以通过使用各种图表和图形库来实现。其中一种常用的库是matplotlib。 首先,我们可以使用matplotlib库中的散点图来可预测结果。我们可以将样本点在二维平面上以不同的颜色和形状绘制出来,然后用预测结果对应的颜色和形状来标记。这样可以直观地展示预测结果与真实结果之间的关系。 另一种常见的可方法是使用matplotlib中的柱状图或折线图来表示预测结果与真实结果之间的差异。可以将预测结果和真实结果按照某种标准进行分组,然后用柱状图或折线图表示每个组的平均预测结果和真实结果。通过对比不同组之间的差异,可以更加直观地了解模型的预测能力。 此外,还可以使用matplotlib中的热力图来展示预测结果的概率分布。热力图可以将不同预测结果的概率用颜色深浅来表示,从而可以观察到不同预测结果的概率分布情况。 除了matplotlib,还有其他一些可库也可以用来展示机器学习的预测结果,例如seaborn和plotly。这些库提供了更多的图表和图形类型,可以根据具体需求选择合适的库进行可。 总结起来,Python机器学习的predict结果可可以通过使用matplotlib等库来实现,可以利用散点图、柱状图、折线图、热力图等图表来展示预测结果与真实结果之间的关系、差异和概率分布。 ### 回答3: 在Python中,我们可以使用不同的库和工具来对机器学习模型的predict结果进行可。下面是几种常见的可方法: 1. Matplotlib库:Matplotlib是Python中最常用的绘图库,可以用于绘制各种类型的图表,包括直方图、散点图、线图等。我们可以将模型的预测结果与真实结果进行对比,使用Matplotlib创建直方图或散点图来显示它们之间的差异。 2. Seaborn库:Seaborn是基于Matplotlib的高级数据可库,它提供了更加美观和专业的统计图表,同时也更加简单易用。我们可以使用Seaborn绘制误差线图、箱线图等,以展示预测结果的分布情况或者模型的性能。 3. Plotly库:Plotly是一种交互式数据可工具,可以创建美观且具有动态性的图表。我们可以使用Plotly来绘制预测结果的3D散点图或表面图,以展示数据的分布和模型的预测情况。 4. TensorFlow的TensorBoard:如果我们使用的是TensorFlow来构建和训练机器学习模型,可以使用TensorBoard来可predict结果。TensorBoard为我们提供了丰富的可工具,包括绘制训练和验证数据的曲线、查看模型的架构和参数分布等。 无论选择哪种方法,可机器学习模型的predict结果可以帮助我们更好地理解模型的性能、数据的分布以及预测的准确性。通过直观的可图表,我们能够更好地分析和解释模型的结果,并提供对改进模型和优算法的有价值的见解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值