Skip to content
Snippets Groups Projects
Commit a643c58b authored by s204797's avatar s204797
Browse files

plot fixes

parent 9eadc843
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ from matplotlib.pyplot import ( ...@@ -14,6 +14,7 @@ from matplotlib.pyplot import (
xlabel, xlabel,
ylabel, ylabel,
yticks, yticks,
tight_layout
) )
from scipy.io import loadmat from scipy.io import loadmat
...@@ -99,6 +100,7 @@ for d in range(D): ...@@ -99,6 +100,7 @@ for d in range(D):
I = np.reshape(W[digit_ix, :] + X.mean(0), (16, 16)) I = np.reshape(W[digit_ix, :] + X.mean(0), (16, 16))
imshow(I, cmap=cm.gray_r) imshow(I, cmap=cm.gray_r)
title("Reconstr.") title("Reconstr.")
tight_layout()
# Visualize the pricipal components # Visualize the pricipal components
...@@ -110,6 +112,7 @@ for k in range(K): ...@@ -110,6 +112,7 @@ for k in range(K):
I = np.reshape(V[:, k], (16, 16)) I = np.reshape(V[:, k], (16, 16))
imshow(I, cmap=cm.hot) imshow(I, cmap=cm.hot)
title("PC{0}".format(k + 1)) title("PC{0}".format(k + 1))
tight_layout()
# output to screen # output to screen
show() show()
......
...@@ -37,6 +37,9 @@ sim_to_index = sorted(zip(sim,noti)) ...@@ -37,6 +37,9 @@ sim_to_index = sorted(zip(sim,noti))
plt.figure(figsize=(12,8)) plt.figure(figsize=(12,8))
plt.subplot(3,1,1) plt.subplot(3,1,1)
# set font size
plt.rcParams.update({'font.size': 16})
img_hw = int(np.sqrt(len(X[0]))) img_hw = int(np.sqrt(len(X[0])))
img = np.reshape(X[i], (img_hw,img_hw)) img = np.reshape(X[i], (img_hw,img_hw))
if transpose: img = img.T if transpose: img = img.T
......
# exercise 4.1.1 # exercise 4.1.1
import numpy as np import numpy as np
from matplotlib.pyplot import figure, hist, plot, show, subplot, title from matplotlib.pyplot import figure, hist, plot, show, subplots, title
# Number of samples # Number of samples
N = 200 N = 200
...@@ -21,12 +21,10 @@ X = np.random.normal(mu, s, N).T ...@@ -21,12 +21,10 @@ X = np.random.normal(mu, s, N).T
X = np.random.randn(N).T * s + mu X = np.random.randn(N).T * s + mu
# Plot the samples and histogram # Plot the samples and histogram
figure(figsize=(12, 4)) fig, ax = subplots(1,2, figsize = (12,4))
title("Normal distribution") fig.suptitle("Normal distribution")
subplot(1, 2, 1) ax[0].plot(X,".")
plot(X, ".") ax[1].hist(X,bins=nbins)
subplot(1, 3, 3)
hist(X, bins=nbins)
show() show()
print("Ran Exercise 4.1.1") print("Ran Exercise 4.1.1")
...@@ -13,6 +13,7 @@ from matplotlib.pyplot import ( ...@@ -13,6 +13,7 @@ from matplotlib.pyplot import (
xticks, xticks,
ylim, ylim,
yticks, yticks,
tight_layout
) )
from scipy.io import loadmat from scipy.io import loadmat
from scipy.stats import zscore from scipy.stats import zscore
...@@ -34,6 +35,7 @@ figure() ...@@ -34,6 +35,7 @@ figure()
title("Wine: Boxplot") title("Wine: Boxplot")
boxplot(X) boxplot(X)
xticks(range(1, M + 1), attributeNames, rotation=45) xticks(range(1, M + 1), attributeNames, rotation=45)
tight_layout()
# From this it is clear that there are some outliers in the Alcohol # From this it is clear that there are some outliers in the Alcohol
# attribute (10x10^14 is clearly not a proper value for alcohol content) # attribute (10x10^14 is clearly not a proper value for alcohol content)
...@@ -44,6 +46,7 @@ figure(figsize=(12, 6)) ...@@ -44,6 +46,7 @@ figure(figsize=(12, 6))
title("Wine: Boxplot (standarized)") title("Wine: Boxplot (standarized)")
boxplot(zscore(X, ddof=1), attributeNames) boxplot(zscore(X, ddof=1), attributeNames)
xticks(range(1, M + 1), attributeNames, rotation=45) xticks(range(1, M + 1), attributeNames, rotation=45)
tight_layout()
# This plot reveals that there are clearly some outliers in the Volatile # This plot reveals that there are clearly some outliers in the Volatile
# acidity, Density, and Alcohol attributes, i.e. attribute number 2, 8, # acidity, Density, and Alcohol attributes, i.e. attribute number 2, 8,
......
...@@ -3,7 +3,7 @@ import sklearn.linear_model as lm ...@@ -3,7 +3,7 @@ import sklearn.linear_model as lm
# requires wine data from exercise 5.1.5 # requires wine data from exercise 5.1.5
from ex5_1_5 import * from ex5_1_5 import *
from matplotlib.pylab import figure, hist, plot, show, subplot, xlabel, ylabel from matplotlib.pylab import figure, hist, plot, show, subplots, xlabel, ylabel, tight_layout
# Split dataset into features and target vector # Split dataset into features and target vector
alcohol_idx = attributeNames.index("Alcohol") alcohol_idx = attributeNames.index("Alcohol")
...@@ -21,13 +21,15 @@ y_est = model.predict(X) ...@@ -21,13 +21,15 @@ y_est = model.predict(X)
residual = y_est - y residual = y_est - y
# Display scatter plot # Display scatter plot
figure() fig, ax = subplots(nrows=2,ncols=1)
subplot(2, 1, 1) ax[0].plot(y, y_est, ".")
plot(y, y_est, ".") ax[0].set_xlabel("Alcohol content (true)")
xlabel("Alcohol content (true)") ax[0].set_ylabel("Alcohol content (estimated)")
ylabel("Alcohol content (estimated)")
subplot(2, 1, 2) ax[1].hist(residual, 40)
hist(residual, 40) ax[1].set_xlabel("Residual")
ax[1].set_ylabel("Count")
tight_layout()
show() show()
......
...@@ -3,7 +3,7 @@ import sklearn.linear_model as lm ...@@ -3,7 +3,7 @@ import sklearn.linear_model as lm
# requires data from exercise 5.1.4 # requires data from exercise 5.1.4
from ex5_1_5 import * from ex5_1_5 import *
from matplotlib.pylab import figure, hist, plot, show, subplot, xlabel, ylabel from matplotlib.pylab import figure, hist, plot, show, subplot, xlabel, ylabel, tight_layout
# Split dataset into features and target vector # Split dataset into features and target vector
alcohol_idx = attributeNames.index("Alcohol") alcohol_idx = attributeNames.index("Alcohol")
...@@ -54,6 +54,7 @@ plot(Xfava, residual, ".r") ...@@ -54,6 +54,7 @@ plot(Xfava, residual, ".r")
xlabel("Fixed*Volatile Acidity") xlabel("Fixed*Volatile Acidity")
ylabel("Residual") ylabel("Residual")
tight_layout()
show() show()
print("Ran Exercise 5.2.5") print("Ran Exercise 5.2.5")
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import importlib_resources import importlib_resources
import numpy as np import numpy as np
import sklearn.linear_model as lm import sklearn.linear_model as lm
from matplotlib.pyplot import clim, figure, plot, show, subplot, title, xlabel, ylabel from matplotlib.pyplot import clim, figure, plot, show, subplot, title, xlabel, ylabel, subplots, suptitle
from scipy.io import loadmat from scipy.io import loadmat
from sklearn import model_selection from sklearn import model_selection
...@@ -117,13 +117,18 @@ else: ...@@ -117,13 +117,18 @@ else:
y_est= m.predict(X[:,ff]) y_est= m.predict(X[:,ff])
residual=y-y_est residual=y-y_est
figure(k+1, figsize=(12,6)) # Determine the number of rows and columns for subplots
title('Residual error vs. Attributes for features selected in cross-validation fold {0}'.format(f)) nrows = int(len(ff) ** 0.5)
ncols = int(len(ff) / nrows) + (len(ff) % nrows > 0)
fig, ax = subplots(nrows=nrows, ncols=ncols, num=k+1, figsize=(12,6))
ax = ax.flatten()
suptitle('Residual error vs. Attributes for features selected in cross-validation fold {0}'.format(f))
for i in range(0,len(ff)): for i in range(0,len(ff)):
subplot(2, int( np.ceil(len(ff)/2)), i+1) ax[i].plot(X[:,ff[i]],residual,'.')
plot(X[:,ff[i]],residual,'.') ax[i].set_xlabel(attributeNames[ff[i]])
xlabel(attributeNames[ff[i]]) ax[i].set_ylabel('residual error')
ylabel('residual error')
show() show()
......
...@@ -149,6 +149,7 @@ summaries_axes[1].set_xlabel("Fold") ...@@ -149,6 +149,7 @@ summaries_axes[1].set_xlabel("Fold")
summaries_axes[1].set_xticks(np.arange(1, K + 1)) summaries_axes[1].set_xticks(np.arange(1, K + 1))
summaries_axes[1].set_ylabel("Error rate") summaries_axes[1].set_ylabel("Error rate")
summaries_axes[1].set_title("Test misclassification rates") summaries_axes[1].set_title("Test misclassification rates")
plt.tight_layout()
# Show the plots # Show the plots
# plt.show(decision_boundaries.number) # try these lines if the following code fails (depends on package versions) # plt.show(decision_boundaries.number) # try these lines if the following code fails (depends on package versions)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment