新手求教:怎样把两个列向量合并成一个 n*2 的矩阵? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
acone2003
V2EX    Python

新手求教:怎样把两个列向量合并成一个 n*2 的矩阵?

  •  
  •   acone2003 2018-06-08 06:12:33 +08:00 5591 次点击
    这是一个创建于 2757 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近学习随即森林分类算法,碰到一个问题,试了各种互联网上的方法,都不能得到正确结果,只好在这里求助大家了.
    是这样:test_lables 是测试样本二分类的真实标签,有 692 个样本,test_hat 是预测值,现在我想把这两个合并在一块,组成一个 692*2 的矩阵,每个预测值对应一个真实值。源代码如下:

    import numpy as np
    import pandas as pd
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.dummy import DummyClassifier
    from sklearn.ensemble import AdaBoostClassifier
    from sklearn.svm import SVC
    #from sklearn import datasets

    dataframe = pd.read_csv( "D:/Research/TuPo_sel0.Train.csv", header = None )
    train_features = dataframe.iloc[ :, 0:24]
    train_lables = dataframe.iloc[:, 24]

    test_data = pd.read_csv( "D:/Research/TuPo_sel0.Valid.csv", header = None )
    test_features = test_data.iloc[ :, 0:24 ]
    test_lables = test_data.iloc[ :, 24 ]

    dummy = DummyClassifier( strategy = 'uniform', random_state = 1 )
    dummy.fit( train_features, train_lables )
    print( "dummy_score =", dummy.score( test_features, test_lables ) )

    style = 1

    if style == 1:
    max_features = 19
    n_estimators = 400
    randomforest = RandomForestClassifier( max_features = max_features, n_estimators = n_estimators, random_state=1, n_jobs=-1 )
    model = randomforest.fit( train_features, train_lables )
    test_hat = model.predict( test_features )
    test_hat1 = np.hstack( ( test_hat, test_lables ) )
    test_hat1.reshape( -1, 2 )
    print( test_hat1.shape )
    print( test_hat1 )
    print( "max_features =", max_features, "; n_estimators =", n_estimators,
    "; randomforest_score =", randomforest.score( test_features, test_lables ) )

    运算结果如下:
    runfile('D:/Python Programs/TryLoadData.py', wdir='D:/Python Programs')
    dummy_score = 0.5447976878612717
    (1384,)
    [0 0 1 ... 0 0 0]
    max_features = 19 ; n_estimators = 400 ; randomforest_score = 0.6416184971098265

    求教各位怎么修改才能得到正确结果?
    acone2003
        1
    acone2003  
    OP
       2018-06-08 06:35:55 +08:00
    另外再顺便问一下:怎样计算测试集中的预测精度,即所有预测为 1 的样本的预测正确率。
    enenaaa
        2
    enenaaa  
       2018-06-08 09:02:17 +08:00
    test_hat1 = np.hstack((test_hat.reshape(-1, 1), test_lables.reshape(-1, 1)))

    查看训练结果可以看简报,metrics.classification_report
    acone2003
        3
    acone2003  
    OP
       2018-06-08 09:18:39 +08:00
    谢谢 enenaaa,搞定!
    necomancer
        4
    necomancer  
       2018-06-08 09:51:42 +08:00
    np.vstack([a, b]).T
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5496 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 03:27 PVG 11:27 LAX 19:27 JFK 22:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86