如何繪製嶺回歸影像的tikz影像?

如何繪製嶺回歸影像的tikz影像?

我正在嘗試重新建立以下圖像:在此輸入影像描述

不幸的是,這是用Python繪製的,所以我不能真正使用它。所以我想我應該用 Tikz 重新創建它。

我嘗試創建左圖:

\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}
\begin{tikzpicture}
\coordinate (Origin) at (0,0);
\coordinate (OLSEstimates) at (2,8);
\coordinate (Intersect) at (2.55,4.3);
%0.5 scaled
\draw[rotate=45] (OLSEstimates) ellipse (1.275 and 3);
%0.75 scaled
\draw[rotate=45] (OLSEstimates) ellipse (1.9125 and 4.5);
%original boundary
\draw[rotate=45] (OLSEstimates) ellipse (2.56 and 6);
\draw [<->,thick] (0,10) node (yaxis) [above] {$y$}
        |- (10,0) node (xaxis) [right] {$x$};
\draw (0,0) circle (5);
%draw line to intersection
\draw[draw=black,-triangle 90] (Origin) -- (Intersect);
\end{tikzpicture}

現在的問題是,儘管看起來我的圓正在撞擊橢圓,但它是透過手動調整座標來完成的(所以它可能是錯誤的)。其次,橢圓和圓的交點也是用同樣的方法完成的,所以可能會有點偏差。

現在我看到了一些瘋狂的東西,Tikz 可以計算兩條線的交點座標,所以我想知道是否可以在這裡應用某種技術。

一個小旁注:如果它甚至可以自動將橢圓縮放到正確的“大小”以使其恰好在一點相交,那就太完美了,但我想這是不可能的(我從未見過有人這樣做)。

謝謝!

答案1

修改程式碼以放置同心橢圓和點。將橢圓錨定到方形節點的相同想法也是可能的。

橢圓的方向可以透過將它們相對於原點節點(但我不明白那部分)錨定來改變,我認為嶺方程式應該指向原點節點。

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric,calc}
\begin{document}
\begin{tikzpicture}[>=latex]
\clip (-1,-1) rectangle (6,6);
\draw[->,thick] (-1,0) --++(6,0);
\draw[->,thick] (0,-1) --++(0,6);
% Given 
\def\myradius{2.5cm} % CHANGE THESE
\def\mypoint{(3,2)}
% computed 
\node[circle,draw,minimum height=2*\myradius] (o) at (0,0) {};
% here we take the point and compute the distance to the circle node 
% and also the angle of the point wrt to origin. Then we rotate ellipses and adjust the size
\path let \p1=\mypoint,\n1 = {veclen(\x1,\y1)-\myradius},\n2={atan2(\y1,\x1)} in 
\foreach \x in {1,0.75,0.5}{
node[ellipse,draw,
     minimum height=2*\n1*\x,
     minimum width=3.5*\n1*\x,
     rotate=\n2-90] (a) at \mypoint {}
};
\draw[->] (o.center) -- (a.center) 
node[above,inner sep=1pt,rounded corners,fill=white,draw] {$\theta_{\text{Normal Equation}}$};
\draw[->] (o.center) -- (o.80) 
node[above,inner sep=1pt,rounded corners,fill=white,draw] {$\theta_{\text{RidgeEquation}}$};
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

您可以嘗試這樣的方法,但應該有更有效的方法:

\usetikzlibrary{intersections,calc}
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}
\begin{tikzpicture}
\draw [<->,thick] (0,10) node (yaxis) [above] {$y$}
    |- (10,0) node (xaxis) [right] {$x$};

\coordinate (Origin) at (0,0);
\coordinate (OLSEstimates) at (2,8);
\coordinate (Intersect) at (0,5.1); % Initial guess, must be inside the ellipse
%original boundary
\draw[rotate=45, name path=ellipse] (OLSEstimates) ellipse (2.56 and 6);
%scaled boundaries
\foreach \scale in {0.75,0.5} {
    \draw[rotate=45] (OLSEstimates) ellipse (\scale*2.56 and \scale*6);
}
%Compute intersection point iteratively
\foreach \j in {1,...,3} {
    \path[name path=circle-\j, overlay] let \p1 = ($(Origin)-(Intersect)$) in (Origin) circle ({veclen(\x1,\y1)});
    \draw[name intersections={of=ellipse and circle-\j,by={a,b}, total=\t}] let \p1 = ($0.5*(a)+0.5*(b)$) in (\x1,\y1) coordinate (Intersect);
}
\draw let \p1 = ($(Origin)-(Intersect)$) in (Origin) circle ({veclen(\x1,\y1)});
%draw line to intersection
\draw[draw=black,-triangle 90] (Origin) -- (Intersect);
\end{tikzpicture}

我們想要找出一個與橢圓只有一個交點的圓。在此程式碼中,透過取圓和橢圓之間的兩個交點的中心並使下一個圓穿過該點來迭代計算。

程式碼可以工作,但是速度非常慢!

至於繪製省略號,我將\draw命令放在循環中foreach,如您所見,非常簡單。

相關內容