美文网首页
DataFrame 数据处理字符转数值

DataFrame 数据处理字符转数值

作者: 垃圾桶边的狗 | 来源:发表于2019-05-30 21:45 被阅读0次
    from sklearn.neighbors import KNeighborsClassifier
    
    from sklearn.model_selection import StratifiedKFold,KFold
    
    sad26.png
    workclass = df['workclass'].unique()
    workclass
    
    123.png
    np.argwhere(workclass=='State-gov')[0][0]
    

    >>>0

    def conver(x):
        return np.argwhere(workclass==x)[0,0]
    
    X['workclass'] = X['workclass'].map(conver)
    
    234.png
    X.columns
    
    qwe17.png
    clos = ['marital_status', 'occupation',
           'relationship', 'race', 'sex', 'native_country']
    
    for clo in clos:
        u = X[clo].unique()
        
        def conver(x):
            return np.argwhere(u==x)[0,0]
        
        X[clo] = X[clo].map(conver)
    
    12313.png
    sKfold = StratifiedKFold(n_splits=20)
    
    knn = KNeighborsClassifier(n_neighbors=5)
    scores = []
    for train,test in sKfold.split(X,y):
    #     print(train.shape)
        knn.fit(X.iloc[train],y[train])
        
        s = knn.score(X.iloc[test],y[test])
        scores.append(s)
    
    np.mean(scores)
    

    >>>0.800651505347638

    相关文章

      网友评论

          本文标题:DataFrame 数据处理字符转数值

          本文链接:https://www.haomeiwen.com/subject/efvbtctx.html