以下是我的数据集中的一个示例
-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列
我需要通过在2D图上为每对特征值(即数据中的每一行)放置一个标记来可视化这一点。在图中,x轴应该是第一个要素的值,y轴应该是第二个要素的值,并且标记应该是,例如,当目标值为+1时为+标记,当目标为−1时为o
发布于 2021-10-05 17:46:34
根据您所说的,我已经为您提供了一些想法来绘制您的数据帧的所需输出。所以我试着用这段代码重现你的问题:
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
)产生的输出为:

发布于 2021-10-05 17:34:13
如果这是第三列中仅有的两个选项,那么您可以很容易地拆分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()https://stackoverflow.com/questions/69452891
复制相似问题