类似于数据Matplotlib的显示的回归

时间:2019-02-22 10:09:45

标签: python python-3.x matplotlib

这是我的数据集:t.csv
我正在寻找这样的显示器:
enter image description here 红点表示负值,灰色表示0,蓝色表示正值。
我试图从以下示例中引用示例:Logistic Regression

def plot_decision_boundary(pred_func):
    # Set min and max values and give it some padding
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # Predict the function value for the whole gid
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)

获得了上述用于绘图的功能,但不了解。
我想要上面的图片。我该怎么办?请让我知道。

1 个答案:

答案 0 :(得分:1)

好的,让我们开始加载数据

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('/home/b2003864/t.csv', header=0)

接下来,使用ax.scatter

即可轻松获得所需的散点
fig, ax = plt.subplots(1, 1)

# Plot positive values in red (C3)
ax.scatter(np.argwhere(df['real'] > 0), df.loc[df['real'] > 0, 'real'], color='C3', edgecolors='k')
# Plot negative values in blue (C0)
ax.scatter(np.argwhere(df['real'] < 0), df.loc[df['real'] < 0, 'real'], color='C0', edgecolors='k')
# Plot neutral value in grey (C7)
ax.scatter(np.argwhere(df['real'] == 0), df.loc[df['real'] == 0, 'real'], color='C7', edgecolors='k')

最后,我们可以根据需要使用ax.fill_between绘制阴影区域:

ax.fill_between([-5, 35], -0.002, 0, color='C0', zorder=-1, alpha=0.7)
ax.fill_between([-5, 35], 0, 0.002, color='C3', zorder=-1, alpha=0.7)

ax.set_ylim([-0.00015, 0.00015])
ax.set_xlim([-5, 35])

所有这些共同给了我

enter image description here