def determineUserNN(self, filename): model = self.loadModel() temp = ["unknown"] tempdata, templabels, words = LetterBreaker.imageProcess( temp, 1, filename) userRankings, this_user = Prediction.predictUser( model, words, self.users) userRankingDict = {} for i in range(0, len(self.users)): userRankingDict[self.users[i]] = userRankings[i] userRankings.sort(reverse=True) return this_user, userRankings, userRankingDict
def AddOutlier(Phi, samp_y, name, poly_x,samp_x): for i in range(len(samp_y)): if (i%10) == 0: samp_y[i] = samp_y[i] + random.uniform(0,100) theta_LS = ls.LS(Phi, samp_y) # LSR theta_RLS = ls.RLS(Phi, samp_y, 0.5) # RLSR lambda = 2.0 theta_LASSO = ls.LASSO(Phi, samp_y, 0.2) # LASSO lambda = 2.0 theta_RR = ls.RR(Phi, samp_y, 5) # RR It seems wrong, but dont know why... mu_estimator, var_estimator = ls.BR(Phi, samp_y,2.5, 5) # BR alpha = 0.5, variance = 5 theta = [theta_LS, theta_RLS, theta_LASSO, theta_RR] result_y = [] for i in range(0, 4): pred_y = Prediction.predict(poly_x=poly_x, theta=theta[i], poly_para_K=K) # plt.plot(poly_x,pred_y) result_y.append(np.ravel(np.array(pred_y))) prob_distribution, pred_mu = Prediction.pref_BR(mu_estimator, var_estimator, poly_para_K=K, poly_x=poly_x) result_y.append((np.ravel(np.array(pred_mu)))) drawRegression(result_y,poly_x, theta,name, samp_x=samp_x, samp_y=samp_y,pred_mu=pred_mu) print(MSE(poly_y, result_y)) # AddOutlier(Phi, samp_y, name, poly_x,samp_x)
def forcasting(): try: repo = request.args.get('repo') data = Prediction.forcasting(repo) return data # return jsonify(success = False) except Exception as e: print(e) return jsonify(success=False)
def RunEval(InputFile): Energy=0 # launch the RNaeval command #conf = loadConfig() energiesFile = "energyvalues.txt" os.system('RNAeval -d2 <' + InputFile + '>' + energiesFile) # -v if we need all loops energy values # Parse the RNAevaloutput to extract energy values lines = PR.Parsefile(energiesFile) #print lines for i in xrange(1, len(lines), 2): # i is the stucture number and 'lines[i].split(" ")[1][1:-2]' is the corresponding energy value Energy= (lines[i].split(" ")[1][1:-2]) return Energy
def main(): # Process data filename = "Data/pima-indians-diabetes.data.csv" dataset = ProcessData.loadCsv(filename) # print("Loaded data file {0} with {1} rows".format(filename,len(dataset))) splitRatio = 0.67 train, test = ProcessData.splitDataset(dataset, splitRatio) # print("Split {0} rows into train with {1} and test with {2}".format(len(dataset),train,test)) #Get data feature separated = GetFeature.separateByClass(train) # print("Separated instances: {0}".format(separated)) summaries = GetFeature.summarizeByClass(train) # print("Summaries[0]: {0}".format(summaries[0]) +"\n" # + "Summaries[1] {0} ".format(summaries[1])) #Preciction predictions = Prediction.getPredictions(summaries, test) accuracy = Prediction.getAccuracy(test, predictions) print("Accuracy:{0}%".format(accuracy))
def DoEverything(message): fileID = message.photo[-1].file_id chatID = message.chat.id bot.send_message(chatID, 'Got a photo!') file_info = bot.get_file(fileID) downloaded_file = bot.download_file(file_info.file_path) with open("image.jpg", 'wb') as new_file: new_file.write(downloaded_file) prediction = Prediction.predicting("image.jpg") print("Prediction: ", prediction) flag = 1 if prediction: flag = ItPredictedNumplate(chatID) if prediction != flag: DoingYOLOThings(chatID)
def doContPopEvaluation(self): noMatch = 0 # How often does the population fail to have a classifier that matches an instance in the data. accuracyEstimateSum = 0 self.env.resetDataRef() # Go to the first instance in dataset instances = self.env.formatData.numTrainInstances # ---------------------------------------------------------------------------------------------- for inst in range(instances): state_phenotype = self.env.getTrainInstance() self.population.makeEvalMatchSet(state_phenotype.attributeList,self) prediction = Prediction(self, self.population) phenotypePrediction = prediction.getDecision() if phenotypePrediction == None: noMatch += 1 else: predictionError = math.fabs(float(phenotypePrediction) - float(state_phenotype.phenotype)) phenotypeRange = self.env.formatData.phenotypeList[1] - self.env.formatData.phenotypeList[0] accuracyEstimateSum += 1.0 - (predictionError / float(phenotypeRange)) self.env.newInstance() # next instance self.population.clearSets() # Accuracy Estimate if instances == noMatch: accuracyEstimate = 0 else: accuracyEstimate = accuracyEstimateSum / float(instances - noMatch) # Adjustment for uncovered instances - to avoid positive or negative bias we incorporate the probability of guessing a phenotype by chance (e.g. 50% if two phenotypes) instanceCoverage = 1.0 - (float(noMatch) / float(instances)) adjustedAccuracyEstimate = accuracyEstimateSum / float(instances) resultList = np.array([adjustedAccuracyEstimate, instanceCoverage]) return resultList
def browse_button(self): # Allow user to select a directory and store it in global var # called folder_path global folder_path filename = filedialog.askopenfilename() pd = Prediction() img_PIL, img_tensor = pd.get_img_tensor(filename) sentence = pd.get_caption(img_tensor) pd.visualize(img_PIL, sentence)
def setup(): ############################################################################### #import Data import Prediction import warnings import Control warnings.filterwarnings("ignore") Prediction_horizon = 60 # in minutes #Data.data_acquisition() DATA_LIST={} wb = openpyxl.load_workbook('DATA_LIST.xlsx') sheet = wb.get_sheet_by_name('Sheet1') for key in range(1, sheet.max_column+1): DATA_LIST[sheet.cell(row=1, column=key).value]=[] for v in range(2, sheet.max_row+1): DATA_LIST[sheet.cell(row=1, column=key).value].append(sheet.cell(row=v, column=key).value) # Model generation #SVR_model = Prediction.Support_Vector_Regression(DATA_LIST, Prediction_horizon) KNN_model = Prediction.kNN_Regression(DATA_LIST, Prediction_horizon) BRR_model = Prediction.Bayesian_Ridge_Regression(DATA_LIST, Prediction_horizon) # workbook = xlsxwriter.Workbook(os.path.dirname(os.path.abspath(__file__))+'\Control.xlsx') # workbook.add_worksheet() # workbook.close() alpha = 0.7 # Higher alpha means slower adaptation T_history={} try: for i in range(1,8): T_history[str(i)] = Control.max_T_history(Control.previous_date(i)) Mean_Running_Average = (1.0 - alpha) * sum( (alpha** (i-1) ) * int(T_history [str(i)]) for i in range(1,8) ) except Exception: Mean_Running_Average = 20 return DATA_LIST, KNN_model, BRR_model, Mean_Running_Average
def main(): '''main function for generating portfolio''' pp = [] choices = [] goodAlphas = [ 'alpha083', 'alpha101', 'alpha024', 'alpha042', 'alpha028', 'alpha025', 'alpha018', 'alpha010', 'alpha047', 'alpha033', 'alpha009', 'alpha005', 'alpha051' ] dist = YFI(0) X, Y = Process.ProcessData(dist, 2) #alphaIndex = Alphas.SingleAlpha(X, Y, 13) for i in range(5): result = Prediction.AvgedPredict(dist, X, Y, goodAlphas[:13], 30, 8) print(result) return result
def predict_BR(mu_estimator, var_estimator, poly_x): poly_x = poly_x.T predict_y = [] mu = [] for i in range(len(poly_x)): mu_star = np.dot(poly_x[i], mu_estimator) mu.append(mu_star) # print(mu_star) # temp = poly_x[i].reshape(poly_x[i].shape[0],1) # print(poly_x[i].shape) var_star = np.dot(np.dot(poly_x[i], var_estimator), poly_x[i].T) # var_star = np.dot(np.dot(temp.T, var_estimator),temp) # print(var_star) y = Prediction.gaussian(mu_star, var_star, (poly_x[i][1])) # y = gaussian(mu_estimator, var_star, mu_star) # print(poly_x[i][1]) # print(y) predict_y.append(y) # print(np.ravel(np.array(predict_y))) return np.ravel(np.array(predict_y)), np.ravel(np.array(mu))
def run(mylist3): mypre = [] mypre2 = [] for i in range(20): p = Prediction.predict(mylist3) mypre2.append(p) if p not in mypre: mypre.append(p) l = len(mypre) mypre = [' '.join(item) for item in mypre] mypredict = [] for i in range(l): disease = mypre[i].replace('Â', '') disease = disease.replace('Ã', '') disease = disease.replace('\xa0', ' ') disease = disease.replace('\x82', '') mypredict.append(disease) return mypredict
def load_and_predict(): app = wx.App(None) style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST dialog = wx.FileDialog(None, 'Open', style=style) if dialog.ShowModal() == wx.ID_OK: filepath = dialog.GetPath() else: filepath = None dialog.Destroy() img = Image.open(filepath) width, height = img.size scale = width / height if scale > 0: height = CANVAS_SIZE / scale else: height = CANVAS_SIZE * scale img = ImageTk.PhotoImage(img.resize((CANVAS_SIZE, round(height)))) canv.create_image(CANVAS_SIZE/2, CANVAS_SIZE/2, image=img) if round(pr.predict(filepath)) == 1: messagebox.showinfo("Prediction", "This is a dog.") else: messagebox.showinfo("Prediction", "This is a cat.")
def predict_course(self, student_id, course_id, depth): """Predict course for the student by calculating all paths and ajdusting horting values.""" predictions = [] if ALGORITHM == ALG_DIJKSTRA: all_paths = self.build_all_paths_dijkstra(student_id) else: all_paths = self.build_all_paths(student_id) for path_len, paths in all_paths.iteritems(): for path in paths: if course_id in self.student_index[path[path_len - 1]].courses: p = Prediction.Prediction(student_id, course_id, path) hort_val = 1.0 for i in xrange(path_len - 1): #apply modifier if available modifier = self.student_index[ student_id].mod_horts.get(path[i + 1], 1.0) hort_val *= self.student_index[path[i]].horts[path[ i + 1]] * modifier p.horting_sum = hort_val predictions.append(p) return predictions
def post_data(request): if(request.method=='POST'): data = json.loads(request.body) parser = ps.Parser() print data['transportation'] transportation = parser.encode_transportation(data['transportation']) season = parser.encode_season(data['season']) lat = data['latitude'] lng = data['longitude'] duration = data['duration'] group = data['group'] holidayType = parser.encode_holidayType(data['holidayType']) # Get List attrib = [transportation,season,lat,lng,duration,group,holidayType] prediction = predict.Prediction() travelCase = prediction.predict_closest_case(transportation,season,lat,lng,duration,group,holidayType) travelCase.decoding() #request.POST = QueryDict(data) return HttpResponse(json.dumps(travelCase.__dict__), content_type = "application/json") else: return render(request,'home/show_view.html',{})
def save_image(image, logger=None): """Resizes and stores the acquired image as a PNG file. The image is store ind DATA_PATH/LABEL as train_image-hhmmssff.png where hh is the current time's hour, mm is the current time's minute, ss is the current time's second and ff the current time's millisecond. Args: image: Expects a 3D np.ndarray with dtype=np.uint8 """ # Expects a np.ndarray with dtype=np.uint8 image_data = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT)) ## Save image file in directory timestamp = datetime.now() path = os.path.join( DATA_PATH, LABEL, "train_image-" + timestamp.strftime("%H%M%S%f") + ".png") triggered = False while (not triggered): log.debug("Reading Coils") response = client.read_coils(1, 1, unit=UNIT) log.debug(response) triggered = response.bits[0] time.sleep(0.5) cv2.imwrite(path, image_data) #cv2.imwrite(path, image) cv2.waitKey(1) # Pass auf mich auf! log.debug("Picture taken and saved") #Prediction.show(image_data) #Prediction.predict(image_data) True_count, False_count, Entscheidungswert = Prediction.predict(image_data) print(Entscheidungswert) log.debug("Write to a Coil and read back") rq = client.write_coil(0, Entscheidungswert, unit=UNIT) time.sleep(1)
def complete_function(PhotoToAnalize): estat_estanteries = [[],[],[],[],[]] PhotoSplitter.get_product_slots(PhotoToAnalize) c = [] for file in os.listdir("product_slots"): c.append(file) c.sort() m = analyze_image(PhotoToAnalize) x = 0 f = 0 for i in m: for j in i: if j == 1: estat_estanteries[f].append("X") else: estat_estanteries[f].append(Prediction.prediccio("product_slots/%s" % c[x])) x += 1 f +=1 for file in os.listdir("product_slots"): os.remove("product_slots/%s" % file) return(estat_estanteries)
def GetPre(ks_dfs_old, warning_time, pre_time, pre_length): # 预测 pre_update_dic = {} pre_insert_dic = {} no_pre_depart = [] ks_dfs_old = CLASS.FillIndex(ks_dfs_old, '1/1/2015', 36) pre_Index = pd.date_range(pre_time, periods=pre_length, freq='MS', name='date') Insert_time = arrow.get(pre_time).shift(months=5).format("YYYY-MM-DD") Update_time = arrow.get(pre_time).shift(months=4).format("YYYY-MM-DD") for k in ks_dfs_old: df_k = ks_dfs_old[k] df_for_pre = df_k[:warning_time] history_data_len = len(df_for_pre) df_pre = df_for_pre.tail(history_data_len - history_data_len % 12) for_pre = np.array(df_pre) # p = testStationarity(for_pre.flatten())[1] holt = TP.Holtwinters(for_pre) data_evaluation = holt.Evaluating(for_pre)[0][0] # print(k,data_evaluation) if data_evaluation > 0.3: data_preall = holt.multiplicative( 0, history_data_len - history_data_len % 12, cycle, pre_length) data_pre = data_preall[0] # 乘法模型预测 data_pre = pd.DataFrame(data_pre, index=pre_Index) pre_insert = data_pre[0][data_pre[0].index == Insert_time] pre_update = data_pre[0][:Update_time] #data_pre['d'] = data_pre[0].index() pre_insert_dic[k] = pre_insert pre_update_dic[k] = pre_update else: no_pre_depart.append(k) return pre_insert_dic, pre_update_dic
# Explorem la relació de les columnes amb la variable Survived # DataExploration.show_survive_relation_by_feature(train_ds, 'Sex') # DataExploration.show_survive_relation_by_feature(train_ds, 'Age') # DataExploration.show_scatter_plot_by_features(train_ds, 'Fare', 'Age') combined_ds = DataImport.combine_datasets(train_ds, test_ds) # Fem una exploració de les dades per a veure si tenim valors nulls DataExploration.explore_dataset(combined_ds) combined_ds = DataExploration.get_titles(combined_ds) # Netejem les dades dataCleaningObj = DataCleaning.DataCleaning(combined_ds) combined_ds = dataCleaningObj.clean() # Test Anderson DataExploration.anderson_darling_test(combined_ds['Age']) DataExploration.anderson_darling_test(combined_ds['Fare']) DataExploration.anderson_darling_test(combined_ds['Sex']) DataExploration.anderson_darling_test(combined_ds['Family']) # Test Fligner DataExploration.fligner_test(combined_ds['Fare'], combined_ds['Pclass']) combined_ds.drop('Pclass', axis=1, inplace=True) combined_ds.to_csv('data/cleaned_processed_data.csv', index=False) # Create and score model Prediction.score_model(combined_ds)
def runIteration(self,state_phenotype,exploreIter): print("ITERATION:"+str(self.explorIter)) print("Data Instance:" ,end=" ") for i in range(state_phenotype.attributeList.size): print(state_phenotype.attributeList[i].value,end=" ") print(" w/ Phenotype: ",state_phenotype.phenotype) print("Population Set Size: "+str(self.population.popSet.size)) #Form [M] self.population.makeMatchSet(state_phenotype,exploreIter,self) #Print [M] self.printMatchSet() #Make a Prediction self.timer.startTimeEvaluation() prediction = Prediction(self,self.population) phenotypePrediction = prediction.getDecision() if phenotypePrediction == None or phenotypePrediction == 'Tie': if self.env.formatData.discretePhenotype: phenotypePrediction = random.choice(self.env.formatData.phenotypeList) else: phenotypePrediction = random.randrange(self.env.formatData.phenotypeList[0],self.env.formatData.phenotypeList[1],(self.env.formatData.phenotypeList[1]-self.env.formatData.phenotypeList[0])/float(1000)) else: if self.env.formatData.discretePhenotype: if phenotypePrediction == state_phenotype.phenotype: self.correct[exploreIter%self.trackingFrequency] = 1 else: self.correct[exploreIter%self.trackingFrequency] = 0 else: predictionError = math.fabs(phenotypePrediction-float(state_phenotype.phenotype)) phenotypeRange = self.env.formatData.phenotypeList[1] - self.env.formatData.phenotypeList[0] accuracyEstimate = 1.0 - (predictionError / float(phenotypeRange)) self.correct[exploreIter%self.trackingFrequency] = accuracyEstimate self.timer.stopTimeEvaluation() #Form [C] self.population.makeCorrectSet(self,state_phenotype.phenotype) #Print [C] self.printCorrectSet() #Update Parameters self.population.updateSets(self,exploreIter) #Perform Subsumption if self.doSubsumption: self.timer.startTimeSubsumption() self.population.doCorrectSetSubsumption(self) self.timer.stopTimeSubsumption() #Perform GA self.population.runGA(self,exploreIter,state_phenotype.attributeList,state_phenotype.phenotype) #Run Deletion self.population.deletion(self,exploreIter) #Clear [M] and [C] self.population.clearSets() print("________________________________________")
for x in range(0,len(a)): a[x] = (a[x] / max) percentChange(coal) percentChange(natural_gas) percentChange(distillate) percentChange(total) percentChange(jet_fuel) percentChange(sp) ab, =plt.plot(coal,'black', label="Coal") ac, =plt.plot(natural_gas,'gray', label="Natural Gas") ad, =plt.plot(total,'blue', label="Total Energy") ae, =plt.plot(distillate,'brown', label="Distillate Fuel Oil") ag, =plt.plot(jet_fuel,'pink', label="Jet_Fuel") ak, =plt.plot(sp,'orange', label="S&P 500") az, =plt.plot(range(533, 654), Prediction.predict(total), 'cyan', label="Predicted Total") # pc, =plt.plot(range(533, 654), Prediction.predict(coal), label="Predicted Coal") sk, =plt.plot(range(538, 659), Prediction.predict(sp), 'yellow', label="Predicted S&P 500") plt.axes().set_xlabel("Months since 1973") plt.axes().set_ylabel("Percent change") plt.title("Predicting C02 Emmisions/S&P 500") plt.gcf().canvas.set_window_title('Energy and Economy Predictions') plt.legend(handles=[ab, ac, ae, ag, ad, ak, az, sk]) callback = Index() axnext = plt.axes([0.7718, 0.115, 0.125, 0.075]) bnext = Button(axnext,'Conclusion') bnext.on_clicked(callback.next) plt.show()
def _Go(): global flag global num_days global length global steps global num_days global f global sa global plots global curr_pos sel_task = task.get() if (data_path == None): display_error(1) elif (sel_task == 'None'): display_error(2) elif (sel_task == 'Summarize'): curr_pos = 0 plots = [] name = str(stock.get()) if name != "None": lot = length.get() if lot == "": display_error(3) else: lot = int(lot) if type(lot) != int: display_error(4) elif lot <= 0: display_error(5) if lot != "" and type(int(lot)) == int and int(lot) > 0: data = LoadTimeseries.read_p_timeseries(data_path, name, lot) original, summarized = Summarize.summarize(data) plt.clf() #yearsFmt = mdates.DateFormatter('%b %Y') f = plt.figure(figsize=(6, 6), dpi=100, facecolor='white') plt.subplot(411) plt.title("Initial Timeserie") original[name].ix[original[name].index].plot(style='b') ## print(summarized[name]["extremas-x"]) ## tps = [] ## tps_ind = [] ## for i in range(len(original[name])): ## if i in summarized[name]["extremas-x"]: ## tps.append(original[name][i]) ## tps_ind.append(original[name].index[i]) ## print(tps_ind) ## print(tps) ## f = Series(tps, index=tps_ind).ix[tps_ind].plot(style='r',marker='o', linestyle="", markersize=7) plt.subplot(412) plt.title("Overall Timeserie Trend") plt.plot(summarized[name]["trend"]["x"], summarized[name]["trend"]["r"], 'g') plt.xlim([0, lot]) plt.subplot(413) plt.title("Turning Points") plt.plot(summarized[name]["extremas-x"], summarized[name]["extremas"], 'o', mfc='none', markersize=7) plt.xlim([0, lot]) plt.subplot(414) plt.title("Seasonality and Cycle") plt.plot(summarized[name]["Ds-x"], summarized[name]["Ds"], 'r') plt.xlim([0, lot]) plt.tight_layout() canvas.figure = f canvas.draw() else: display_error(6) elif sel_task == 'Clustering': n = num.get() lot = length.get() if n == "": display_error(7) else: n = int(n) if type(n) != int: display_error(8) elif n <= 0: display_error(9) if lot == "": display_error(10) else: lot = int(lot) if type(lot) != int: display_error(11) elif lot <= 0: display_error(12) if type(lot) == int and type(n) == int and lot > 0 and n > 0: curr_pos = 0 plots = [] data, aDates = LoadTimeseries.read_c_timeseries(data_path, n, lot) original, summarized = Summarize4Clustering.summarize( data, aDates, lot) plt.clf() curr_pos = 0 plots = Clustering.cluster(original, summarized, n) canvas.figure = plots[0] canvas.draw() elif sel_task == 'Prediction': curr_pos = 0 plots = [] name = str(stock.get()) if name != "None": lot = length.get() sp = steps.get() if sp == "": display_error(13) else: sp = int(sp) if type(sp) != int: display_error(14) elif sp <= 0: display_error(15) if lot != "": lot = int(lot) if type(lot) != int: display_error(11) elif lot <= 0: display_error(12) if lot + sp > num_days: display_error(16) if type(lot) == int and lot > 0 and type( sp) == int and sp > 0 and lot + sp <= num_days: data = LoadTimeseries.read_p_timeseries( data_path, name, lot + sp) original, summarized = Summarize4Prediction.summarize(data, sp) orig_pre, orig_r_pre, sum_pre = Prediction.predict( original, summarized[name]["Ds"], summarized[name]["AL"], sp) plt.clf() # print(original) f = plt.figure(figsize=(6, 6), dpi=100, facecolor='white') original[name].ix[original[name].index[0]:].plot( style='b', label="Actual") orig_pre.ix[orig_pre.index[0]:].plot(style='r', label="ARMA") orig_r_pre.ix[orig_r_pre.index[0]:].plot(style='purple', label="r-ARMA") sum_pre.ix[orig_pre.index[0]:].plot(style='g', label="Summarized") plt.legend(loc=2, prop={'size': 10}) canvas.figure = f canvas.draw() else: display_error(3)
Phi = ls.generate_Phi( K ,samp_x) # 每列是一个sample 每个列中的行是feature (10*50) # print(Phi.shape) # Obtain parameters theta_LS = ls.LS(Phi,samp_y) # LSR theta_RLS = ls.RLS(Phi,samp_y,0.5) # RLSR theta_LASSO = ls.LASSO(Phi,samp_y,0.2) # LASSO theta_RR = ls.RR(Phi, samp_y, K) # RR mu_estimator, var_estimator = ls.BR(Phi,samp_y,2.5,5) #BR variance = 5 # print(var_estimator) result_y = [] name = ['LS','RLS','LASSO','RR','BR'] theta = [theta_LS, theta_RLS, theta_LASSO, theta_RR] for i in range(0,4): pred_y = Prediction.predict(poly_x=poly_x, theta=theta[i], poly_para_K=K) # plt.plot(poly_x,pred_y) result_y.append(np.ravel(np.array(pred_y))) prob_distribution, pred_mu= Prediction.pref_BR(mu_estimator, var_estimator,poly_para_K = K, poly_x=poly_x) result_y.append((np.ravel(np.array(pred_mu)))) #result_y is a list, which has 5 sub-list, each sub-list has 100 predicted value of poly_y # print((result_y[1])) def drawRegression(result_y,poly_x,theta,name, samp_x, samp_y,pred_mu): #Draw plot, result_y is the result list, poly_x is orginal x, theta are parameter lists for i in range(0,len(name)): plt.plot(poly_x, result_y[i]) plt.scatter(samp_x,samp_y,edgecolors='k',color='k',marker='x',s=15) # Draw standard deviation std_dev = np.std((np.ravel(np.array(pred_mu)))) meanAddstd_dev = [i + std_dev for i in pred_mu]
date = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') warning = 0 # Real time data try: [T, co2, set_point, hum, T_outdoor, Human_date, Season, Cal_data, vent_power, H_C_power, cool_power, hour]=Data.Real_Time_Data(client) except Exception, e: print 'WARNING : PROBLEM DETECTED 1' print e warning = 3 [T, co2, set_point, T_outdoor, Cal_data, H_C_power, hour] = [23,401,23,23,2,-12,12] #occupancy prdiction try: state, Human_power, number = Prediction.occupancy(state, Cal_data, hour, co2) except Exception, e: print 'WARNING : PROBLEM DETECTED 2' print e warning = 2 state='occupied' Human_power=3.0 number=30 # Temperature prediction T_needed_data=[T , H_C_power, set_point, T_outdoor, Cal_data, Human_power] try: T_predicted, warning= Prediction.T_prediciton(d, T_needed_data , model) except Exception, e: print 'WARNING : PROBLEM DETECTED 3' print e
def flop(self): self.phase = 3 self.community += self.deck.deal(3) Prediction.predictRound(self) Display.displayGameState(self)
def river(self): self.phase = 5 self.community += self.deck.deal(1) Prediction.predictRound(self) Display.displayGameState(self)
test = pd.read_csv( '/Users/carrillo/workspace/Kaggle/resources/AfSIS/testDecaySubtracted.csv', header=0) train.replace(to_replace='Topsoil', value=0, inplace=True) train.replace(to_replace='Subsoil', value=1, inplace=True) trainForCalibration = train.drop(['Ca', 'P', 'pH', 'SOC', 'Sand'], 1) test.replace(to_replace='Topsoil', value=0, inplace=True) test.replace(to_replace='Subsoil', value=1, inplace=True) # Set up the individual regressors ca_est = svm.SVR(kernel='poly', C=5000.0, degree=2, gamma=0.0009) # Control SVR for Ca ca_featureSelection = None ca_predict = Prediction.Prediction(train, test, ca_est, ca_featureSelection, 'Ca') ca_predict.learn() ca_prediction = ca_predict.predict() # Predict P p_est = svm.SVR(C=10000) # Control SVR for P p_featureSelection = None p_predict = Prediction.Prediction(train, test, p_est, p_featureSelection, 'P') p_predict.learn() p_prediction = p_predict.predict() # Predict pH pH_est = svm.SVR(kernel='rbf', C=6100.0, degree=1, gamma=0.000875) # Control SVR for pH
data = pd.read_csv(fileName, header=0) data.replace(to_replace='Topsoil', value=0, inplace=True) data.replace(to_replace='Subsoil', value=1, inplace=True) return data # Set up the individual regressors ca_train = getData( '/Users/carrillo/workspace/Kaggle/resources/AfSIS/trainingDecaySubtracted1500Pc_matchedWithTestSet.csv' ) ca_test = getData( '/Users/carrillo/workspace/Kaggle/resources/AfSIS/testDecaySubtracted1500Pc_DepthNumeric.csv' ) ca_est = svm.SVR(C=10000.0) # Control SVR for Ca ca_featureSelection = None ca_predict = Prediction.Prediction(ca_train, ca_test, ca_est, ca_featureSelection, 'Ca') ca_predict.learn() ca_prediction = ca_predict.predict() # Predict P p_train = getData( '/Users/carrillo/workspace/Kaggle/resources/AfSIS/trainingDecaySubtracted500Pc_matchedWithTestSet.csv' ) p_test = getData( '/Users/carrillo/workspace/Kaggle/resources/AfSIS/testDecaySubtracted500Pc_DepthNumeric.csv' ) p_est = svm.SVR(C=10000.0) # Control SVR for P p_featureSelection = None p_predict = Prediction.Prediction(p_train, p_test, p_est, p_featureSelection, 'P')
'/Users/carrillo/workspace/Kaggle/resources/AfSIS/trainingDecaySubtracted.csv', header=0) test = pd.read_csv( '/Users/carrillo/workspace/Kaggle/resources/AfSIS/testDecaySubtracted.csv', header=0) train.replace(to_replace='Topsoil', value=0, inplace=True) train.replace(to_replace='Subsoil', value=1, inplace=True) test.replace(to_replace='Topsoil', value=0, inplace=True) test.replace(to_replace='Subsoil', value=1, inplace=True) # Set up the individual regressors ca_est = linear_model.Ridge(alpha=0.06) # Control SVR for Ca ca_featureSelection = None ca_predict = Prediction.Prediction(train, test, ca_est, ca_featureSelection, 'Ca') ca_predict.learn() ca_prediction = ca_predict.predict() # Predict P p_est = linear_model.Ridge(alpha=0.05) # Control SVR for P p_featureSelection = None p_predict = Prediction.Prediction(train, test, ca_est, ca_featureSelection, 'P') p_predict.learn() p_prediction = p_predict.predict() # Predict pH pH_est = linear_model.Ridge(alpha=0.0015) # Control SVR for pH
# ############################################################################# # Generate sample data centers = [[1, 1], [-1, -1], [1, -1]] X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4, random_state=0) X = StandardScaler().fit_transform(X) return X # ############################################################################# fileList = [] root_folder = sys.argv[1] Prediction.getFileList(root_folder, fileList) destinations = list(map(load.getDestination, fileList)) destinations = pd.concat(destinations) X = destinations.as_matrix(columns=destinations.columns[1:]) # ############################################################################# # Compute DBSCAN db = DBSCAN(eps=0.005, min_samples=2).fit(X) core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True labels = db.labels_ # Number of clusters in labels, ignoring noise if present. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
def _Go(): global flag;global num_days;global length;global steps; global num_days;global f;global sa;global plots;global curr_pos sel_task = task.get() if (data_path == None): display_error(1) elif (sel_task == 'None'): display_error(2) elif (sel_task == 'Summarize'): curr_pos = 0 plots = [] name = str(stock.get()) if name != "None": lot = length.get() if lot=="": display_error(3) else: lot = int(lot) if type(lot) != int: display_error(4) elif lot <= 0: display_error(5) if lot != "" and type(int(lot)) == int and int(lot) > 0: data = LoadTimeseries.read_p_timeseries(data_path, name, lot) original, summarized = Summarize.summarize(data) plt.clf() #yearsFmt = mdates.DateFormatter('%b %Y') f = plt.figure(figsize=(6,6), dpi=100, facecolor='white') plt.subplot(411) plt.title("Initial Timeserie") original[name].ix[original[name].index].plot(style='b') ## print(summarized[name]["extremas-x"]) ## tps = [] ## tps_ind = [] ## for i in range(len(original[name])): ## if i in summarized[name]["extremas-x"]: ## tps.append(original[name][i]) ## tps_ind.append(original[name].index[i]) ## print(tps_ind) ## print(tps) ## f = Series(tps, index=tps_ind).ix[tps_ind].plot(style='r',marker='o', linestyle="", markersize=7) plt.subplot(412) plt.title("Overall Timeserie Trend") plt.plot(summarized[name]["trend"]["x"],summarized[name]["trend"]["r"],'g') plt.xlim([0,lot]) plt.subplot(413) plt.title("Turning Points") plt.plot(summarized[name]["extremas-x"], summarized[name]["extremas"], 'o', mfc='none', markersize=7) plt.xlim([0,lot]) plt.subplot(414) plt.title("Seasonality and Cycle") plt.plot(summarized[name]["Ds-x"], summarized[name]["Ds"], 'r') plt.xlim([0,lot]) plt.tight_layout() canvas.figure = f canvas.draw() else: display_error(6) elif sel_task == 'Clustering': n = num.get() lot = length.get() if n == "": display_error(7) else: n = int(n) if type(n) != int: display_error(8) elif n <= 0: display_error(9) if lot == "": display_error(10) else: lot = int(lot) if type(lot) != int: display_error(11) elif lot <= 0: display_error(12) if type(lot) == int and type(n) == int and lot > 0 and n > 0: curr_pos = 0 plots = [] data, aDates = LoadTimeseries.read_c_timeseries(data_path, n, lot) original, summarized = Summarize4Clustering.summarize(data, aDates, lot) plt.clf() curr_pos = 0 plots = Clustering.cluster(original, summarized, n) canvas.figure = plots[0] canvas.draw() elif sel_task == 'Prediction': curr_pos = 0 plots = [] name = str(stock.get()) if name != "None": lot = length.get() sp = steps.get() if sp == "": display_error(13) else: sp = int(sp) if type(sp) != int: display_error(14) elif sp <= 0: display_error(15) if lot != "": lot = int(lot) if type(lot) != int: display_error(11) elif lot <= 0: display_error(12) if lot+sp > num_days: display_error(16) if type(lot) == int and lot > 0 and type(sp) == int and sp > 0 and lot+sp<=num_days: data = LoadTimeseries.read_p_timeseries(data_path, name, lot+sp) original, summarized = Summarize4Prediction.summarize(data,sp) orig_pre, orig_r_pre, sum_pre = Prediction.predict(original, summarized[name]["Ds"], summarized[name]["AL"], sp) plt.clf() # print(original) f = plt.figure(figsize=(6,6), dpi=100, facecolor='white') original[name].ix[original[name].index[0]:].plot(style='b', label="Actual") orig_pre.ix[orig_pre.index[0]:].plot(style='r',label="ARMA") orig_r_pre.ix[orig_r_pre.index[0]:].plot(style='purple',label="r-ARMA") sum_pre.ix[orig_pre.index[0]:].plot(style='g', label="Summarized") plt.legend(loc=2,prop={'size':10}) canvas.figure = f canvas.draw() else: display_error(3)
def doPopEvaluation(self): noMatch = 0 # How often does the population fail to have a classifier that matches an instance in the data. tie = 0 # How often can the algorithm not make a decision between classes due to a tie. self.env.resetDataRef() # Go to the first instance in dataset phenotypeList = self.env.formatData.phenotypeList classAccDict = {} for each in phenotypeList: classAccDict[each] = ClassAccuracy() # ---------------------------------------------- instances = self.env.formatData.numTrainInstances # ---------------------------------------------------------------------------------------------- for inst in range(instances): state_phenotype = self.env.getTrainInstance() self.population.makeEvalMatchSet(state_phenotype.attributeList,self) prediction = Prediction(self,self.population) phenotypeSelection = prediction.getDecision() if phenotypeSelection == None: noMatch += 1 elif phenotypeSelection == 'Tie': tie += 1 else: # Instances which failed to be covered are excluded from the accuracy calculation for each in phenotypeList: thisIsMe = False accuratePhenotype = False truePhenotype = state_phenotype.phenotype if each == truePhenotype: thisIsMe = True if phenotypeSelection == truePhenotype: accuratePhenotype = True classAccDict[each].updateAccuracy(thisIsMe, accuratePhenotype) self.env.newInstance() # next instance self.population.clearSets() # Calculate Standard Accuracy-------------------------------------------- instancesCorrectlyClassified = classAccDict[phenotypeList[0]].T_myClass + classAccDict[phenotypeList[0]].T_otherClass instancesIncorrectlyClassified = classAccDict[phenotypeList[0]].F_myClass + classAccDict[phenotypeList[0]].F_otherClass standardAccuracy = float(instancesCorrectlyClassified) / float(instancesCorrectlyClassified + instancesIncorrectlyClassified) # Calculate Balanced Accuracy--------------------------------------------- T_mySum = 0 T_otherSum = 0 F_mySum = 0 F_otherSum = 0 for each in phenotypeList: T_mySum += classAccDict[each].T_myClass T_otherSum += classAccDict[each].T_otherClass F_mySum += classAccDict[each].F_myClass F_otherSum += classAccDict[each].F_otherClass balancedAccuracy = ((0.5 * T_mySum / (float(T_mySum + F_otherSum)) + 0.5 * T_otherSum / (float(T_otherSum + F_mySum)))) # BalancedAccuracy = (Specificity + Sensitivity)/2 # Adjustment for uncovered instances - to avoid positive or negative bias we incorporate the probability of guessing a phenotype by chance (e.g. 50% if two phenotypes) predictionFail = float(noMatch) / float(instances) predictionTies = float(tie) / float(instances) instanceCoverage = 1.0 - predictionFail predictionMade = 1.0 - (predictionFail + predictionTies) adjustedStandardAccuracy = (standardAccuracy * predictionMade) + ((1.0 - predictionMade) * (1.0 / float(len(phenotypeList)))) adjustedBalancedAccuracy = (balancedAccuracy * predictionMade) + ((1.0 - predictionMade) * (1.0 / float(len(phenotypeList)))) resultList = np.array([adjustedBalancedAccuracy,instanceCoverage]) return resultList
import pandas as pd import numpy as np import os import Prediction # Bokeh libraries from bokeh.io import output_notebook from bokeh.plotting import figure, show __file__ = 'script.py' url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQVtdpXMHB4g9h75a0jw8CsrqSuQmP5eMIB2adpKR5hkRggwMwzFy5kB-AIThodhVHNLxlZYm8fuoWj/pub?gid=2105854808&single=true&output=csv' target_bike = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Prediction", "data", "bike.csv") bike = Prediction.Load_data(url, target_bike).save_as_df(target_bike) bike.columns = [ 'Date', 'Heure', 'Grandtotal', 'Todaystotal', 'Unnamed', 'Remark' ] bike.drop(0, 0, inplace=True) bike.drop(1, 0, inplace=True) bike['Date'] = pd.to_datetime(bike['Date']) bike['Heure'].fillna(0, inplace=True) # Create a figure with a datetime type x-axis fig = figure(title='The number of TodaysTotal by date', plot_height=400, plot_width=700, x_axis_label='Day ', y_axis_label='Todaystotal', x_minor_ticks=2,