Skip to content
Snippets Groups Projects
Commit 538f8e8d authored by bjje's avatar bjje
Browse files

Consistent plt use

parent 13c26834
Branches
No related tags found
No related merge requests found
Showing
with 189 additions and 213 deletions
# exercise 10.1.1
import importlib_resources
from matplotlib.pyplot import figure, show
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.cluster import k_means
......@@ -24,8 +24,8 @@ K = 4
centroids, cls, inertia = k_means(X, K)
# Plot results:
figure(figsize=(14, 9))
plt.figure(figsize=(14, 9))
clusterplot(X, cls, centroids, y)
show()
plt.show()
print("Ran Exercise 10.1.1")
# exercise 10.1.3
import importlib_resources
import numpy as np
from matplotlib.pyplot import figure, legend, plot, show, title, ylim
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.cluster import k_means
......@@ -35,12 +35,12 @@ for k in range(K-1):
# Plot results:
figure(1)
title('Cluster validity')
plot(np.arange(K-1)+2, Rand)
plot(np.arange(K-1)+2, Jaccard)
plot(np.arange(K-1)+2, NMI)
legend(['Rand', 'Jaccard', 'NMI'], loc=4)
show()
plt.figure(1)
plt.title('Cluster validity')
plt.plot(np.arange(K-1)+2, Rand)
plt.plot(np.arange(K-1)+2, Jaccard)
plt.plot(np.arange(K-1)+2, NMI)
plt.legend(['Rand', 'Jaccard', 'NMI'], loc=4)
plt.show()
print('Ran Exercise 10.1.3')
......@@ -11,16 +11,15 @@ filename = importlib_resources.files("dtuimldmtools").joinpath("data/wildfaces.m
mat_data = loadmat(filename)
#filename = importlib_resources.files("dtuimldmtools").joinpath("data/digits.mat") #<-- uncomment this for using the digits dataset
#mat_data = loadmat('../Data/digits.mat') #<-- uncomment this for using the digits dataset
X = mat_data['X']
N, M = X.shape
# Image resolution and number of colors
x = 40 #<-- change this for using the digits dataset
y = 40 #<-- change this for using the digits dataset
c = 3 #<-- change this for using the digits dataset
# Number of clusters:
K = 10
......
# exercise 10.2.1
import importlib_resources
from matplotlib.pyplot import figure, show
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, fcluster, linkage
from scipy.io import loadmat
......@@ -27,16 +27,16 @@ Z = linkage(X, method=Method, metric=Metric)
# Compute and display clusters by thresholding the dendrogram
Maxclust = 4
cls = fcluster(Z, criterion="maxclust", t=Maxclust)
figure(1)
plt.figure(1)
clusterplot(X, cls.reshape(cls.shape[0], 1), y=y)
# Display dendrogram
max_display_levels = 6
figure(2, figsize=(10, 4))
plt.figure(2, figsize=(10, 4))
dendrogram(
Z, truncate_mode="level", p=max_display_levels, color_threshold=Z[-Maxclust + 1, 2]
)
show()
plt.show()
print("Ran Exercise 10.2.1")
# exercise 11.1.1
import importlib_resources
import numpy as np
from matplotlib.pyplot import figure, show
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.mixture import GaussianMixture
......@@ -59,9 +59,9 @@ if cov_type.lower() == "diag":
covs = new_covs
# Plot results:
figure(figsize=(14, 9))
plt.figure(figsize=(14, 9))
clusterplot(X, clusterid=cls, centroids=cds, y=y, covars=covs)
show()
plt.show()
## In case the number of features != 2, then a subset of features most be plotted instead.
# figure(figsize=(14,9))
......
# exercise 11.1.5
import importlib_resources
import numpy as np
from matplotlib.pyplot import figure, legend, plot, show, xlabel
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn import model_selection
from sklearn.mixture import GaussianMixture
......@@ -68,12 +68,12 @@ for t, K in enumerate(KRange):
# Plot results
figure(1)
plot(KRange, BIC, "-*b")
plot(KRange, AIC, "-xr")
plot(KRange, 2 * CVE, "-ok")
legend(["BIC", "AIC", "Crossvalidation"])
xlabel("K")
show()
plt.figure(1)
plt.plot(KRange, BIC, "-*b")
plt.plot(KRange, AIC, "-xr")
plt.plot(KRange, 2 * CVE, "-ok")
plt.legend(["BIC", "AIC", "Crossvalidation"])
plt.xlabel("K")
plt.show()
print("Ran Exercise 11.1.5")
# exercise 11_2_1
import numpy as np
from matplotlib.pyplot import figure, hist, show
import matplotlib.pyplot as plt
# Number of data objects
N = 1000
......@@ -27,8 +27,8 @@ for c_id, c_size in enumerate(c_sizes):
# Plot histogram of sampled data
figure()
hist(X, x)
show()
plt.figure()
plt.hist(X, x)
plt.show()
print("Ran Exercise 11.2.1")
# exercise 11.2.2
import numpy as np
from matplotlib.pyplot import figure, hist, plot, show, subplot, title
import matplotlib.pyplot as plt
from scipy.stats.kde import gaussian_kde
# Draw samples from mixture of gaussians (as in exercise 11.1.1)
......@@ -24,13 +24,13 @@ xe = np.linspace(-10, 10, 100)
kde = gaussian_kde(X.ravel())
# Plot kernel density estimate
figure(figsize=(6, 7))
subplot(2, 1, 1)
hist(X, x)
title("Data histogram")
subplot(2, 1, 2)
plot(xe, kde.evaluate(xe))
title("Kernel density estimate")
show()
plt.figure(figsize=(6, 7))
plt.subplot(2, 1, 1)
plt.hist(X, x)
plt.title("Data histogram")
plt.subplot(2, 1, 2)
plt.plot(xe, kde.evaluate(xe))
plt.title("Kernel density estimate")
plt.show()
print("Ran Exercise 11.2.2")
# exercise 11.2.3
import numpy as np
from matplotlib.pyplot import figure, hist, plot, show, subplot, title
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestNeighbors
# Draw samples from mixture of gaussians (as in exercise 11.1.1)
......@@ -39,22 +39,23 @@ knn_avg_rel_density = knn_density / (knn_densityX[i[:, 1:]].sum(axis=1) / K)
# Plot KNN density
figure(figsize=(6, 7))
subplot(2, 1, 1)
hist(X, x)
title("Data histogram")
subplot(2, 1, 2)
plot(xe, knn_density)
title("KNN density")
plt.figure(figsize=(6, 7))
plt.subplot(2, 1, 1)
plt.hist(X, x)
plt.title("Data histogram")
plt.subplot(2, 1, 2)
plt.plot(xe, knn_density)
plt.title("KNN density")
# Plot KNN average relative density
figure(figsize=(6, 7))
subplot(2, 1, 1)
hist(X, x)
title("Data histogram")
subplot(2, 1, 2)
plot(xe, knn_avg_rel_density)
title("KNN average relative density")
show()
plt.figure(figsize=(6, 7))
plt.subplot(2, 1, 1)
plt.hist(X, x)
plt.title("Data histogram")
plt.subplot(2, 1, 2)
plt.plot(xe, knn_avg_rel_density)
plt.title("KNN average relative density")
plt.show()
print("Ran Exercise 11.2.3")
# exercise 11.3.1
import numpy as np
from matplotlib.pyplot import bar, figure, show, title
import matplotlib.pyplot as plt
from scipy.stats.kde import gaussian_kde
# Draw samples from mixture of gaussians (as in exercise 11.1.1), add outlier
......@@ -28,9 +28,9 @@ scores.sort()
print("The index of the lowest density object: {0}".format(idx[0]))
# Plot kernel density estimate
figure()
bar(range(20), scores[:20])
title("Outlier score")
show()
plt.figure()
plt.bar(range(20), scores[:20])
plt.title("Outlier score")
plt.show()
print("Ran Exercise 11.3.1")
# exercise 11.3.2
import numpy as np
from matplotlib.pyplot import bar, figure, plot, show, title
import matplotlib.pyplot as plt
from dtuimldmtools import gausKernelDensity
......@@ -43,17 +43,18 @@ density = density[i]
print("Lowest density: {0} for data object: {1}".format(density[0], i[0]))
# Plot density estimate of outlier score
figure(1)
bar(
plt.figure(1)
plt.bar(
range(20),
density[:20].reshape(
-1,
),
)
title("Density estimate")
figure(2)
plot(logP)
title("Optimal width")
show()
plt.title("Density estimate")
plt.figure(2)
plt.plot(logP)
plt.title("Optimal width")
plt.show()
print("Ran Exercise 11.3.2")
# exercise 11.4.1
import importlib_resources
import numpy as np
from matplotlib.pyplot import (
bar,
cm,
figure,
imshow,
show,
subplot,
title,
xticks,
yticks,
)
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.neighbors import NearestNeighbors
......@@ -56,19 +46,19 @@ density = density[i].reshape(
)
# Plot density estimate of outlier score
figure(1)
bar(range(20), density[:20])
title("Density estimate")
plt.figure(1)
plt.bar(range(20), density[:20])
plt.title("Density estimate")
# Plot possible outliers
figure(2)
plt.figure(2)
for k in range(1, 21):
subplot(4, 5, k)
imshow(np.reshape(X[i[k], :], (16, 16)).T, cmap=cm.binary)
xticks([])
yticks([])
plt.subplot(4, 5, k)
plt.imshow(np.reshape(X[i[k], :], (16, 16)).T, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
if k == 3:
title("Gaussian Kernel Density: Possible outliers")
plt.title("Gaussian Kernel Density: Possible outliers")
### K-neighbors density estimator
......@@ -94,18 +84,19 @@ i = dens.argsort()
dens = dens[i]
# Plot k-neighbor estimate of outlier score (distances)
figure(3)
bar(range(20), dens[:20])
title("KNN density: Outlier score")
plt.figure(3)
plt.bar(range(20), dens[:20])
plt.title("KNN density: Outlier score")
# Plot possible outliers
figure(4)
plt.figure(4)
for k in range(1, 21):
subplot(4, 5, k)
imshow(np.reshape(X[i[k], :], (16, 16)).T, cmap=cm.binary)
xticks([])
yticks([])
plt.subplot(4, 5, k)
plt.imshow(np.reshape(X[i[k], :], (16, 16)).T, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
if k == 3:
title("KNN density: Possible outliers")
plt.title("KNN density: Possible outliers")
### K-nearest neigbor average relative density
......@@ -123,18 +114,18 @@ i_avg_rel = avg_rel_density.argsort()
avg_rel_density = avg_rel_density[i_avg_rel]
# Plot k-neighbor estimate of outlier score (distances)
figure(5)
bar(range(20), avg_rel_density[:20])
title("KNN average relative density: Outlier score")
plt.figure(5)
plt.bar(range(20), avg_rel_density[:20])
plt.title("KNN average relative density: Outlier score")
# Plot possible outliers
figure(6)
plt.figure(6)
for k in range(1, 21):
subplot(4, 5, k)
imshow(np.reshape(X[i_avg_rel[k], :], (16, 16)).T, cmap=cm.binary)
xticks([])
yticks([])
plt.subplot(4, 5, k)
plt.imshow(np.reshape(X[i_avg_rel[k], :], (16, 16)).T, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
if k == 3:
title("KNN average relative density: Possible outliers")
plt.title("KNN average relative density: Possible outliers")
### Distance to 5'th nearest neighbor outlier score
K = 5
......@@ -150,29 +141,30 @@ i = score.argsort()
score = score[i[::-1]]
# Plot k-neighbor estimate of outlier score (distances)
figure(7)
bar(range(20), score[:20])
title("5th neighbor distance: Outlier score")
plt.figure(7)
plt.bar(range(20), score[:20])
plt.title("5th neighbor distance: Outlier score")
# Plot possible outliers
figure(8)
plt.figure(8)
for k in range(1, 21):
subplot(4, 5, k)
imshow(np.reshape(X[i[k], :], (16, 16)).T, cmap=cm.binary)
xticks([])
yticks([])
plt.subplot(4, 5, k)
plt.imshow(np.reshape(X[i[k], :], (16, 16)).T, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
if k == 3:
title("5th neighbor distance: Possible outliers")
plt.title("5th neighbor distance: Possible outliers")
# Plot random digits (the first 20 in the data set), for comparison
figure(9)
plt.figure(9)
for k in range(1, 21):
subplot(4, 5, k)
imshow(np.reshape(X[k, :], (16, 16)).T, cmap=cm.binary)
xticks([])
yticks([])
plt.subplot(4, 5, k)
plt.imshow(np.reshape(X[k, :], (16, 16)).T, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
if k == 3:
title("Random digits from data set")
show()
plt.title("Random digits from data set")
plt.show()
print("Ran Exercise 11.4.1")
......@@ -80,3 +80,5 @@ N, M = X.shape
# Finally, the last variable that we need to have the dataset in the
# "standard representation" for the course, is the number of classes, C:
C = len(classNames)
print("Ran 1.5.1 -- loaded the Iris data")
\ No newline at end of file
# exercise 4.1.1
import numpy as np
from matplotlib.pyplot import figure, hist, plot, show, subplot, title
import matplotlib.pyplot as plt
# Number of samples
N = 200
......@@ -21,10 +20,10 @@ X = np.random.normal(mu, s, N).T
X = np.random.randn(N).T * s + mu
# Plot the samples and histogram
figure(figsize=(12, 4))
title("Normal distribution")
subplot(1, 2, 1)
plot(X, ".")
subplot(1, 3, 3)
hist(X, bins=nbins)
show()
plt.figure(figsize=(12, 4))
plt.title("Normal distribution")
plt.subplot(1, 2, 1)
plt.plot(X, ".")
plt.subplot(1, 3, 3)
plt.hist(X, bins=nbins)
plt.show()
# exercise 4.1.2
import numpy as np
from matplotlib.pyplot import figure, hist, plot, show, subplot, title
import matplotlib.pyplot as plt
# Number of samples
N = 200
......@@ -20,13 +20,6 @@ X = np.random.normal(mu, s, N).T
# or equally:
X = np.random.randn(N).T * s + mu
# Plot the samples and histogram
figure()
title("Normal distribution")
subplot(1, 2, 1)
plot(X, "x")
subplot(1, 2, 2)
hist(X, bins=nbins)
# Compute empirical mean and standard deviation
mu_ = X.mean()
......@@ -37,5 +30,12 @@ print("Theoretical std.dev.: ", s)
print("Empirical mean: ", mu_)
print("Empirical std.dev.: ", s_)
show()
# Plot the samples and histogram
plt.figure()
plt.title("Normal distribution")
plt.subplot(1, 2, 1)
plt.plot(X, "x")
plt.subplot(1, 2, 2)
plt.hist(X, bins=nbins)
plt.show()
# exercise 4.1.3
import numpy as np
from matplotlib.pyplot import figure, hist, plot, show, subplot, title
import matplotlib.pyplot as plt
from scipy import stats
# Number of samples
......@@ -22,14 +21,14 @@ X = np.random.normal(mu, s, N).T
X = np.random.randn(N).T * s + mu
# Plot the histogram
f = figure()
title("Normal distribution")
hist(X, bins=nbins, density=True)
f = plt.figure()
plt.title("Normal distribution")
plt.hist(X, bins=nbins, density=True)
# Over the histogram, plot the theoretical probability distribution function:
x = np.linspace(X.min(), X.max(), 1000)
pdf = stats.norm.pdf(x, loc=17, scale=2)
plot(x, pdf, ".", color="red")
plt.plot(x, pdf, ".", color="red")
# Compute empirical mean and standard deviation
mu_ = X.mean()
......@@ -40,5 +39,5 @@ print("Theoretical std.dev.: ", s)
print("Empirical mean: ", mu_)
print("Empirical std.dev.: ", s_)
show()
plt.show()
# exercise 4.1.5
import numpy as np
from matplotlib.pyplot import (
cm,
colorbar,
figure,
hist,
imshow,
plot,
show,
subplot,
suptitle,
title,
xlabel,
xticks,
ylabel,
yticks,
)
import matplotlib.pyplot as plt
# Number of samples
N = 1000
......@@ -44,25 +29,25 @@ X = np.random.multivariate_normal(mu, S, N)
# Plot scatter plot of data
figure(figsize=(12, 8))
suptitle("2-D Normal distribution")
plt.figure(figsize=(12, 8))
plt.suptitle("2-D Normal distribution")
subplot(1, 2, 1)
plot(X[:, 0], X[:, 1], "x")
xlabel("x1")
ylabel("x2")
title("Scatter plot of data")
plt.subplot(1, 2, 1)
plt.plot(X[:, 0], X[:, 1], "x")
plt.xlabel("x1")
plt.ylabel("x2")
plt.title("Scatter plot of data")
subplot(1, 2, 2)
plt.subplot(1, 2, 2)
x = np.histogram2d(X[:, 0], X[:, 1], nbins)
imshow(x[0], cmap=cm.gray_r, interpolation="None", origin="lower")
colorbar()
xlabel("x1")
ylabel("x2")
xticks([])
yticks([])
title("2D histogram")
show()
plt.imshow(x[0], cmap=plt.cm.gray_r, interpolation="None", origin="lower")
plt.colorbar()
plt.xlabel("x1")
plt.ylabel("x2")
plt.xticks([])
plt.yticks([])
plt.title("2D histogram")
plt.show()
# exercise 4.1.6
import importlib_resources
import numpy as np
import scipy.linalg as linalg
from matplotlib.pyplot import cm, figure, imshow, show, subplot, title, xticks, yticks
import matplotlib.pyplot as plt
from scipy.io import loadmat
filename = importlib_resources.files("dtuimldmtools").joinpath("data/zipdata.mat")
......@@ -31,19 +30,19 @@ mu = X.mean(axis=0)
s = X.std(ddof=1, axis=0)
S = np.cov(X, rowvar=0, ddof=1)
figure()
subplot(1, 2, 1)
plt.figure()
plt.subplot(1, 2, 1)
I = np.reshape(mu, (16, 16))
imshow(I, cmap=cm.gray_r)
title("Mean")
xticks([])
yticks([])
subplot(1, 2, 2)
plt.imshow(I, cmap=plt.cm.gray_r)
plt.title("Mean")
plt.xticks([])
plt.yticks([])
plt.subplot(1, 2, 2)
I = np.reshape(s, (16, 16))
imshow(I, cmap=cm.gray_r)
title("Standard deviation")
xticks([])
yticks([])
plt.imshow(I, cmap=plt.cm.gray_r)
plt.title("Standard deviation")
plt.xticks([])
plt.yticks([])
show()
plt.show()
......@@ -2,7 +2,7 @@
import importlib_resources
import numpy as np
from matplotlib.pyplot import cm, figure, imshow, show, subplot, title, xticks, yticks
import matplotlib.pyplot as plt
from scipy.io import loadmat
filename = importlib_resources.files("dtuimldmtools").joinpath("data/zipdata.mat")
......@@ -39,15 +39,15 @@ for i in range(ngen):
Xgen[i] = np.multiply(Xgen[i], s) + mu
# Plot images
figure()
plt.figure()
for k in range(ngen):
subplot(2, int(np.ceil(ngen / 2.0)), k + 1)
plt.subplot(2, int(np.ceil(ngen / 2.0)), k + 1)
I = np.reshape(Xgen[k, :], (16, 16))
imshow(I, cmap=cm.gray_r)
xticks([])
yticks([])
plt.imshow(I, cmap=plt.cm.gray_r)
plt.xticks([])
plt.yticks([])
if k == 1:
title("Digits: 1-D Normal")
plt.title("Digits: 1-D Normal")
# Generate 10 samples from multivariate normal distribution
......@@ -58,15 +58,15 @@ Xmvgen = np.random.multivariate_normal(mu, S, ngen)
# Plot images
figure()
plt.figure()
for k in range(ngen):
subplot(2, int(np.ceil(ngen / 2.0)), k + 1)
plt.subplot(2, int(np.ceil(ngen / 2.0)), k + 1)
I = np.reshape(Xmvgen[k, :], (16, 16))
imshow(I, cmap=cm.gray_r)
xticks([])
yticks([])
plt.imshow(I, cmap=plt.cm.gray_r)
plt.xticks([])
plt.yticks([])
if k == 1:
title("Digits: Multivariate Normal")
plt.title("Digits: Multivariate Normal")
show()
plt.show()
# exercise 5.1.2
from os import getcwd
from platform import system
import matplotlib.pyplot as plt
import numpy as np
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment