Skip to content

Commit

Permalink
fix deprecated sklearn syntax as well as more py2/py3 compatiblity fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmu committed Oct 10, 2017
1 parent b541fa4 commit d69e388
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 68 deletions.
3 changes: 1 addition & 2 deletions examples_alt/visualization/clustering.py
Expand Up @@ -23,11 +23,10 @@
windpark = ds.get_windpark(NREL.park_id['tehachapi'], 15, 2004)
X = np.array(windpark.get_powermatrix())

clf = KMeans(k=3)
clf = KMeans(n_clusters=3)
info = []

for turbine in windpark.turbines:

month_power = windml.util.power_features.compute_highlevel_features(turbine)
info.append(month_power)

Expand Down
36 changes: 18 additions & 18 deletions examples_alt/visualization/sequence.py
@@ -1,4 +1,4 @@
"""
'''
Sequence Visualization Based on ISOMAP
-------------------------------------------------------------------------
Expand All @@ -8,11 +8,12 @@
Thereby, the mapping maintains important properties of the original
high-dimensional data so that varying wind conditions and seasonal changes can
be monitored.
"""
'''

# Author: Oliver Kramer <oliver.kramer@uni-oldenburg.de>
# License: BSD 3 clause

from __future__ import print_function
import sklearn
import numpy as np
import pylab as plt
Expand All @@ -28,50 +29,49 @@

X = np.array(windpark.get_powermatrix())
X_train = X[:2000]
X_test = X[2000:2000+200*4]
X_test = X[2000:2000 + 200 * 4]

# computation of ISOMAP projection
print "computation of ISOMAP projection"
print('computation of the ISOMAP projection')

if(sklearn.__version__ == "0.9"):
X_latent = manifold.Isomap(K, out_dim=2).fit_transform(X_train)
else:
X_latent = manifold.Isomap(K, n_components=2).fit_transform(X_train)
X_latent = manifold.Isomap(K, n_components=2).fit_transform(X_train)

# computation of sequence of closest embedded patterns
sequence = []
for x in X_test:
win = 0
smallest = 10E100
for b in range(len(X_train)):
if np.dot(x-X_train[b],x-X_train[b])<smallest:
smallest = np.dot(x-X_train[b],x-X_train[b])
if np.dot(x - X_train[b], x - X_train[b]) < smallest:
smallest = np.dot(x - X_train[b], x - X_train[b])
win = b
sequence.append(X_latent[win])

# normalization of the sequence
sequence = np.array(sequence)
sequence[:,0] = (sequence[:,0]-sequence[:,0].min())/abs(sequence[:,0].max()-sequence[:,0].min())
sequence[:,1] = (sequence[:,1]-sequence[:,1].min())/abs(sequence[:,1].max()-sequence[:,1].min())
col = [[i,j,0.5] for [i,j] in sequence]
sequence[:, 0] = (sequence[:, 0] - sequence[:, 0].min()) / \
abs(sequence[:, 0].max() - sequence[:, 0].min())
sequence[:, 1] = (sequence[:, 1] - sequence[:, 1].min()) / \
abs(sequence[:, 1].max() - sequence[:, 1].min())
col = [[i, j, 0.5] for [i, j] in sequence]

# plotting ...
fig = plt.figure(figsize=(20,4))
fig = plt.figure(figsize=(20, 4))
ax = plt.subplot(4, 1, 1)

for i in range(0,200):
for i in range(0, 200):
ax.bar(i, 1, 1, linewidth=0.0, color=col[i])

ax = plt.subplot(4, 1, 2)
for i in range(200,400):
for i in range(200, 400):
ax.bar(i, 1, 1, linewidth=0.0, color=col[i])

ax = plt.subplot(4, 1, 3)
for i in range(400,600):
for i in range(400, 600):
ax.bar(i, 1, 1, linewidth=0.0, color=col[i])

ax = plt.subplot(4, 1, 4)
for i in range(600,800):
for i in range(600, 800):
ax.bar(i, 1, 1, linewidth=0.0, color=col[i])

plt.show()
11 changes: 6 additions & 5 deletions examples_alt/visualization/show_coord_topo.py
@@ -1,14 +1,15 @@
"""
'''
Topography of a Windpark
-------------------------------------------------------------------------
This example shows the topography of a wind park near Tehachapi. The black dots
illustrate the locations of turbines. The red dot is the target turbine.
"""
'''

# Author: Nils A. Treiber <nils.andre.treiber@uni-oldenburg.de>
# License: BSD 3 clause

from __future__ import print_function
from windml.datasets.nrel import NREL
from windml.visualization.show_coord_topo import show_coord_topo

Expand All @@ -17,8 +18,8 @@

windpark = NREL().get_windpark(NREL.park_id['tehachapi'], 30, 2004)

print "Working on windpark around target turbine", str(windpark.get_target_idx())
print "Plotting windpark ..."
print('Working on windpark around target turbine', str(windpark.get_target_idx()))
print('Plotting windpark ...')

title = "Some Turbines of NREL Data Set"
title = 'Some Turbines of NREL Data Set'
show_coord_topo(windpark, title)
81 changes: 38 additions & 43 deletions examples_alt/visualization/wind_embeddings.py
@@ -1,22 +1,24 @@
"""
'''
Manifold Learning with PCA, LDA, ISOMAP and LLE.
--------------------------------------------------
For various post-provessing purposes, the reduction of the dimensionality of
high-dimensional wind power time series may be desirable. The embedding example
employs dimensionality reduction methods like ISOMAP to embed time series of a
single turbine into 2-dimensional latent spaces.
"""
'''

