Grafik für überwachtes und unüberwachtes Lernen in tikzpicture

Grafik für überwachtes und unüberwachtes Lernen in tikzpicture

Ich möchte diese beiden Diagramme mit darstellen tikzpicture. Ich muss sie nicht wie unten dargestellt miteinander verbinden, aber ich hätte gerne etwas Ähnliches.

Bildbeschreibung hier eingeben

Ich kann ein Streudiagramm wie folgt generieren:

Bildbeschreibung hier eingeben

Wo ich zwei eindeutige/unterschiedliche Cluster habe, möchte ich die linear trennbare Linie wie im Beispiel des überwachten Lernens zeichnen und gleichzeitig die Cluster im Beispiel des unüberwachten Lernens zeichnen.

Darüber hinaus wären die Farben/größeren Punkte toll.

Code:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines = left,
xlabel = x,
ylabel = y,
]
\addplot[only marks] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    %cluster 1
    2, 3
    3, 5
    4, 5
    3, 8
    5, 9
    3, 2
    5, 6
    6, 6
    7, 9
    10, 4
    11, 5
    9, 4
    %cluser 2
    
    20, 10
    21, 12
    24, 12
    25, 13
    27, 14
    22, 13
    23, 15
    25, 10
    15, 14
 };
\end{axis}
\end{tikzpicture}
\end{document}

Antwort1

Sie könnten verwenden, scatter/classesum die Punkte der beiden Cluster unterschiedlich zu formatieren, aber es ist einfacher, sie einfach in zwei \addplots aufzuteilen. Verwenden Sie zum Festlegen der Formatierung beispielsweise \addplot [blue, only marks, mark=*, mark size=5] ..., die größtenteils selbsterklärend sind, denke ich.

Für die Linien und Kreise müssen Sie fast nur normale TikZ-Befehle verwenden. Beachten Sie jedoch, dass Sie standardmäßig \draw (x,y) ..innerhalb einer axisUmgebung arbeiten xund ysich nicht im Koordinatensystem von befinden axis.Es sei dennSie müssen entweder a) verwenden (axis cs:x,y)oder b) hinzufügen \pgfplotsset{compat=1.11}(oder eine höhere Versionsnummer), wobei dies axis cszur Standardeinstellung wird.

Beachten Sie außerdem, dass ein circle[radius=2]innerhalb eines axiserfordert compat=1.11(glaube ich), aber der Radius ist wieder in Achsenkoordinaten, sodass es in diesem Fall eine Ellipse werden würde. Was Sie stattdessen tun könnten, ist, eine Koordinate innerhalb des zu definieren axisund den Kreis außerhalb davon zu zeichnen. Beides wird im folgenden Code demonstriert.

Bildbeschreibung hier eingeben

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11} % <-- added
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines = left,
xlabel = $x$,
ylabel = $y$,
clip mode=individual % so things drawn by \draw and similar are not cut off
]
\addplot [blue, only marks, mark=*, mark size=5] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    %cluster 1
    2, 3
    3, 5
    4, 5
    3, 8
    5, 9
    3, 2
    5, 6
    6, 6
    7, 9
    10, 4
    11, 5
    9, 4
    };
    
\addplot+[red, only marks, mark=*, mark size=5] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    20, 10
    21, 12
    24, 12
    25, 13
    27, 14
    22, 13
    23, 15
    25, 10
    15, 14
 };
 
 % to be able to use axis coordinates with \draw directly you need
 % \pgfplotsset{compat=1.11} or a higher version
 % if that is not present, use (axis cs:4,14) instead of (4,14),
 % to specify that the values should be interpreted as axis coordinates
 \draw [dashed] (4,14) -- (25,2);
 
 % save a coordinate for use later
 \coordinate (c2) at (23,12);
 
 % the blue circle is drawn inside the axis environment, and in axis coordinates
 % hence it becomes an ellipse
 \draw [blue, dashed] (6,6) circle[radius=5]; 

\end{axis}

% the red circle is drawn outside the axis, so actually looks like a circle,
% but the radius has no relation to the axis coordinates
\draw [red, dashed] (c2) circle[radius=2cm];
\end{tikzpicture}
\end{document}

Zwei Achsen

Es gibt mehrere Methoden, um zwei Diagramme nebeneinander zu platzieren. Sie können zwei tikzpicturedirekt hintereinander hinzufügen oder zwei axisUmgebungen in derselben haben tikzpictureund die zweite mit positionieren \begin{axis}[at={(x,y)},.... Mir persönlich gefällt die groupplotUmgebung aus der groupplotsBibliothek am besten, die zum Erstellen von Achsenrastern dient.

Bildbeschreibung hier eingeben

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
  group size=2 by 1,
  horizontal sep=1.5cm
  },
axis lines = left,
xlabel = $x$,
ylabel = $y$,
width=5cm, % <-- set size of axes
clip mode=individual, % to avoid \draws being cut off
title style={yshift=1mm, font=\bfseries\sffamily}
]

\nextgroupplot[title=Supervised learning]
\addplot [blue, only marks, mark=*, mark size=3] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    %cluster 1
    2, 3
    3, 5
    4, 5
    3, 8
    5, 9
    3, 2
    5, 6
    6, 6
    7, 9
    10, 4
    11, 5
    9, 4
    };
    
\addplot+[red, only marks, mark=*, mark size=3] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    20, 10
    21, 12
    24, 12
    25, 13
    27, 14
    22, 13
    23, 15
    25, 10
    15, 14
 };
 
 % to be able to use axis coordinates with \draw directly you need
 % \pgfplotsset{compat=1.11} or a higher version
 % if that is not present, use (axis cs:4,14) instead of (4,14),
 % to specify that the values should be interpreted as axis coordinates
 \draw [dashed] (4,14) -- (25,2);
 

 
\nextgroupplot[title=Unsupervised learning]
\addplot [blue, only marks, mark=*, mark size=3] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    %cluster 1
    2, 3
    3, 5
    4, 5
    3, 8
    5, 9
    3, 2
    5, 6
    6, 6
    7, 9
    10, 4
    11, 5
    9, 4
    };
    
\addplot+[red, only marks, mark=*, mark size=3] table [%
x = x, 
y = y, 
col sep = comma]{
    x, y
    20, 10
    21, 12
    24, 12
    25, 13
    27, 14
    22, 13
    23, 15
    25, 10
    15, 14
 };
 

 % save a coordinate for use later
 \coordinate (c2) at (23,12);
 
 % the blue circle is drawn inside the axis environment, and in axis coordinates
 % hence it becomes an ellipse
 \draw [blue, dashed] (6,6) circle[radius=5]; 

\end{groupplot}

% the red circle is drawn outside the axis, so actually looks like a circle,
% but the radius has no relation to the axis coordinates
\draw [red, dashed] (c2) circle[radius=1cm];


\end{tikzpicture}
\end{document}

verwandte Informationen