微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ML:如何确保标签在正确的位置?太好了,不能成为真正的类回归结果

如何解决ML:如何确保标签在正确的位置?太好了,不能成为真正的类回归结果

我使用 1100 行 84 列的市场数据。

https://archive.ics.uci.edu/ml/machine-learning-databases/00554/

数据清理相对较新,所以我认为我弄乱了标签位置? 这个想法是获得滞后 n+1 个索引计数的对数回报,并将其标记为 y={0,1},其中 0 是下跌日,1 是上涨日。

我做了 2 次 dropna,一次是在开始时清理初始 NaN 值,一次是在日志返回计算之后,因为第一行得到 NaN。如何确保标签位于正确的位置?

# Compare
from sklearn.model_selection import cross_val_score

for dataset_name,dataset in [('S&P500',data_spx1),('RUSSELL',data_russ1),('NASDAQ',data_ndaq1),('NYSE',data_nyse1),('DJIA',data_dow1)]:
    
    X = dataset.loc[:,dataset.columns != 'Class']
    y = dataset['Class']
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.3,random_state = 42)
    for name,model in class_models:
        mod = model
        mod.fit(X_train,y_train)
        accuracy = mod.score(X_test,y_test)
        n = 5
        kfold = model_selection.KFold(n_splits = n,shuffle = True,random_state = None)
        cv_result = cross_val_score(mod,X,cv = kfold,scoring = 'accuracy')
        print(dataset_name,name,'test_accuracy =',accuracy)
        print('CV random: ',cv_result)
        print('CV avg.: ',np.sum(cv_result) / n)
    print("")

平均测试集结果:NaiveBayes 0.80、SVM 0.55、KNN 0.56、DecisionTree 0.9、RandomForest 0.98

S&P500 NaiveBayes test_accuracy = 0.8293413173652695
CV avg.:  0.8220538924574798
S&P500 SVM test_accuracy = 0.5778443113772455
CV avg.:  0.5570233911041086
S&P500 KNN test_accuracy = 0.5718562874251497
CV avg.:  0.5678463216579808
S&P500 DTree test_accuracy = 0.8622754491017964
CV avg.:  0.8831979962024805
S&P500 RForest test_accuracy = 0.9311377245508982
CV avg.:  0.9272209429160101

CV shuffle 返回接近测试结果的平均值。 这些结果对于我使用的数据框是否正常,我是否有重复的数据点? 我尝试删除分类 base='Return' 列,结果几乎相同。

这是 F1 分数:

# Random forest - F1 score
# data_dow1
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.ensemble import RandomForestClassifier

X_train,random_state = 4)
rf = RandomForestClassifier(random_state = None)
rf.fit(X_train,y_train)
y_pred = rf.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print('Confusion matrix: \n',cm)
print('Classification report: \n',classification_report(y_test,y_pred))
sns.heatmap(cm,annot=True,fmt="d") 
plt.show()

输出

Confusion matrix: 
 [[145   1]
 [  0 188]]
Classification report: 
               precision    recall  f1-score   support

         0.0       1.00      0.99      1.00       146
         1.0       0.99      1.00      1.00       188

    accuracy                           1.00       334
   macro avg       1.00      1.00      1.00       334
weighted avg       1.00      1.00      1.00       334

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。