from read_PLAID_data import read_processed_data from linear import SingleLayerModel, DoubleLayerModel, MultiLayerModer def confusion_matrix(preds, labels, conf_matrix): for p, l in zip(preds, labels): conf_matrix[p, l] += 1 return conf_matrix process_start_time = time.time() label_transformer = {'I': 0, 'R': 1, 'NL': 0} feature_select = ['i_pp_rms', 'P_F', 'i_hp1', 'i_hm3', 'i_hp3', 'z_hp3'] x, y = read_processed_data('load', feature_select=feature_select, direaction=1, offset=30, Transformer=label_transformer) print('finished reading data, cost %2.2f s' % (time.time() - process_start_time)) x_mean = np.mean(x, axis=0) x_std = np.std(x, axis=0) for i in range(len(x)): for j in range(len(x[0])): if x_std[j] != 0: x[i][j] = (x[i][j] - x_mean[j]) / x_std[j] x_train = torch.tensor(x).float() y_train = torch.tensor(y.astype(float)).float() dataset = Data.TensorDataset(x_train, y_train)
import sys from sklearn import tree from sklearn.model_selection import train_test_split sys.path.append('data/') from read_PLAID_data import read_processed_data, get_feature_name x, y, index = read_processed_data('type', selected_label=[ 'Fridge', 'Air Conditioner', ], direaction=1, offset=30, each_lenth=1, source='submetered_process/training/') x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=0) x_train = x_train[:, 1:] x_test = x_test[:, 1:] feature_select = get_feature_name( '/home/chaofan/powerknowledge/data/source/submetered_process/training/') for md in range(1, 10): for msl in [1, 5, 10, 15, 20, 25, 50, 100]: decision_tree = tree.DecisionTreeClassifier(max_depth=md, min_samples_leaf=msl) decision_tree.fit(x_train, y_train) score = decision_tree.score(x_test, y_test) print('[max_depth:%02d,min_samples_leaf:%02d] score %.6f' % (md, msl, score))
import sys import time from sklearn.model_selection import train_test_split from sklearn import tree import numpy as np import joblib sys.path.append('data/') sys.path.append('data/source/') sys.path.append('data/model/deep_learning_model') sys.path.append('data/model/knowledge_model') from read_PLAID_data import read_processed_data start_reading_time = time.time() x_is_cool, y_is_cool = read_processed_data('is_cool', type_header='extra label', direaction=1, offset=30) x_is_cool = x_is_cool[:, 1:] x_is_heat, y_is_heat = read_processed_data('is_heat', type_header='extra label', direaction=1, offset=30) x_is_heat = x_is_heat[:, 1:] x_is_light, y_is_light = read_processed_data('is_light', type_header='extra label', direaction=1, offset=30) x_is_light = x_is_light[:, 1:]
y_epoch.numpy()) epoch_loss += loss.item() train_time = time.time() - train_start_time return epoch_acc, train_time def confusion_matrix(preds, labels, conf_matrix): for p, l in zip(preds, labels): conf_matrix[p, l] += 1 return conf_matrix process_start_time = time.time() label_transformer = {'I': 0, 'R': 1, 'NL': 0} x, y = read_processed_data('load', direaction=1, offset=30, Transformer=label_transformer) print('finished reading data, cost %2.2f s' % (time.time() - process_start_time)) x_mean = np.mean(x, axis=0) x_std = np.std(x, axis=0) for i in range(len(x)): for j in range(len(x[0])): if x_std[j] != 0: x[i][j] = (x[i][j] - x_mean[j]) / x_std[j] feature_len = x.shape[1] data_len = x.shape[0] acc_matrix = np.zeros([feature_len, feature_len]) for a in range(feature_len):
import numpy as np import time import sys sys.path.append(r'data/') from read_PLAID_data import read_processed_data process_start_time = time.time() label_transformer = {'0': 0, '1': 1} x, y = read_processed_data('is_heat', type_header='extra label', direaction=1, offset=30, Transformer=label_transformer) print('finished reading data, cost %2.2f s' % (time.time() - process_start_time)) x = x[:, 1:] np.savetxt('model/knowledge_model/heater/y_label.csv', y, delimiter=',') np.savetxt('model/knowledge_model/heater/x.csv', x, delimiter=',')
import sys from sklearn import tree from sklearn.model_selection import train_test_split sys.path.append('data/') from read_PLAID_data import read_processed_data x, y = read_processed_data('type', selected_label=[ 'Laptop', 'Compact Fluorescent Lamp', 'Incandescent Light Bulb' ], direaction=1, offset=30, each_lenth=1) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=0) for md in range(1, 10): for msl in [1, 5, 10, 15, 20, 25, 50, 100]: decision_tree = tree.DecisionTreeClassifier(max_depth=md, min_samples_leaf=msl) decision_tree.fit(x_train, y_train) score = decision_tree.score(x_test, y_test) print('[max_depth:%02d,min_samples_leaf:%02d] score %.6f' % (md, msl, score)) y_predict = decision_tree.predict(x_test)