熊猫, 情节, 散点图

Panda, Plot, Scatter

这是我的数据集中的样本

-0.76,-0.66,-1
0.07,0.59,1
0.73,0.6,-1
0.58,-0.46,-1
-0.71,0.9,-1
-0.82,-0.13,-1
0.2,0.43,1
-0.72,-0.93,-1
0.8,-0.79,-1
-0.35,-0.52,-1
0.53,0.72,1
-0.7,0.96,-1

每列 3 列

我需要通过在二维图上为每个标记放置一个标记来将其可视化 一对特征值,即数据中的每一行。在图上 x 轴应该 是第一个特征的值,y 轴是第二个特征的值, 例如,当目标值为 +1 时,标记应为 + 标记,并且 a o 当目标为 −1

如果这些是第三列的唯一两个选项,您可以很容易地拆分 z=1 和 z=-1 的数据集,然后使用两个绘图函数将它们显示在具有不同标记的同一图表上。

df1 = df[ df['z'] == 1 ].copy(deep=true)
df2 = df[ df['z'] == -1].copy(deep=true)
plt.plot(df1['x'],df1['y'], marker='+')
plt.plot(df2['x'],df2['y'], marker='o')
plt.show()

根据你所说的,我已经为你提供了一些想法,可以从你的数据框中绘制出所需的输出。所以我试着用这段代码重现你的问题:

import pandas as pd

# I made this solution using matplotlib to make the scatterplot
from matplotlib import pyplot as plt

data = [
    (-0.76, -0.66, -1),
    ( 0.07,  0.59,  1),
    ( 0.73,  0.60, -1),
    ( 0.58, -0.46, -1),
    (-0.71,  0.90, -1),
    (-0.82, -0.13, -1),
    ( 0.20,  0.43,  1),
    (-0.72, -0.93, -1),
    ( 0.80, -0.79, -1),
    (-0.35, -0.52, -1),
    ( 0.53,  0.72,  1),
    (-0.70,  0.96, -1),
]

df = pd.DataFrame(data, columns = ["X", "Y", "Sign"])

x = df['X']  # Values for x-axis
y = df['Y']  # Values for y-axis
signs = df['Sign']  # Values for changing the marker in the plots

for i in range(len(x)):
    plt.scatter(
        x[i], y[i],     # X, Y coordinates
        s = 100,        # Size of the markers
        linewidth = 3,  # Line width
        marker = "+" if signs[i] > 0 else "_",      # Control wether the marker is a '+' or a '-'
        color = "green" if signs[i] > 0 else "red"  # Control the color based on minus or plus sign
    )

产生的输出是: