在使用python编码时,四舍五入小数时遇到麻烦

问题描述 投票:-2回答:1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.preprocessing import OrdinalEncoder


df = pd.read_csv("mushrooms.csv",index_col=False,header=None)
def n(target):
   if target == 'p':
       return 1
   elif target == 'e':
       return 0

df[0] = df[0].apply(n)
#manually encoding the targets


targets = df[0]
inputs = df[df.columns[1:]]


def test_train_split(mydf, inputs, tratio, target):
   splitter = StratifiedShuffleSplit(n_splits = 1, test_size = tratio, random_state = 42)
   train_index, test_index = next(splitter.split(inputs, target))
   strat_train = mydf.iloc[train_index]
   strat_test = mydf.iloc[test_index]
   return strat_train, strat_test

def print_test_train_dfs(train_df, test_df, target_column = 'None'):
   print("\nTraining data:")
   train_df.info()
   if target_column != 'None':
       print(train_df[target_column].value_counts())
   print('\nTest data:')
   test_df.info()
   if target_column != 'None':
       print(test_df[target_column].value_counts())


traindf, testdf = test_train_split(df, inputs, 0.2, targets)

enc = OrdinalEncoder()
enc.fit(traindf)
df = enc.transform(testdf)
for i in range(len(df)):
   for j in range(len(df[1])):
       df[i][j].round(0)
df = pd.DataFrame.from_records(df)  
print(df)

df总是以小数结尾,例如1.0,而不是我想要的1。

我正在使用的数据集在这里https://www.kaggle.com/uciml/mushroom-classification

我还将补充一点,在.transform之后,df比数据帧更多地位于数组中

python encoding rounding
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.