Commit 3078eda2 authored by Graeme Weatherill's avatar Graeme Weatherill

Merge branch 'group1_add_mark' into 'master'

Adds code/notebooks from Mark Rudolf

See merge request ml-gfz/hackathons/20200503_gfz!2
parents 133d5cff 9d846b4f
import pandas as pd
import numpy as np
from sklearn.preprocessing import normalize
from keras.models import Sequential
from keras.layers import Dense,Dropout,Conv1D, Flatten, MaxPooling1D
from keras import optimizers
from keras import backend as K
from sklearn.model_selection import train_test_split
import sklearn
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
import os
import sys
df = pd.read_csv("LUCAS.csv",header=0)
#XN = df[df.columns[6:]].values
#XD = (XN[:,4:np.shape(XN)[1]-1]-XN[:,5:np.shape(XN)[1]])/2
#X3 = X2[:,np.random.choice(np.arange(np.shape(XN)[1]),10)]
input_data = df[df.columns[4:]].values
output_data = df[["SOC"]].values
#out_data = df[df.columns[4:6]].values
#input_data = XD
from sklearn.model_selection import train_test_split
input_data = np.expand_dims(input_data, axis=2)
input_train, input_test, output_train, output_test = train_test_split(input_data, output_data, test_size=0.33, random_state=42)
input_shape = np.shape(input_train)
#idx = np.random.permutation(np.shape(input_data)[0])
#ida = idx[:int(0.5*len(input_data))]
#idb = idx[int(0.5*len(input_data)):]
#input_train = input_data[ida]
#input_test = input_data[idb]
#output_train = out_data[ida]
#output_test = out_data[idb]
def coeff_determination(y_true, y_pred):
from keras import backend as K
SS_res = K.sum(K.square( y_true-y_pred ))
SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
return ( 1 - SS_res/(SS_tot + K.epsilon()) )
#Build keras NN model
K.clear_session()
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=27, activation='relu', input_shape = (1000,1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(1, activation="relu"))
##model.add(Conv1D(64,2, activation="relu", input_dim=len(input_train[0])))
#model.add(Conv1D(filters=64, kernel_size=27, activation='relu', input_shape = (1000,1)))
##model.add(Dense(8, activation="softmax"))
#model.add(Dropout(0.5))
#model.add(MaxPooling1D(pool_size=2))
#model.add(Flatten())
#model.add(Dense(64, activation="relu"))
#model.add(Dense(1, activation="relu"))
model.compile(optimizer="RMSprop", loss="mean_squared_error",metrics=[coeff_determination])
model.fit(input_train,output_train,epochs=200,batch_size=32)
calculated_cal = model.predict(input_train)
calculated_val = model.predict(input_test)
rmse_train = np.sqrt(mean_squared_error(output_train,calculated_cal))
rmse_val = np.sqrt(mean_squared_error(output_test,calculated_val))
R2_train=sklearn.metrics.r2_score(output_train,calculated_cal)
R2_val=sklearn.metrics.r2_score(output_test,calculated_val)
print(rmse_train)
print(rmse_val)
print(R2_train)
print(R2_val)
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
# -*- coding: utf-8 -*-
from __future__ import division
import numpy as np
def cmdscale(D):
"""
Classical multidimensional scaling (MDS)
Code by Francis Song (song.francis@gmail.com)
http://www.nervouscomputer.com/hfs/cmdscale-in-python/
Parameters
----------
D : (n, n) array
Symmetric distance matrix.
Returns
-------
Y : (n, p) array
Configuration matrix. Each column represents a dimension. Only the
p dimensions corresponding to positive eigenvalues of B are returned.
Note that each dimension is only determined up to an overall sign,
corresponding to a reflection.
e : (n,) array
Eigenvalues of B.
"""
# Number of points
n = len(D)
# Centering matrix
H = np.eye(n) - np.ones((n, n))/n
# YY^T
B = -H.dot(D**2).dot(H)/2
# Diagonalize
evals, evecs = np.linalg.eigh(B)
# Sort by eigenvalue in descending order
idx = np.argsort(evals)[::-1]
evals = evals[idx]
evecs = evecs[:,idx]
# Compute the coordinates using positive-eigenvalued components only
w, = np.where(evals > 0)
L = np.diag(np.sqrt(evals[w]))
V = evecs[:,w]
Y = V.dot(L)
return Y, evals
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment