cleaning up

This commit is contained in:
Morten Hjorth-Jensen
2022-11-03 08:04:30 +01:00
parent 83c780d856
commit 048d2ddd0d
10 changed files with 533 additions and 966 deletions
+1 -1
View File
@@ -318,7 +318,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 2, 2022</h4>
<h4>Nov 3, 2022</h4>
</center> <!-- date -->
<br>
-10
View File
@@ -356,12 +356,6 @@ display(grades)
X <span style="color: #666666">=</span> grades<span style="color: #666666">.</span>loc[:, grades<span style="color: #666666">.</span>columns <span style="color: #666666">!=</span> <span style="color: #BA2121">&#39;Grade&#39;</span>]<span style="color: #666666">.</span>values
y <span style="color: #666666">=</span> grades<span style="color: #666666">.</span>loc[:, grades<span style="color: #666666">.</span>columns <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;Grade&#39;</span>]<span style="color: #666666">.</span>values
<span style="color: #008000">print</span>(X)
<span style="color: #408080; font-style: italic"># Create the encoder.</span>
encoder <span style="color: #666666">=</span> OneHotEncoder(handle_unknown<span style="color: #666666">=</span><span style="color: #BA2121">&quot;ignore&quot;</span>)
<span style="color: #408080; font-style: italic"># Assume for simplicity all features are categorical.</span>
encoder<span style="color: #666666">.</span>fit(X)
<span style="color: #408080; font-style: italic"># Apply the encoder.</span>
X <span style="color: #666666">=</span> encoder<span style="color: #666666">.</span>transform(X)
<span style="color: #408080; font-style: italic"># Then do a Classification tree</span>
tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=2</span>)
tree_clf<span style="color: #666666">.</span>fit(X, y)
@@ -375,10 +369,6 @@ export_graphviz(
)
cmd <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png&#39;</span>
os<span style="color: #666666">.</span>system(cmd)
<span style="color: #408080; font-style: italic">#data_pandas = pd.DataFrame(data,index=[&#39;Frodo&#39;,&#39;Bilbo&#39;,&#39;Aragorn&#39;,&#39;Sam&#39;])</span>
<span style="color: #408080; font-style: italic">#df.columns = [&#39;First&#39;, &#39;Second&#39;, &#39;Third&#39;, &#39;Fourth&#39;, &#39;Fifth&#39;]</span>
</pre>
</div>
</div>
+2 -2
View File
@@ -360,8 +360,8 @@ simpleprediction <span style="color: #666666">=</span> simpletree<span style="co
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> &gt;= </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> + </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> = </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
mse_simpletree<span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> simpleprediction)<span style="color: #666666">**2</span>)
<span style="color: #008000">print</span>(mse_simpletree)
mse_simpletree<span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> simpleprediction)<span style="color: #666666">**2</span>))
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Simple tree:&quot;</span>,mse_simpletree)
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdepth)
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;MSE&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;bias&#39;</span>)
+1 -1
View File
@@ -318,7 +318,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 2, 2022</h4>
<h4>Nov 3, 2022</h4>
</center> <!-- date -->
<br>
+3 -13
View File
@@ -184,7 +184,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 2, 2022</h4>
<h4>Nov 3, 2022</h4>
</center> <!-- date -->
<br>
@@ -1139,12 +1139,6 @@ display(grades)
X = grades.loc[:, grades.columns != <span style="color: #CD5555">&#39;Grade&#39;</span>].values
y = grades.loc[:, grades.columns == <span style="color: #CD5555">&#39;Grade&#39;</span>].values
<span style="color: #658b00">print</span>(X)
<span style="color: #228B22"># Create the encoder.</span>
encoder = OneHotEncoder(handle_unknown=<span style="color: #CD5555">&quot;ignore&quot;</span>)
<span style="color: #228B22"># Assume for simplicity all features are categorical.</span>
encoder.fit(X)
<span style="color: #228B22"># Apply the encoder.</span>
X = encoder.transform(X)
<span style="color: #228B22"># Then do a Classification tree</span>
tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #B452CD">2</span>)
tree_clf.fit(X, y)
@@ -1158,10 +1152,6 @@ export_graphviz(
)
cmd = <span style="color: #CD5555">&#39;dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png&#39;</span>
os.system(cmd)
<span style="color: #228B22">#data_pandas = pd.DataFrame(data,index=[&#39;Frodo&#39;,&#39;Bilbo&#39;,&#39;Aragorn&#39;,&#39;Sam&#39;])</span>
<span style="color: #228B22">#df.columns = [&#39;First&#39;, &#39;Second&#39;, &#39;Third&#39;, &#39;Fourth&#39;, &#39;Fifth&#39;]</span>
</pre>
</div>
</div>
@@ -2228,8 +2218,8 @@ simpleprediction = simpletree.predict(X_test_scaled)
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;{} &gt;= {} + {} = {}&#39;</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**<span style="color: #B452CD">2</span>)
<span style="color: #658b00">print</span>(mse_simpletree)
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**<span style="color: #B452CD">2</span>))
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&quot;Simple tree:&quot;</span>,mse_simpletree)
plt.xlim(<span style="color: #B452CD">1</span>,maxdepth)
plt.plot(polydegree, error, label=<span style="color: #CD5555">&#39;MSE&#39;</span>)
plt.plot(polydegree, bias, label=<span style="color: #CD5555">&#39;bias&#39;</span>)
+3 -13
View File
@@ -262,7 +262,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 2, 2022</h4>
<h4>Nov 3, 2022</h4>
</center> <!-- date -->
<br>
@@ -1147,12 +1147,6 @@ display(grades)
X = grades.loc[:, grades.columns != <span style="color: #CD5555">&#39;Grade&#39;</span>].values
y = grades.loc[:, grades.columns == <span style="color: #CD5555">&#39;Grade&#39;</span>].values
<span style="color: #658b00">print</span>(X)
<span style="color: #228B22"># Create the encoder.</span>
encoder = OneHotEncoder(handle_unknown=<span style="color: #CD5555">&quot;ignore&quot;</span>)
<span style="color: #228B22"># Assume for simplicity all features are categorical.</span>
encoder.fit(X)
<span style="color: #228B22"># Apply the encoder.</span>
X = encoder.transform(X)
<span style="color: #228B22"># Then do a Classification tree</span>
tree_clf = DecisionTreeClassifier(max_depth=<span style="color: #B452CD">2</span>)
tree_clf.fit(X, y)
@@ -1166,10 +1160,6 @@ export_graphviz(
)
cmd = <span style="color: #CD5555">&#39;dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png&#39;</span>
os.system(cmd)
<span style="color: #228B22">#data_pandas = pd.DataFrame(data,index=[&#39;Frodo&#39;,&#39;Bilbo&#39;,&#39;Aragorn&#39;,&#39;Sam&#39;])</span>
<span style="color: #228B22">#df.columns = [&#39;First&#39;, &#39;Second&#39;, &#39;Third&#39;, &#39;Fourth&#39;, &#39;Fifth&#39;]</span>
</pre>
</div>
</div>
@@ -2224,8 +2214,8 @@ simpleprediction = simpletree.predict(X_test_scaled)
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&#39;{} &gt;= {} + {} = {}&#39;</span>.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**<span style="color: #B452CD">2</span>)
<span style="color: #658b00">print</span>(mse_simpletree)
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**<span style="color: #B452CD">2</span>))
<span style="color: #658b00">print</span>(<span style="color: #CD5555">&quot;Simple tree:&quot;</span>,mse_simpletree)
plt.xlim(<span style="color: #B452CD">1</span>,maxdepth)
plt.plot(polydegree, error, label=<span style="color: #CD5555">&#39;MSE&#39;</span>)
plt.plot(polydegree, bias, label=<span style="color: #CD5555">&#39;bias&#39;</span>)
+3 -13
View File
@@ -339,7 +339,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 2, 2022</h4>
<h4>Nov 3, 2022</h4>
</center> <!-- date -->
<br>
@@ -1224,12 +1224,6 @@ display(grades)
X <span style="color: #666666">=</span> grades<span style="color: #666666">.</span>loc[:, grades<span style="color: #666666">.</span>columns <span style="color: #666666">!=</span> <span style="color: #BA2121">&#39;Grade&#39;</span>]<span style="color: #666666">.</span>values
y <span style="color: #666666">=</span> grades<span style="color: #666666">.</span>loc[:, grades<span style="color: #666666">.</span>columns <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;Grade&#39;</span>]<span style="color: #666666">.</span>values
<span style="color: #008000">print</span>(X)
<span style="color: #408080; font-style: italic"># Create the encoder.</span>
encoder <span style="color: #666666">=</span> OneHotEncoder(handle_unknown<span style="color: #666666">=</span><span style="color: #BA2121">&quot;ignore&quot;</span>)
<span style="color: #408080; font-style: italic"># Assume for simplicity all features are categorical.</span>
encoder<span style="color: #666666">.</span>fit(X)
<span style="color: #408080; font-style: italic"># Apply the encoder.</span>
X <span style="color: #666666">=</span> encoder<span style="color: #666666">.</span>transform(X)
<span style="color: #408080; font-style: italic"># Then do a Classification tree</span>
tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=2</span>)
tree_clf<span style="color: #666666">.</span>fit(X, y)
@@ -1243,10 +1237,6 @@ export_graphviz(
)
cmd <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png&#39;</span>
os<span style="color: #666666">.</span>system(cmd)
<span style="color: #408080; font-style: italic">#data_pandas = pd.DataFrame(data,index=[&#39;Frodo&#39;,&#39;Bilbo&#39;,&#39;Aragorn&#39;,&#39;Sam&#39;])</span>
<span style="color: #408080; font-style: italic">#df.columns = [&#39;First&#39;, &#39;Second&#39;, &#39;Third&#39;, &#39;Fourth&#39;, &#39;Fifth&#39;]</span>
</pre>
</div>
</div>
@@ -2301,8 +2291,8 @@ simpleprediction <span style="color: #666666">=</span> simpletree<span style="co
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> &gt;= </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> + </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> = </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
mse_simpletree<span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> simpleprediction)<span style="color: #666666">**2</span>)
<span style="color: #008000">print</span>(mse_simpletree)
mse_simpletree<span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> simpleprediction)<span style="color: #666666">**2</span>))
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Simple tree:&quot;</span>,mse_simpletree)
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdepth)
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;MSE&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;bias&#39;</span>)
Binary file not shown.
File diff suppressed because one or more lines are too long
+2 -11
View File
@@ -742,12 +742,6 @@ display(grades)
X = grades.loc[:, grades.columns != 'Grade'].values
y = grades.loc[:, grades.columns == 'Grade'].values
print(X)
# Create the encoder.
encoder = OneHotEncoder(handle_unknown="ignore")
# Assume for simplicity all features are categorical.
encoder.fit(X)
# Apply the encoder.
X = encoder.transform(X)
# Then do a Classification tree
tree_clf = DecisionTreeClassifier(max_depth=2)
tree_clf.fit(X, y)
@@ -763,9 +757,6 @@ cmd = 'dot -Tpng DataFiles/grade.dot -o DataFiles/grades.png'
os.system(cmd)
#data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
#df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
!ec
@@ -1525,8 +1516,8 @@ for degree in range(1,maxdepth):
print('Var:', variance[degree])
print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**2)
print(mse_simpletree)
mse_simpletree= np.mean( np.mean((y_test - simpleprediction)**2))
print("Simple tree:",mse_simpletree)
plt.xlim(1,maxdepth)
plt.plot(polydegree, error, label='MSE')
plt.plot(polydegree, bias, label='bias')