test_idx_range = np.sort(test_idx_reorder)
时间: 2023-06-23 14:09:07 浏览: 89
这行代码的作用是将 test_idx_reorder 中的元素排序,并返回排序后的索引。具体地,排序后的索引按升序排列,即第一个索引是 test_idx_reorder 中最小的元素的索引,第二个索引是 test_idx_reorder 中第二小的元素的索引,以此类推,最后一个索引是 test_idx_reorder 中最大的元素的索引。这个操作通常用于对数据进行切片或者选择子集。
相关问题
import os import pickle import cv2 import matplotlib.pyplot as plt import numpy as np from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout from keras.models import Sequential from keras.optimizers import adam_v2 from keras_preprocessing.image import ImageDataGenerator from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder, OneHotEncoder, LabelBinarizer def load_data(filename=r'/root/autodl-tmp/RML2016.10b.dat'): with open(r'/root/autodl-tmp/RML2016.10b.dat', 'rb') as p_f: Xd = pickle.load(p_f, encoding="latin-1") # 提取频谱图数据和标签 spectrograms = [] labels = [] train_idx = [] val_idx = [] test_idx = [] np.random.seed(2016) a = 0 for (mod, snr) in Xd: X_mod_snr = Xd[(mod, snr)] for i in range(X_mod_snr.shape[0]): data = X_mod_snr[i, 0] frequency_spectrum = np.fft.fft(data) power_spectrum = np.abs(frequency_spectrum) ** 2 spectrograms.append(power_spectrum) labels.append(mod) train_idx += list(np.random.choice(range(a * 6000, (a + 1) * 6000), size=3600, replace=False)) val_idx += list(np.random.choice(list(set(range(a * 6000, (a + 1) * 6000)) - set(train_idx)), size=1200, replace=False)) a += 1 # 数据预处理 # 1. 将频谱图的数值范围调整到0到1之间 spectrograms_normalized = spectrograms / np.max(spectrograms) # 2. 对标签进行独热编码 label_binarizer = LabelBinarizer() labels_encoded= label_binarizer.fit_transform(labels) # transfor the label form to one-hot # 3. 划分训练集、验证集和测试集 # X_train, X_temp, y_train, y_temp = train_test_split(spectrograms_normalized, labels_encoded, test_size=0.15, random_state=42) # X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42) spectrogramss = np.array(spectrograms_normalized) print(spectrogramss.shape) labels = np.array(labels) X = np.vstack(spectrogramss) n_examples = X.shape[0] test_idx = list(set(range(0, n_examples)) - set(train_idx) - set(val_idx)) np.random.shuffle(train_idx) np.random.shuffle(val_idx) np.random.shuffle(test_idx) X_train = X[train_idx] X_val = X[val_idx] X_test = X[test_idx] print(X_train.shape) print(X_val.shape) print(X_test.shape) y_train = labels[train_idx] y_val = labels[val_idx] y_test = labels[test_idx] print(y_train.shape) print(y_val.shape) print(y_test.shape) # X_train = np.expand_dims(X_train,axis=-1) # X_test = np.expand_dims(X_test,axis=-1) # print(X_train.shape) return (mod, snr), (X_train, y_train), (X_val, y_val), (X_test, y_test) 这是我的数据预处理代码
你的数据预处理代码看起来很完整。它首先加载pickle文件,然后提取频谱图数据和标签。接下来,它对频谱图数据进行归一化处理,并对标签进行独热编码。最后,它将数据集划分为训练集、验证集和测试集,并返回这些数据集。
在代码中,你还有一些注释掉的代码段,例如对数据集进行划分的部分。如果你打算使用这些划分代码,你可以取消注释相应的行。另外,如果你的频谱图数据维度是二维的,你可能需要使用`np.expand_dims`函数将其扩展为三维,以便与Conv2D层兼容。
如果你还有其他问题或需要进一步帮助,请随时问我。
param = {'num_leaves': 31, 'min_data_in_leaf': 20, 'objective': 'binary', 'learning_rate': 0.06, "boosting": "gbdt", "metric": 'None', "verbosity": -1} trn_data = lgb.Dataset(trn, trn_label) val_data = lgb.Dataset(val, val_label) num_round = 666 # clf = lgb.train(param, trn_data, num_round, valid_sets=[trn_data, val_data], verbose_eval=100, # early_stopping_rounds=300, feval=win_score_eval) clf = lgb.train(param, trn_data, num_round) # oof_lgb = clf.predict(val, num_iteration=clf.best_iteration) test_lgb = clf.predict(test, num_iteration=clf.best_iteration)thresh_hold = 0.5 oof_test_final = test_lgb >= thresh_hold print(metrics.accuracy_score(test_label, oof_test_final)) print(metrics.confusion_matrix(test_label, oof_test_final)) tp = np.sum(((oof_test_final == 1) & (test_label == 1))) pp = np.sum(oof_test_final == 1) print('accuracy1:%.3f'% (tp/(pp)))test_postive_idx = np.argwhere(oof_test_final == True).reshape(-1) # test_postive_idx = list(range(len(oof_test_final))) test_all_idx = np.argwhere(np.array(test_data_idx)).reshape(-1) stock_info['trade_date_id'] = stock_info['trade_date'].map(date_map) stock_info['trade_date_id'] = stock_info['trade_date_id'] + 1tmp_col = ['ts_code', 'trade_date', 'trade_date_id', 'open', 'high', 'low', 'close', 'ma5', 'ma13', 'ma21', 'label_final', 'name'] stock_info.iloc[test_all_idx[test_postive_idx]] tmp_df = stock_info[tmp_col].iloc[test_all_idx[test_postive_idx]].reset_index() tmp_df['label_prob'] = test_lgb[test_postive_idx] tmp_df['is_limit_up'] = tmp_df['close'] == tmp_df['high'] buy_df = tmp_df[(tmp_df['is_limit_up']==False)].reset_index() buy_df.drop(['index', 'level_0'], axis=1, inplace=True)buy_df['buy_flag'] = 1 stock_info_copy['sell_flag'] = 0tmp_idx = (index_df['trade_date'] == test_date_min+1) close1 = index_df[tmp_idx]['close'].values[0] test_date_max = 20220829 tmp_idx = (index_df['trade_date'] == test_date_max) close2 = index_df[tmp_idx]['close'].values[0]tmp_idx = (stock_info_copy['trade_date'] >= test_date_min) & (stock_info_copy['trade_date'] <= test_date_max) tmp_df = stock_info_copy[tmp_idx].reset_index(drop=True)from imp import reload import Account reload(Account) money_init = 200000 account = Account.Account(money_init, max_hold_period=20, stop_loss_rate=-0.07, stop_profit_rate=0.12) account.BackTest(buy_df, tmp_df, index_df, buy_price='open')tmp_df2 = buy_df[['ts_code', 'trade_date', 'label_prob', 'label_final']] tmp_df2 = tmp_df2.rename(columns={'trade_date':'buy_date'}) tmp_df = account.info tmp_df['buy_date'] = tmp_df['buy_date'].apply(lambda x: int(x)) tmp_df = tmp_df.merge(tmp_df2, on=['ts_code', 'buy_date'], how='left')最终的tmp_df是什么?tmp_df[tmp_df['label_final']==1]又选取了什么股票?
根据代码逐行分析,tmp_df 是一个 DataFrame,包含了股票的信息以及回测结果。其中,选取了 label_final 为 1 的股票,也就是模型预测为涨的股票,并且过滤掉了当天涨停的股票。最终买入的股票信息保存在 buy_df 中,回测时将其与 stock_info_copy、index_df 进行合并,得到了回测的结果,并将买入的股票信息与回测结果合并,生成了最终的 tmp_df。
阅读全文