def TDUpdate(state, nextState, reward, w, eta=1e-1): features = util.extractFeatures(state) value = util.logisticValue(w, features) residual = reward - value if nextState is not None: nextFeatures = util.extractFeatures(nextState) residual += util.logisticValue(w, nextFeatures) gradient = value * (1 - value) * features newWeights = w + eta * residual * gradient return newWeights
def getValueRec(self, agent, game, card, depth, alpha, beta): otherAgent = int(not agent) gameClone = copy.deepcopy(game) gameClone.playCard(otherAgent, card) if gameClone.roundOver(): gameClone.endRound() if gameClone.gameOver() and gameClone.isWinner(self.playerNum): return 1 elif gameClone.gameOver() and gameClone.isLoser(self.playerNum): return 0 elif depth == 0: state = gameClone.getState(agent) features = util.extractFeatures(state) if agent == gameClone.attacker: weights = self.w_atk else: weights = self.w_def return util.logisticValue(weights, features) if otherAgent == self.playerNum: depth -= 1 if agent == gameClone.attacker: cards = gameClone.getAttackOptions(agent) else: cards = gameClone.getDefendOptions(agent) if agent == self.playerNum: v = float('-inf') for card in cards: v = max( v, self.getValueRec(otherAgent, gameClone, card, depth, alpha, beta)) if v >= beta: return v alpha = max(alpha, v) return v else: v = float('+inf') for card in cards: v = min( v, self.getValueRec(otherAgent, gameClone, card, depth, alpha, beta)) if v <= alpha: return v beta = min(beta, v) return v
def getValue(self, card, game): gameClone = copy.deepcopy(game) gameClone.playCard(self.playerNum, card) state = gameClone.getState(self.playerNum) if state['isAttacker']: if card == dk.Durak.END_ROUND: state['isAttacker'] = False weights = self.w_def else: weights = self.w_atk else: weights = self.w_def state['hand'].addCards(state['table'].getCards()) features = util.extractFeatures(state) return util.logisticValue(weights, features)
def getValueRec(self, agent, game, card, depth, alpha, beta): otherAgent = int(not agent) gameClone = copy.deepcopy(game) gameClone.playCard(otherAgent, card) if gameClone.roundOver(): gameClone.endRound() if gameClone.gameOver() and gameClone.isWinner(self.playerNum): return 1 elif gameClone.gameOver() and gameClone.isLoser(self.playerNum): return 0 elif depth == 0: state = gameClone.getState(agent) features = util.extractFeatures(state) if agent == gameClone.attacker: weights = self.w_atk else: weights = self.w_def return util.logisticValue(weights, features) if otherAgent == self.playerNum: depth -= 1 if agent == gameClone.attacker: cards = gameClone.getAttackOptions(agent) else: cards = gameClone.getDefendOptions(agent) if agent == self.playerNum: v = float('-inf') for card in cards: v = max(v, self.getValueRec(otherAgent, gameClone, card, depth, alpha, beta)) if v >= beta: return v alpha = max(alpha, v) return v else: v = float('+inf') for card in cards: v = min(v, self.getValueRec(otherAgent, gameClone, card, depth, alpha, beta)) if v <= alpha: return v beta = min(beta, v) return v
import pandas as pd import util from sklearn.cross_validation import train_test_split from sklearn.ensemble import GradientBoostingRegressor,RandomForestRegressor from sklearn.externals import joblib from sklearn.metrics import classification_report #build 7's model after case study data7=pd.read_excel('201707_label new.xlsx') #data7=data7[data7['是否接通']==1] #data7=data7[['存折计划' in c for c in data7['租机计划']]] #data7['跪舔']=data7['跪舔']-data7['label']#这个feature使f1-score增加0.02 data7.loc[data7['跪舔']>=1,'跪舔']=1 data7=util.preprocessing(data7) X=features=util.extractFeatures(data7) y=data7['label'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) model=GradientBoostingRegressor() model.fit(X_train, y_train) print(model.score(X_train,y_train)) print(model.score(X_test,y_test)) y_pre=model.predict(X_test) a=pd.DataFrame({"pre":y_pre,"y_test":y_test}).merge(data7,how='left',left_index=True,right_index=True)[['pre','跪舔','y_test']] for k in range(50,105,5): k=k/100 a['pre_weight_{}'.format(k)]=k*a.pre+(1-k)*a['跪舔'] util.showReport(a['pre_weight_{}'.format(k)],a.y_test,30) #util.showFigure1(y_pre,y_test) #util.showPRfigure(y_pre,y_test) #util.showReport(y_pre,y_test,20) '''
y_pre=model.predict(X_test) util.showFigure1(y_pre,y_test) util.showPRfigure(y_pre,y_test) util.showReport(y_pre,y_test,50) ''' #predict 9 with 8's model data8 = pd.read_excel('8月租机到期数据-结果.xlsx') data8 = data8[data8['是否接通'] == 1] data8['跪舔'] = data8['跪舔'] - data8['label'] data8.loc[data8['跪舔'] >= 1, '跪舔'] = 1 data9 = pd.read_csv('201709-租机到期数据.csv', encoding='gbk') data9.loc[data9['跪舔'] >= 1, '跪舔'] = 1 data = pd.concat((data8, data9), axis=0, join='inner', ignore_index=True) data = util.preprocessing(data) X = features = util.extractFeatures(data, num_train=data8.shape[0]) X_train = X[:data8.shape[0]] X_test = X[data8.shape[0]:] y_train = data8['label'] model = GradientBoostingRegressor() model.fit(X_train, y_train) print(model.score(X_train, y_train)) y_pre = model.predict(X_test) data9['label'] = y_pre data9['label'] = 0.9 * data9['label'] + 0.1 * data9['跪舔'] data9.sort_values(by='label', ascending=False, inplace=True) data9.to_csv('201709_score.csv') #slice data9 = data9[[ '客户等级', '客户名称', '客户证件类型', '用户状态', '服务号码', '用户在网时长(月)', '入网时间', '年龄',
def generateDataAndLabels(inputFiles, matrixOutputFile, controlMatrixOutputFile, labelOutputFile, dateRange=None): if os.path.isfile(matrixOutputFile) and os.path.isfile( controlMatrixOutputFile) and os.path.isfile(labelOutputFile): print('Opening old file') with open(matrixOutputFile, 'rb') as f: pubRelations = pickle.load(f) with open(controlMatrixOutputFile, 'rb') as f: controlPubRelations = pickle.load(f) with open(labelOutputFile, 'rb') as f: labels = pickle.load(f) return pubRelations, controlPubRelations, labels if os.path.isfile('temp' + labelOutputFile): with open('temp' + labelOutputFile, 'rb') as f: publicationList = pickle.load(f) else: publicationList = PubmedStringParser.getAllData(inputFiles) for i in range(len(publicationList)): publicationList[i].genRetrospectiveCitations() publicationList[i].getCitedByPMCitations() with concurrent.futures.ThreadPoolExecutor( max_workers=constants.MAX_WORKERS) as executor: future_to_article = { executor.submit(publicationList[i].genCitedBy): publicationList[i].title for i in range(len(publicationList)) } for future in concurrent.futures.as_completed(future_to_article): articleTitle = future_to_article[future] try: data = future.result() except Exception as exc: print('%r generated an exception: %s' % (articleTitle, exc)) else: print('Graph retrieved for %s' % articleTitle) if data is None: print('But the data is none') else: print(data) with open('temp' + labelOutputFile, 'wb') as f: pickle.dump(publicationList, f) # for publication in publicationList: # publication.pubPrint() # Experimental vs control publication relations (eg TF-IDF vs count) publicationRelationships = [] controlPubRelations = [] labels = [] for i in range(len(publicationList)): if len(publicationList[i].abstract) == 0: continue # if len(publicationList[i].citedBy) == 0: continue for j in range(i + 1, len(publicationList)): if len(publicationList[j].abstract) == 0: continue # if len(publicationList[j].citedBy) == 0: continue if dateRange == None or util.dateDifference( publicationList[i].date, publicationList[j].date) <= dateRange: publicationRelationships.append( util.extractFeatures(publicationList[i], publicationList[j], 'tfidf')) controlPubRelations.append( util.extractFeatures(publicationList[i], publicationList[j], 'count')) citedByI = publicationList[i].citedBy citedByJ = publicationList[j].citedBy pmCitedByI = publicationList[i].PMCitedBy pmCitedByJ = publicationList[j].PMCitedBy if len(citedByI & citedByJ) == 0 and len(pmCitedByI & pmCitedByJ) == 0: labels.append(0) else: labels.append(1) # Convert to np array publicationRelationships = np.array(publicationRelationships) controlPubRelations = np.array(controlPubRelations) labels = np.array(labels) print('Saving new file') with open(matrixOutputFile, 'wb') as f: pickle.dump(publicationRelationships, f) with open(controlMatrixOutputFile, 'wb') as f: pickle.dump(controlPubRelations, f) with open(labelOutputFile, 'wb') as f: pickle.dump(labels, f) return publicationRelationships, controlPubRelations, labels