def test_init_arrays_returns_the_right_unitary_size_nparrays(self): # Given class_num = 1 sample_num_per_class = 1 batch_num_per_class = 1 # When _, query_images, query_labels, support_images, support_labels, zeros = init_arrays( class_num, sample_num_per_class, batch_num_per_class) # Then expected_query_images = np.zeros((1, 3, 224, 224)) expected_query_labels = np.zeros((1, 1, 224, 224)) expected_support_images = np.zeros((1, 3, 224, 224)) expected_support_labels = np.zeros((1, 1, 224, 224)) zeros = np.zeros((1, 1, 224, 224)) self.assertEqual(np.shape(expected_query_images), np.shape(query_images)) self.assertEqual(np.shape(expected_query_labels), np.shape(query_labels)) self.assertEqual(np.shape(expected_support_images), np.shape(support_images)) self.assertEqual(np.shape(expected_support_labels), np.shape(support_labels))
def gen_ans(dataX, dataY, maxTimes): costList = list() dataSamples, dataFeatures = dataX.shape # 63 3 yita = 0.01 # 学习率 hiddenLayerNum = 4 # 第二层数量 thetaJ = 0 # theta J gamaH = np.zeros((1, hiddenLayerNum)) vh = np.zeros((dataFeatures, hiddenLayerNum)) wh = np.zeros((hiddenLayerNum, 1)) for time in range(maxTimes): coooost = 0 for i in range(len((dataX))): y = calNNY(dataX[i], vh, wh, gamaH, thetaJ) cost = calCost(y, dataY[i]) coooost += cost dertaWh, dertaThetaj, dertaVih, dertaGameH = calDertaGd( dataX[i], y, dataY[i], vh, wh, gamaH, thetaJ, yita) wh = wh + dertaWh thetaJ = thetaJ + dertaThetaj vh = vh + dertaVih gamaH = gamaH + dertaGameH # print(wh, thetaJ, vh, gamaH) costList.append(coooost) return costList, wh, thetaJ, vh, gamaH
def fit_linear(self, features, y): data = { 'T': y.shape[0], 'K': features.shape[1], 'y': y, 'X': features, } try: params = self.stan_model.optimizing( data=data, iter=4000, init=lambda: { 'beta': np.zeros(features.shape[1]), 'sigma_obs': 1 }, ) except RuntimeError: params = self.stan_model.optimizing( data=data, iter=100, init=lambda: { 'beta': np.zeros(features.shape[1]), 'sigma_obs': 1 }, algorithm='Newton', ) return params['beta']
def fit(self, X_train, Y_train, kernel, param, C): # arrays: X; Y =-1,1 Lk = 0 self._alphas = np.zeros(len(Y_train)) self._alphas.fill(C / 1000) print("We are before iteration:") for iteration in range(self._iterations): batch_size = 10 for k in range(0, (len(X_train) - 1) // batch_size + 1): # Calculate the amount of butches in the Dt cur_batch_size = min( batch_size, len(X_train) - 1 - k * batch_size) # Choose the butch or remaining number L_diff = np.zeros(len(self._alphas)) L_butch = 0 gradient_rate = 0.0006 / (k + 1) alpha = 0.05 for i in range(cur_batch_size): L_diff += self.diffF( X_train, Y_train, X_train[k * batch_size + i][:], Y_train[k * batch_size + i], kernel, param, C, k * batch_size + i) * gradient_rate L_butch += self.F(X_train, Y_train, X_train[k * batch_size + i], Y_train[k * batch_size + i], kernel, param, k * batch_size + i) sumai = 0 for i in range(len(self._alphas) - 1): sumai += Y_train[i] * self._alphas[i] self._alphas[len(self._alphas) - 1] = -sumai / Y_train[len(self._alphas) - 1] alpha_k = self._alphas self._alphas = alpha_k - L_diff / batch_size for i in range(len(self._alphas)): if self._alphas[i] > C: self._alphas = alpha_k break Lk_previous = Lk Lk = (1 - alpha) * Lk + alpha * L_butch if (np.linalg.norm(alpha_k - self._alphas < 0.00001)) and ( np.linalg.norm(Lk - Lk_previous < 0.00001)): break w = np.zeros(len(X_train[0])) X_extended = X_train for i in range(len(X_extended)): w += self._alphas[i] * X_train[i] self._w = w
def levenshtein_ratio_and_distance(s, t, ratio_calc=False): # Initialize matrix of zeros rows = len(s) + 1 cols = len(t) + 1 distance = np.zeros((rows, cols), dtype=int) for i in range(1, rows): for k in range(1, cols): distance[i][0] = i distance[0][k] = k # Iterate over the matrix for col in range(1, cols): for row in range(1, rows): if s[row - 1] == t[col - 1]: cost = 0 # If the characters are the same in the two strings in a given position [i,j] then the cost is 0 else: # the cost of a substitution is 2. If we calculate just distance, then the cost of a substitution is 1. if ratio_calc == True: cost = 2 else: cost = 1 distance[row][col] = min(distance[row - 1][col] + 1, distance[row][col - 1] + 1, distance[row - 1][col - 1] + cost) if ratio_calc == True: # Computation of the Levenshtein Distance Ratio Ratio = ((len(s) + len(t)) - distance[row][col]) / (len(s) + len(t)) return Ratio else: return None
def sequential_evaluation(self): for index in range(len(self.encoded_sequence_to_evaluate) - 1): self.is_trivial_prediction = False self.is_only_catalog_prediction = False self.prm = np.zeros(len(self.metrics.values())) self.ndcg = 0 clicked_item = self.encoded_sequence_to_evaluate[index] self.evaluate_sequence(clicked_item, self.decoded_sequence) insert_evaluation(user_id=self.user_id, session_id=self.session_id, precision=self.prm[0], recall=self.prm[1], mrr=self.prm[2], ndcg=self.ndcg, predictor_name=self.model_name, trivial_prediction=self.is_trivial_prediction, catalog_prediction=self.is_only_catalog_prediction, catalog_count=0, ground_truth=self.real, sequence=' '.join(map(str, self.decoded_sequence)), input_sequence=int(clicked_item), input_sequence_length=1, user_sequence_length=len(self.decoded_sequence), predictions=' '.join(map(str, self.recommendation))) self.increment_by += 1 if self.increment_by == len(self.decoded_sequence): break
def getUnseenData(images_dir, max_num, input_shape): for root, dirs, _ in os.walk(images_dir): num_of_images = min(max_num, len(dirs)) unseen_images = np.zeros([num_of_images, 3, 100, 100]) index = 0 for folder in dirs: g_img_path = get_pkg_data_filename('%s/g_norm.fits' % (os.path.join(root, folder))) r_img_path = get_pkg_data_filename('%s/r_norm.fits' % (os.path.join(root, folder))) i_img_path = get_pkg_data_filename('%s/i_norm.fits' % (os.path.join(root, folder))) g_data = fits.open(g_img_path)[0].data[0:100, 0:100] r_data = fits.open(r_img_path)[0].data[0:100, 0:100] i_data = fits.open(i_img_path)[0].data[0:100, 0:100] img_data = [g_data, r_data, i_data] unseen_images[index] = img_data index += 1 if index >= num_of_images: break return unseen_images.reshape(num_of_images, input_shape[0], input_shape[1], input_shape[2])
def find_missing_seat(seats_data: List[Tuple[int, int]]) -> Tuple[int, int]: """ Given a list of seat coordinates taken, return the only one with previous and after seats occupied :param seats_data: Occupied seats data coordinates :return: """ # Create seats matrix ar = np.array(seats_data) res = np.zeros((PLANE_ROW_NUMBER, PLANE_COLUMN_NUMBER), dtype=int) res[ar[:, 0], ar[:, 1]] = 1 # Find all empty seats empty_seats_raw = np.where(res == 0) empty_seats = list(zip(empty_seats_raw[0], empty_seats_raw[1])) # Find the only valid empty seat for empty_seat in empty_seats: before_seat, after_seat = get_adjacent_seats(*empty_seat) if (before_seat is not None and after_seat is not None and res[before_seat[0]][before_seat[1]] and res[after_seat[0]][after_seat[1]]): return empty_seat
def __init__(self, legend_place: str, a_float: float, p_stats: PortfolioStats, portfolio_data: DataFrame = DataFrame()): self._legend_place = legend_place self._a_float = a_float self._portfolio_data = portfolio_data # Creating an empty array to store portfolio weights self._weight_matrix = np.zeros( (self._threshold, len(portfolio_data.columns))) # Creating an empty array to store portfolio returns self._annual_weighted_log_return_matrix = np.zeros(self._threshold) # Creating an empty array to store portfolio risks self._risk_matrix = np.zeros(self._threshold) # Creating an empty array to store portfolio sharpe ratio self._sharpe_ratio_matrix = np.zeros(self._threshold) self._setMatrices(portfolio_data, p_stats.LogDailyReturns, p_stats.LogAnnualCovarianceMatrix) print('portfolio_risk.min', self._risk_matrix.min()) print('sharpe_ratio.max', self._sharpe_ratio_matrix.max()) self._min_risk_series = \ self._getMinimalRisk(self._weight_matrix[self._risk_matrix.argmin()], portfolio_data.columns) print(self._min_risk_series) #self._plotMinimalRisk() self._max_sharpe_ratio_series = \ self._getMaximalSharpeRatio(self._weight_matrix[self._sharpe_ratio_matrix.argmax()], portfolio_data.columns) print(self._max_sharpe_ratio_series) #self._plotMaximalSharpeRatio() #self._plotRiskReturns(portfolio_data) #mu: Series = expected_returns.mean_historical_return(portfolio_data) # returns.mean() * 252 #S: DataFrame = risk_models.sample_cov(portfolio_data) # Get the sample covariance matrix #ef: EfficientFrontier = EfficientFrontier(mu, S) self._efficient_frontier = self._getEfficientFrontier(portfolio_data) # Maximize the Sharpe ratio, and get the raw weights max_weights = self._efficient_frontier.max_sharpe() # Note the weights may have some rounding error, meaning they may not add up exactly to 1 but should be close cleaned_weights = self._efficient_frontier.clean_weights() self._efficient_frontier.portfolio_performance(verbose=True) latest_prices_series: Series = get_latest_prices(portfolio_data) max_weights = cleaned_weights self._discrete_allocation = DiscreteAllocation( max_weights, latest_prices_series, total_portfolio_value=10000) allocation, leftover = self._discrete_allocation.lp_portfolio() print("Discrete allocation:", allocation) print("Funds remaining: ${:.2f}".format(leftover))
def transform(self, X, **transform_params): numpy_table = np.zeros((len(X), len(self.user_names))) for row_index,tweet in enumerate(X): for name_index,name in enumerate(self.user_names): if name==tweet.screen_name: numpy_table[row_index,name_index] = 1 break table = DataFrame(numpy_table,columns=self.user_names) return table
def transform(self, X, **transform_params): numpy_table = np.zeros((len(X), len(self.punctuations))) for row_index,tweet in enumerate(X): for punct_index, punct in enumerate(self.punctuations): if punct in tweet.text: numpy_table[row_index, punct_index] = 1 break table = DataFrame(numpy_table,columns=self.punctuations) return table
def addRatings(self, ISBNS, ratings, user_id=88888888): """ :param ISBNS:新用户的评价的书籍 列表 :param ratings:新用户给书籍的评价 列表 :param user_id:分配给新用户的id,存在默认值 :return: """ zers = np.zeros(shape=(1, self.R.shape[1])) for i in range(len(ratings)): zers[0][self.ISBN_list.index(ISBNS[i])] = ratings[i] self.R = np.append(self.R, zers, axis=0) self.user_list.append(user_id)
def predict(self, X, Y, X_train, Y_train): y_pred = np.zeros(len(Y)) for j in range(len(X)): pr = 0 for i in range(len(X_train)): pr += self._alphas[i] * np.dot( X[j], X_train[i]) * Y_train[i] - ( np.dot(self._w, X_train[i]) - Y_train[i]) y_pred[j] = np.sign(pr) accuracy = accuracy_score(Y, y_pred, normalize=True) return accuracy
def getblock(self, xco, y): last = np.zeros((3, 3)) if xco <= 2: if y <= 2: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i)[j] return last elif 2 < y <= 5: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i)[j + 3] return last elif 5 < y <= 8: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i)[j + 6] return last elif 2 < xco <= 5: if y <= 2: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i + 3)[j] return last elif 2 < y <= 5: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i + 3)[j + 3] return last elif 5 < y <= 8: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i + 3)[j + 6] return last elif 5 < xco <= 8: if y <= 2: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i + 6)[j] return last elif 2 < y <= 5: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i + 6)[j + 3] return last elif 5 < y <= 8: for i in range(0, 3): for j in range(0, 3): last[i, j] = self.getrow(i + 6)[j + 6] return last
def diffF(self, diffX_train, diffY_train, diffX, diffY, kernel, param, C, index): new_alphas = np.zeros(len(self._alphas)) sum1 = 0 sum2 = 0 sum1 += 1 for j in range(len(self._alphas)): sum2 += self._alphas[index] * diffY * diffY_train[j] * Kernel( kernel, diffX, diffX_train[j], param) new_alphas[index] = sum2 + sum1 return new_alphas
def F(self, FX_train, FY_train, FX, FY, kernel, param, index): new_alphas = np.zeros(len(self._alphas)) sum1 = 0 sum2 = 0 sum1 += -self._alphas[index] for j in range(len(self._alphas)): sum2 += self._alphas[index] * self._alphas[j] * FY * FY_train[ j] * Kernel(kernel, FX, FX_train[j], param) sum2 = sum2 * (1 / 2) new_alphas[index] = sum1 + sum2 return new_alphas
def __init__(self, user_id, model_name, predictor, item_dictionary, session_id, encoded_sequence_to_evaluate, decoded_sequence, top_k): self.user_id = user_id self.model_name = model_name self.predictor = predictor self.item_dictionary = item_dictionary self.session_id = session_id self.encoded_sequence_to_evaluate = encoded_sequence_to_evaluate self.decoded_sequence = decoded_sequence self.top_k = top_k self.increment_by = 1 self.ndcg = 0. self.real = 0. self.real_index = 0 self.metrics = {'precision': precision, 'recall': recall, 'mrr': mrr} self.prm = np.zeros(len(self.metrics.values())) self.recommendation = None self.is_trivial_prediction = False self.is_only_catalog_prediction = False
def transform(self, X, **transform_params): numpy_table = np.zeros((len(X), len(self.allTags))) for row_num, tweet in enumerate(X): tokens = nltk.wordpunct_tokenize(tweet.text) word_tags = self._tagger.tag(tokens) tag_fd = nltk.FreqDist(tag for (word, tag) in word_tags) for tag in tag_fd: if tag in self.allTags: indexOfTag = self.allTags.index(tag) if indexOfTag > -1: # Proportion, how much part of speech appears in the text. numpy_table[row_num, indexOfTag] = (tag_fd[tag]/(len(tokens)*1.0)) data_table = DataFrame(numpy_table, columns=self.allTags) return data_table
def get_sample_map(delta_fname, x_coverage, average_read_length, rate_param): lengthdb = read_pickle(lengthdbpath) bin_size = int(rate_param / float(x_coverage)) with open(delta_fname) as inf: delta = ujson.load(inf, precise_float=True) bacid_maps = dict() for _, mapngs in delta: for dest_id, pos1, pos2, used_koef, _ in mapngs: if dest_id not in bacid_maps: bacid_maps[dest_id] = np.zeros( int(lengthdb[dest_id] / bin_size) + 1) ind1 = int((pos1 + (average_read_length / 2)) / bin_size) if pos2 >= 0: used_koef = used_koef / 2.0 ind2 = int((pos2 + (average_read_length / 2)) / bin_size) bacid_maps[dest_id][ind2] += used_koef bacid_maps[dest_id][ind1] += used_koef return {dest_id:cov_map for dest_id, cov_map in bacid_maps.iteritems()\ if np.median(cov_map) >= rate_param}
def sequential_evaluation(self): """ Method iterates through the user sequences (not including the last item) and evaluates the next-item predictions for the given item visit. 1. Get the clicked item 2. Call the evaluate_sequence method by giving the clicked item and the sequence information to get recommendations and its evaluation. 3. Insert the evaluation data to the database """ for index in range(len(self.encoded_sequence_to_evaluate) - 1): self.is_trivial_prediction = False self.is_only_catalog_prediction = False self.prm = np.zeros(len(self.metrics.values())) self.ndcg = 0 clicked_item = self.encoded_sequence_to_evaluate[index] self.evaluate_sequence(clicked_item, self.decoded_sequence) insert_evaluation( user_id=self.user_id, session_id=self.session_id, precision=self.prm[0], recall=self.prm[1], mrr=self.prm[2], ndcg=self.ndcg, predictor_name=self.model_name, trivial_prediction=self.is_trivial_prediction, catalog_prediction=self.is_only_catalog_prediction, catalog_count=0, ground_truth=self.real, sequence=' '.join(map(str, self.decoded_sequence)), input_sequence=int(clicked_item), input_sequence_length=1, user_sequence_length=len(self.decoded_sequence), predictions=' '.join(map(str, self.recommendation))) self.increment_by += 1 if self.increment_by == len(self.decoded_sequence): break
def dice(img1, img2, labels=None, nargout=1): ''' Dice [1] volume overlap metric The default is to *not* return a measure for the background layer (label = 0) [1] Dice, Lee R. "Measures of the amount of ecologic association between species." Ecology 26.3 (1945): 297-302. Parameters ---------- vol1 : nd array. The first volume (e.g. predicted volume) vol2 : nd array. The second volume (e.g. "true" volume) labels : optional vector of labels on which to compute Dice. If this is not provided, Dice is computed on all non-background (non-0) labels nargout : optional control of output arguments. if 1, output Dice measure(s). if 2, output tuple of (Dice, labels) Output ------ if nargout == 1 : dice : vector of dice measures for each labels if nargout == 2 : (dice, labels) : where labels is a vector of the labels on which dice was computed ''' if labels is None: labels = np.unique(np.concatenate((img1, img2))) # 输出一维数组 labels = np.delete(labels, np.where(labels == 0)) # remove background dicem = np.zeros(len(labels)) for idx, lab in enumerate(labels): top = 2 * np.sum(np.logical_and(img1 == lab, img2 == lab)) bottom = np.sum(img1 == lab) + np.sum(img2 == lab) bottom = np.maximum(bottom, np.finfo(float).eps) # add epsilon. 机器最小的正数 dicem[idx] = top / bottom if nargout == 1: return dicem else: return (dicem, labels)
def __init__(self, user_id, model_name, session_id, encoded_sequence_to_evaluate, decoded_sequence, top_k, increment_by, content_dataframe, cosine_similarity_matrix, tour_ids): self.user_id = user_id self.model_name = model_name self.session_id = session_id self.encoded_sequence_to_evaluate = encoded_sequence_to_evaluate self.decoded_sequence = decoded_sequence self.top_k = top_k self.increment_by = increment_by self.ndcg = 0. self.real = 0. self.real_index = 0 self.metrics = {'precision': precision, 'recall': recall, 'mrr': mrr} self.prm = np.zeros(len(self.metrics.values())) self.recommendation = None self.content_dataframe = content_dataframe self.cosine_similarity_matrix = cosine_similarity_matrix self.tour_ids = tour_ids self.is_trivial_prediction = False self.is_only_catalog_prediction = False
def converge_to_0_dvh(raw_dvh): """ :param raw_dvh: Dictionary produced by calc_dvhs(..) function. :return: Dictionary of bincenters and counts (x and y of DVH) """ res = {} zeros = np.zeros(3) for roi in raw_dvh: res[roi] = {} dvh = raw_dvh[roi] # The last value of DVH is not equal to 0 if len(dvh.counts) > 0: if dvh.counts[-1] != 0: tmp_bincenters = [] for i in range(3): tmp_bincenters.append(dvh.bincenters[-1] + i) tmp_bincenters = np.array(tmp_bincenters) tmp_bincenters = np.concatenate( (dvh.bincenters.flatten(), tmp_bincenters)) bincenters = np.array(tmp_bincenters) counts = np.concatenate( (dvh.counts.flatten(), np.array(zeros))) # The last value of DVH is equal to 0 else: bincenters = dvh.bincenters counts = dvh.counts else: bincenters = dvh.bincenters counts = dvh.counts res[roi]['bincenters'] = bincenters res[roi]['counts'] = counts return res
def returnMatrix(jsonStr): jsonObject = demjson.decode(jsonStr) # print(jsonObject) # print(jsonObject["root"]["text"]) treeData = jsonObject['root'] # 树节点 不包含例子和横向节点 summariesData = jsonObject['summaries'] summariesIdNode = {} # 储存sunmary for i in summariesData: summariesIdNode[i['trees'][0]] = i['text'] linksData = jsonObject['links'] # 横向链接 linksIdNode = {} # 储存links 其中两个id 以&分割 for i in linksData: inAndOut = i['input'] + '&' + i['output'] linksIdNode[inAndOut] = i['text'] idNodeDic = {} # 储存id和节点的对应关系 idTemp = [] # 储存变量的临时ID,存到字典 listTree = [] # 储存节点层数和文本 nodeLevelNum = 0 # 储存不包含sunmmay的层数 for i in dict_generator(treeData): # print(i) if i.__contains__('text') and not i.__contains__('connectText'): listTree.append(i) if i.__len__() - 1 > nodeLevelNum: nodeLevelNum = i.__len__() - 1 if i.__contains__('id'): idTemp.append(i[-1]) for i in range(0, idTemp.__len__()): idNodeDic[idTemp[i]] = listTree[i][-1] # print(idNodeDic) # 储存了id和node的对应关系 # print('.'.join(i[0:-1]), ':', i[-1]) # print(listTree) indexText = [] # 储存树的索引 不包括summary节点 for i in listTree: indexText.append(i[-1]) nodes = list(set(list(indexText + list(summariesIdNode.values())))) # 节点个数和重复节点处理 mapMatrix = np.zeros((nodes.__len__(), nodes.__len__())) # 建立全部节点矩阵 mapMatrixIndex = pd.DataFrame(mapMatrix, index=nodes, columns=nodes) # 建立所有节点矩阵,以节点内容为索引 for i in range(0, listTree.__len__() - 1): # 纵向节点关系存入 if len(listTree[i]) < len(listTree[i + 1]): mapMatrixIndex.at[listTree[i][-1], listTree[i + 1][-1]] = 1 indexNode = listTree[i][-1] if len(listTree[i]) == len(listTree[i + 1]): mapMatrixIndex.at[[indexNode], listTree[i + 1][-1]] = 1 # print(indexNode+'--------'+listTree[i + 1][-1]) if len(listTree[i + 1]) == 3: # 第一层节点数 mapMatrixIndex.at[listTree[0][-1], listTree[i + 1][-1]] = 1 if len(listTree[i]) > len(listTree[i + 1]): j = i while 1: if listTree[j].__len__() == listTree[ i + 1].__len__() - 1 or listTree[j].__len__() == 2: break j = j - 1 mapMatrixIndex.at[listTree[j][-1], listTree[i + 1][-1]] = 1 indexNode = listTree[j][-1] for i in summariesIdNode.keys(): mapMatrixIndex.at[ idNodeDic.get(i), summariesIdNode.get(i)] = 1 # 纵向节点关系存储结束 返回1:树结构矩阵mapMatrixIndex nodeSummaryLevel = [] # 储存层次关系 每个节点的层次 每个层数的节点 for i in range(0, nodeLevelNum): nodeSummaryLevel.append([]) # 构造储存结构 # print(listTree) for i in listTree: nodeSummaryLevel[i.__len__() - 2].append(i[-1]) levelTemp = [] # 储存summary扩展的节点 for i in summariesIdNode.values(): for j in nodes: # 遍历找到summary节点链接的普通节点 if mapMatrixIndex.at[j, i] == 1: for k in range(0, len(nodeSummaryLevel)): if nodeSummaryLevel[k].__contains__( j) and k < len(nodeSummaryLevel) - 1: nodeSummaryLevel[k + 1].append(i) # 如果上一层包含 则下一层增加例子 if nodeSummaryLevel[k].__contains__( j) and k == len(nodeSummaryLevel) - 1: levelTemp.append(i) if levelTemp.__len__() != 0: nodeSummaryLevel.append(levelTemp) # 层次关系 返回2:nodeSummaryLevel # print(nodeSummaryLevel) # print(nodeSummaryLevel) # print(nodeLevelNum) #层数波包括summary ###########################横向链接矩阵构造 nodeLinksSummaryMatrix = np.zeros( (nodes.__len__() + linksIdNode.__len__(), nodes.__len__() + linksIdNode.__len__())) nodeLinksSummaryMatrixIndex = pd.DataFrame(nodeLinksSummaryMatrix, index=nodes + list(linksIdNode), columns=nodes + list(linksIdNode)) for i in nodes: # 将原有的不包括横向连接的复制进来 for j in nodes: nodeLinksSummaryMatrixIndex.at[i, j] = mapMatrixIndex.loc[i][j] for i in linksIdNode.keys(): # 将横向链接情况输入 inputAndOutSplit = i.split('&') nodeLinksSummaryMatrixIndex.at[idNodeDic.get(inputAndOutSplit[0]), linksIdNode.get(i)] = 1 # 横向连接的输入节点 nodeLinksSummaryMatrixIndex.at[ linksIdNode.get(i), idNodeDic.get(inputAndOutSplit[1])] = 1 # 横向连接的输出节点 # 返回3:横向链接矩阵 nodeLinksSummaryMatrixIndex return mapMatrixIndex.fillna( 0), nodeSummaryLevel, nodeLinksSummaryMatrixIndex.fillna(0)
iterations = 10000 batch_size = 20 save_dir = 'output' start = 0 for step in range(iterations): random_latent_vectors = np.random.normal(size=(batch_size, latent_dim)) generated_images = generator.predict(random_latent_vectors) stop = start + batch_size real_images = x_train[start:stop] combined_images = np.concatenate([generated_images, real_images]) labels = np.concatenate( [np.ones((batch_size, 1)), np.zeros((batch_size, 1))]) labels += 0.05 * np.random.random(labels.shape) d_loss = discriminator.train_on_batch(combined_images, labels) random_latent_vectors = np.random.normal(size=(batch_size, latent_dim)) misleading_targets = np.zeros((batch_size, 1)) a_loss = gan.train_on_batch(random_latent_vectors, misleading_targets) start += batch_size if start > len(x_train) - batch_size: start = 0 if step % 100 == 0: gan.save_weights('gan.h5') img = image.array_to_img(generated_images[0] * 255., scale=False) img.save(os.path.join(save_dir, 'generated_frog' + str(step) + '.png')) img = image.array_to_img(real_images[0] * 255., scale=False) img.save(os.path.join(save_dir, 'real_frog' + str(step) + '.png'))
from pandas import np h1 = [ 14.53, 14.93, 14.10, 14.83, 14.48, 14.08, 13.39, 14.36, 12.76, 13.00, 12.29, 12.00, 10.43, 12.61, 11.16, 11.76, 12.07, 9.18, 11.69, 10.59, 12.22, 11.25, 9.59, 9.83, 8.94, 11.09, 8.63, 10.57, 9.87, 8.77, 7.60, 8.48, 10.57, 10.66, 11.84, 12.71, 11.42, 11.33, 13.12, 12.64, 12.75, 12.63, 13.03, 13.09, 13.91, 12.59, 13.21, 7.54, 10.67, 9.63, 9.00, 8.78, 8.17, 11.40, 8.18, 9.51, 13.49, 13.76, 15.58, 10.83, 11.77, 12.11, 11.09, 11.77, 9.85, 10.69, 9.11, 10.18, 9.52, 8.52, 9.20, 11.51, 12.53, 11.27, 5.83, 10.58 ] h2 = [ 11.98, 12.85, 11.04, 12.64, 11.88, 11.00, 9.50, 11.61, 8.14, 8.66, 11.41, 10.75, 7.19, 12.13, 8.86, 10.20, 10.91, 4.36, 10.06, 7.57, 11.24, 9.05, 5.29, 8.81, 6.80, 11.67, 6.08, 10.49, 8.91, 6.41, 3.75, 5.75, 10.50, 5.22, 7.78, 9.66, 6.87, 6.68, 14.09, 12.95, 13.21, 12.94, 13.88, 14.02, 15.96, 12.83, 14.31, 6.27, 13.11, 10.83, 9.46, 8.97, 7.64, 14.70, 7.66, 10.58, 14.36, 14.95, 18.91, 8.60, 10.65, 11.39, 9.17, 10.64, 11.30, 12.98, 9.81, 11.95, 10.62, 8.62, 9.98, 14.62, 16.66, 14.14, 3.23, 12.76 ] h = np.zeros(76) for i in range(76): h[i] = 0.3 * h1[i] + 0.7 * h2[i] # for i in range(len(h)): # h[i] = h[i] / 2 print(h, "\n")
classifier_array = np.array(classifier) agent_array = np.array(agent) min_cla = np.amin(classifier_array) max_cla = np.amax(classifier_array) unique_element_classifier = np.unique(classifier_array) # print(unique_element_classifier) # print(len(unique_element_classifier)) unique_element_agent = np.unique(agent_array) # print(unique_element_agent) # print(len(unique_element_agent)) dif_cla = max_cla - min_cla real_classifier = np.zeros( len(unique_element_classifier)) # (maxEnd - minEnd) * ((value - minStart) / (maxStart - minStart)) + minEnd; value_agent = agent[0] clax = [] final = [] for i in range(len(agent) + 1): if i >= len(agent): # sort the list of tuples clax.sort(key=lambda tup: tup[0]) real_result_ordered = [] for el in clax: real_result_ordered.append(el[1])
import operator # |ri-rj| ri_rj = calculate_ri_rj(star_a, star_b) # G * Mj/(|rj-ri|^3)*(xj-xi) ax = G * star_b[0] / ri_rj**3 * (star_b[1][0] - star_a[1][0]) ay = G * star_b[0] / ri_rj**3 * (star_b[1][1] - star_a[1][1]) az = G * star_b[0] / ri_rj**3 * (star_b[1][2] - star_a[1][2]) new_ac = tuple(map(operator.add, old_ac, (ax, ay, az))) return new_ac stars = [[100000, (1, 1, 1), (1, 1, 1)], [1, (2, 2, 2), (2, 1000, 2)], [100000, (3, 3, 3), (3, 3, 3)], [1, (4, 4, 4), (4, 1000, 4)]] accu = np.zeros((4), dtype=[('ax', np.double), ('ay', np.double), ('az', np.double)]) print(f"accu: {accu}", flush=True) for i in range(4): for j in range(4): if i == j: continue accu[i] = calculate_interactions(accu[i], stars[i], stars[j]) print(accu) acc_t1_parallel = [(4.81146608e+03, 4.81146608e+03, 4.81146608e+03), (4.81125224e-02, 4.81125224e-02, 4.81125224e-02), (-4.81125224e+03, -4.81125224e+03, -4.81125224e+03), (-2.13833914e+04, -2.13833914e+04, -2.13833914e+04)]
# train a random forest classifier rfc = RandomForestClassifier(n_jobs=-1, n_estimators=300) print('Training a random forest on the training set...') t0 = cpu_time() # `train_features_tfidf` = 60k x 93 matrix of term frequencies normalized (divided by) document frequencies # `train_targets` = array(['Class_1', 'Class_1', 'Class_1', ..., 'Class_9', 'Class_9', 'Class_9'], dtype=object) rfc.fit(train_features_tfidf, train_targets) print("Random Forest took {} sec of the CPU's time.".format(cpu_time() - t0)) # predict on training set print('Rerunning the predictor to predict the the labels for the {} training set records...'.format(len(train_features_tfidf))) t0 = cpu_time() rfc_preds = pd.DataFrame(rfc.predict_proba(train_features_tfidf), index=train_ids, columns=sample_label_set) print('completed RFC predictions') train_actual = pd.DataFrame(np.zeros(rfc_preds.shape), index=train_ids, columns=sample_label_set) for i in range(len(train_actual)): train_actual.iloc[i, train_targets_encoded[i]] = 1 ll_rfc = metrics.log_loss(train_actual.values, rfc_preds.values) print('log loss for Random Forest: {}'.format(ll_rfc)) print("Predictions on training set features took {} sec of the CPU's time.".format(cpu_time() - t0)) print("Writing a Kaggle submission csv file") t0 = cpu_time() # create submission file submission_df = pd.DataFrame(rfc.predict_proba(test), index=sample_ids, columns=sample_label_set) submission_df.to_csv('random_forest_300_submission.csv', index_label='id') ################################################################################################### ### PCA