def main(): data = getdata(sys.argv[1]) e = data[0] #[[0,0],[0,1],[1,0],[1,1]] e_num = data[1] #4 for i in range(e_num): print(i,end=" ") for j in range(inputNum): print(e[i][j],end=" ") hiddenOut = calcHidden(e[i]) o = calcOutput(hiddenOut) print(o)
def main(): w = [1.0,1.0,1.5] data = getdata(sys.argv[1]) #e[0] = [[0, 0], [0, 1], [1, 0], [1, 1]] , e[1] = 4 e = data[0] e_num = data[1] for i in range(e_num): print(i,end=" ") for j in range(INPUT_NUM): print(e[i][j],end=" ") o = forward(w,e[i]) #順伝播 print(o)
from sklearn.ensemble import AdaBoostClassifier from getData import getdata, evaluate, MCC, Get_f1_score, Get_Accuracy, Get_Precision_score, Get_Recall clf = AdaBoostClassifier() address = '' x_train, y_train, x_test, y_test = getdata(address) clf.fit(x_train, y_train) y_predict = clf.predict(x_test) accuracy = Get_Accuracy(y_test, y_predict) accuracy = round(accuracy, 3) precision = Get_Precision_score(y_test, y_predict) precision = round(precision, 3) recall = Get_Recall(y_test, y_predict) recall = round(recall, 3) f1_score = Get_f1_score(y_test, y_predict) f1_score = round(f1_score, 3) mcc = MCC(y_test, y_predict) mcc = round(mcc, 3) print('\n') evaluate(y_test, y_predict) print("Precision = ", precision) print("Accuracy_Score = ", accuracy) print("F1-Score = ", f1_score) print("MCC = ", mcc)
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split import numpy as np from getData import getdata attributes, target = getdata() def sigmoid(inX): return 1.0 / (1 + np.exp(-inX)) def trainLogRegres(train_x, train_y, maxIter, alpha): numSamples, numFeatures = np.shape(train_x) weights = np.ones((numFeatures, 1)) for k in range(maxIter): for i in range(numSamples): output = sigmoid(train_x[i, :] * weights) #print("output") #print(output) error = train_y[i, 0] - output print(weights) weights = weights + alpha * train_x[i, :].transpose() * error return weights def testLogRegres(weights, test_x, test_y): numSamples, numFeatures = np.shape(test_x) matchCount = 0 for i in range(numSamples): predict = sigmoid(test_x[i, :] * weights)[0, 0] > 0.5
def IFS(address, importence_rank): x_train, y_train, x_test, y_test = getdata(address) for i in range(len(importence_rank)): y_test = [] y_train = [] flag = importence_rank[i] fileList = os.listdir(address + "\\train\positive") flag_train = 0 for name in fileList: x_train[flag_train].append( np.load(address + "\\train\positive\\" + name)[flag]) y_train.append(1) flag_train += 1 fileList = os.listdir(address + "\\train\\negitive") for name in fileList: x_train[flag_train].append( np.load(address + "\\train\\negitive\\" + name)[flag]) y_train.append(0) flag_train += 1 fileList = os.listdir(address + "\\test\positive") flag_test = 0 for name in fileList: x_test[flag_test].append( np.load(address + "\\test\positive\\" + name)[flag]) y_test.append(1) flag_test += 1 fileList = os.listdir(address + "\\test\\negitive") for name in fileList: x_test[flag_test].append( np.load(address + "\\test\\negitive\\" + name)[flag]) flag_test += 1 y_test.append(0) kf = KFold(n_splits=10) kf.get_n_splits(x_train) accuracy_ten = 0 precision_ten = 0 recall_ten = 0 f1_score_ten = 0 mcc_ten = 0 sen_ten = 0 spe_ten = 0 x2_train, y2_train = shuffle(x_train, y_train, random_state=0) for train_index, test_index in kf.split(x_train): x1_train = [] y1_train = [] x1_test = [] y1_test = [] for i in range(len(train_index)): x1_train.append(x2_train[train_index[i]]) y1_train.append(y2_train[train_index[i]]) for i in range(len(test_index)): x1_test.append(x2_train[test_index[i]]) y1_test.append(y2_train[test_index[i]]) x1_train = np.array(x1_train) y1_train = np.array(y1_train) x1_test = np.array(x1_test) y1_test = np.array(y1_test) clf = XGBClassifier() clf.fit(x1_train, y1_train) y_predict = clf.predict(x1_test) accuracy = Get_Accuracy(y1_test, y_predict) accuracy_ten += accuracy precision = Get_Precision_score(y1_test, y_predict) precision_ten += precision recall = Get_Recall(y1_test, y_predict) recall_ten += recall f1_score = Get_f1_score(y1_test, y_predict) f1_score_ten += f1_score mcc = MCC(y1_test, y_predict) mcc_ten += mcc sen, spe = evaluate(y1_test, y_predict) sen_ten += sen spe_ten += spe accuracy = accuracy_ten / 10 precision = precision_ten / 10 f1_score = f1_score_ten / 10 mcc = mcc_ten / 10 spe = spe_ten / 10 sen = sen_ten / 10 accuracy = round(accuracy, 3) precision = round(precision, 3) f1_score = round(f1_score, 3) spe = round(spe, 3) sen = round(sen, 3) mcc = round(mcc, 3) acc_list.append(accuracy) pre_list.append(precision) f1_list.append(f1_score) mcc_list.append(mcc) sen_list.append(sen) spe_list.append(spe)
def calculate(trustFactor, startDate, endDate, predictionObjectID, predictionObject, *providers): #Get realized data realizedData = json.loads( getData.getproductiondata(startDate, endDate, predictionObjectID)) #Create empty arrays selectedProviders = [None] * len(providers) accuracyRates = [None] * len(providers) i = 0 #take date from providers for provider in providers: selectedProviders[i] = json.loads( getData.getdata(startDate, endDate, provider, predictionObject)) accuracyRates[i] = 100 / len(providers) i = i + 1 for selectedProvider in selectedProviders: #check if data lengths are equal if len(realizedData["data"]) > len(selectedProvider["data"]): print("Data lenghts are not equal between realized product and ", selectedProvider["data"][0]["ShortName"]) print("Trying to syncronize...") #compare with date-hour and if they are not equal this means there are missing prediction.So fill that missed production with worst prediction for k in range(0, len(realizedData["data"])): realizedDate = realizedData["data"][k]["dateText"] realizedTime = realizedData["data"][k]["timeText"] providerDate = selectedProvider["data"][k]["Instrument"] providerTime = providerDate[11:16] providerDate = providerDate[0:10] if realizedDate == providerDate and realizedTime == providerTime: continue else: print("One of the missed index on provider is : ", k) print("Syncronizing...") reserved = selectedProvider error = 0 temp = "" for provider in selectedProviders: if provider == reserved: continue else: dif = abs(realizedData["data"][k]["total"] - provider["data"][k]["Forecast"]) if dif > error: error = dif temp = provider instrument = realizedDate + "T" + realizedTime + ":00.000Z" forecast = temp["data"][k]["Forecast"] shortname = selectedProvider["data"][0]["ShortName"] name = selectedProvider["data"][0]["Name"] selectedProvider["data"].insert( k, { 'Name': name, 'ShortName': shortname, 'Forecast': forecast, 'Instrument': instrument }) #check if is there any missed value in realized production.If yes,delete that hour from all the providers. for t in range(0, len(realizedData["data"]) - 1): currentTime = realizedData["data"][t]["timeText"][:2] nextTime = realizedData["data"][t + 1]["timeText"][:2] if (int(currentTime) + 1) == int(nextTime) or (int(currentTime) == 23 and (int(nextTime)) == 00): continue else: print( "There is a missed index in realized data.This index will be deleted: ", t + 1) for selectedProvider in selectedProviders: del selectedProvider["data"][t + 1] #For every provider calculate the most and least trusting provider. #This is ranking providers by accuracy rate for every production between providers for index in range(0, len(realizedData["data"])): realizedDate = realizedData["data"][index]["dateText"] realizedTime = realizedData["data"][index]["timeText"] providerDate = selectedProvider["data"][index]["Instrument"] providerTime = providerDate[11:16] providerDate = providerDate[0:10] if realizedDate == providerDate and realizedTime == providerTime: i = 0 totalLapse = 0 deltaProviders = [None] * len(providers) for selectedProvider in selectedProviders: deltaProviders[i] = abs( realizedData["data"][index]["total"] - selectedProvider["data"][index]["Forecast"]) totalLapse = totalLapse + deltaProviders[i] i = i + 1 i = 0 deltaPercentageProviders = [None] * len(providers) for selectedProvider in selectedProviders: deltaPercentageProviders[i] = (100 * deltaProviders[i]) / totalLapse i = i + 1 i = 0 averageLapsePercentage = 100 / len(providers) for selectedProvider in selectedProviders: accuracyRates[i] = accuracyRates[i] + ( averageLapsePercentage - deltaPercentageProviders[i]) / float(trustFactor) i = i + 1 i = 0 for selectedProvider in selectedProviders: if accuracyRates[i] > 95: deliveredRemain = abs(accuracyRates[i] - 95) accuracyRates[i] = accuracyRates[i] - deliveredRemain j = 0 for selectedProvider in selectedProviders: if j == i: j = j + 1 continue else: accuracyRates[j] = accuracyRates[ j] + deliveredRemain / (len(providers) - 1) j = j + 1 if accuracyRates[i] < 5: deliveredRemain = abs(accuracyRates[i] - 5) accuracyRates[i] = accuracyRates[i] + deliveredRemain j = 0 for selectedProvider in selectedProviders: if j == i: j = j + 1 continue else: accuracyRates[j] = accuracyRates[ j] - deliveredRemain / (len(providers) - 1) j = j + 1 i = i + 1 else: print("This index is not same with realized: ", index) print(providerTime + " " + providerDate) print(realizedTime + " " + providerDate) break return accuracyRates