# Author: Oliver Kramer <oliver.kramer@uni-oldenburg.de>
# Jendrik Poloczek <jendrik.poloczek@madewithtea.com>
# License: BSD 3 clause

from __future__ import print_function
import sklearn
import numpy as np
import pylab as plt
from matplotlib import offsetbox
from sklearn import manifold, datasets, decomposition, lda
from sklearn import manifold, datasets, decomposition
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as lda

from windml.visualization.plot_timeseries import plot_timeseries
from windml.datasets.nrel import NREL
Expand All @@ -26,67 +28,60 @@
windpark = ds.get_windpark(NREL.park_id['tehachapi'], 20, 2004)

X = np.array(windpark.get_powermatrix())
y = np.array(X[:,-1])
y = np.array(X[:, -1], dtype=np.int32)

X=X[:1000]
y=y[:1000]
X = X[:1000]
y = y[:1000]

# scale and plot method of embeddings - from SKLEARN
# scale and plot method of embeddings

def plot_embedding(X, title=None, j=1):
x_min, x_max = np.min(X, 0), np.max(X, 0)
X = (X - x_min) / (x_max - x_min)
x_min, x_max = np.min(X, 0), np.max(X, 0)
X = (X - x_min) / (x_max - x_min)

ax = plt.subplot(2, 2, j)
for i in range(X.shape[0]):
plt.text(X[i, 0], X[i, 1], str(int(y[i])),
color = plt.cm.jet(y[i] / 30.),
fontdict={'weight': 'bold', 'size': 9})
ax = plt.subplot(2, 2, j)
for i in range(X.shape[0]):
plt.text(X[i, 0], X[i, 1], str(int(y[i])),
color=plt.cm.jet(y[i] / 30.),
fontdict={'weight': 'bold', 'size': 9})

if title is not None:
plt.title(title)

if title is not None:
plt.title(title)

j = 1
figure = plt.figure(figsize=(15, 10))
title = "Projection of Wind Time Series"
title = 'Projection of the Wind Time Series'

# computation of PCA projection
print "Computation of PCA Projection"
print('Computation of PCA Projection')

X_pca = decomposition.RandomizedPCA(n_components=2).fit_transform(X)
X_pca = (decomposition
.PCA(svd_solver='randomized',
n_components=2)
.fit_transform(X))

plot_embedding(X_pca,
"PCA "+title,j=1)
plot_embedding(X_pca, 'PCA '+title, j=1)

# computation of LDA projection
print "Computation of LDA Projection"
# computation of the LDA projection
print('Computation of the LDA Projection')
X2 = X.copy()
X2.flat[::X.shape[1] + 1] += 0.01 # make X invertible

X_lda = lda.LDA(n_components=2).fit_transform(X2, y)

plot_embedding(X_lda,
"LDA "+title,j=2)
X_lda = lda(n_components=2).fit_transform(X2, y)
plot_embedding(X_lda, 'LDA '+title, j=2)

# computation of ISOMAP projection
print "Computation of ISOMAP Projection"
if(sklearn.__version__ == "0.9"):
X_iso = manifold.Isomap(K, out_dim=2).fit_transform(X)
else:
X_iso = manifold.Isomap(K, n_components=2).fit_transform(X)
# computation of the ISOMAP projection
print('Computation of the ISOMAP Projection')
X_iso = manifold.Isomap(K, n_components=2).fit_transform(X)

plot_embedding(X_iso,
"ISOMAP "+title,j=3)
plot_embedding(X_iso, 'ISOMAP '+title, j=3)

# computation of LLE projection
print "Computation of LLE Projection"
if(sklearn.__version__ == "0.9"):
clf = manifold.LocallyLinearEmbedding(K, out_dim=2, method='standard')
else:
clf = manifold.LocallyLinearEmbedding(K, n_components=2, method='standard')
# computation of the LLE projection
print('Computation of the LLE Projection')
clf = manifold.LocallyLinearEmbedding(K, n_components=2, method='standard')

X_lle = clf.fit_transform(X)
plot_embedding(X_lle,
"LLE "+title,j=4)
plot_embedding(X_lle, 'LLE '+title, j=4)

plt.show()

0 comments on commit d69e388

Please sign in to comment.