繁体   English   中英

TypeError: Expected binary or unicode string, got 618.0

[英]TypeError: Expected binary or unicode string, got 618.0


我一直在尝试将这个 ML Linear Model 实现到我的数据集中。 (https://www.tensorflow.org/tutorials/estimator/linear)
语言:Python 3.8.3
图书馆:TensorFlow 2.4.0
Numpy:1.19.3
Pandas
Matplotlib
和其他人:
 import os import sys import numpy as np import pandas as pd import matplotlib.pyplot as plt from IPython.display import clear_output from six.moves import urllib
 import tensorflow.compat.v2.feature_column as fc import tensorflow as tf

ss1517 是我的数据集的名称。 这是一个 CSV 文件,有 4116 行和 20 列,并且有很多 NaN 值(没有没有 NaN 值的列)

 traindata = ss1517.iloc[0:2470,:] # 60 % of my dataset is splitted by training set evaldata = ss1517.iloc[2470:4116, :] # 40 % of my dataset is splitted by eval set ytrain = traindata.pop("AvgOfMajor N") yeval = evaldata.pop("AvgOfMajor N")

CATEGORICAL_COLUMNS 是我的数据集中的分类列。
NUMERIC_COLUMNS 是我的数据集中的数字列。

 CATEGORICAL_COLUMNS = ['Location Name', 'Location Code', 'Borough', 'Register', 'Building Name', 'Schools in Building', 'ENGroupA', 'RangeA'] NUMERIC_COLUMNS = ['Geographical District Code', '# Schools', 'Major N', 'Oth N', 'NoCrim N', 'Prop N', 'Vio N', 'AvgOfOth N', 'AvgOfNoCrim N', 'AvgOfProp N', 'AvgOfVio N'] feature_columns = []#Sadece linear regression'u eğitmek için kullanıyoruz for feature_name in CATEGORICAL_COLUMNS: vocabulary = traindata[feature_name].unique() feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary)) for feature_name in NUMERIC_COLUMNS: feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))
 def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32): def input_function():# inner function, this will be returned. ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df)) # Create tf.data.Dataset object with data and its label if shuffle: ds = ds.shuffle(1000) # randomize order of data ds = ds.batch(batch_size).repeat(num_epochs) return ds # return a batch of dataset return input_function # return the input_function train_input_fn = make_input_fn(traindata, ytrain) eval_input_fn = make_input_fn(evaldata, yeval, num_epochs=1, shuffle=False)
 linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns) linear_est.train(train_input_fn) #train result = linear_est.evaluate(eval_input_fn) #get model metrics/stats by testing on testing data clear_output() #clears console output print(result["accuracy"]) #the result variable is simply dict of stats about our model

每次我尝试用df.fillna(method="ffill") TypeError: Expected binary or unicode string, got 618.0 df.fillna(method="bfill")df.fillna(value = 0)df.fillna(value="randomstringvalues) 。我还尝试使用df.dropna()删除 NaN 值
不用说,当我尝试使用 NaN 值运行我的代码时,它无法工作。
我有两个问题。
第一个,如何处理我的 NaN 值,以便将来不会看到此错误( TypeError: Expected binary or unicode string, got 618.0 )?
第二个,我怎样才能摆脱这个错误并将我的数据集快速实施到这个 model 中?
PS:我很肯定我没有打错字。

我的猜测是您的数据中有一些非 unicode 字符。 非 unicode 字符是这样的: �ä

任何不是字母、数字或符号的东西。 您在这里有两个选择,查找所有这些字符并将它们替换为其他字符或删除它们。

或者您可以在读取 csv 文件时使用正确的编码。 pandas.read_csv

data = pandas.read_csv(myfile, encoding='utf-8', quotechar='"', delimiter=',') 

我看不到你的数据,所以这是一个猜测。 打开您的.csv 文件并搜索 618.0。 也许,某些行没有所有预期值,并且解析器正在尝试加载预期分类值的数值。 另一种查看您是否有“格式”问题的方法是在 excel 中打开 csv 并查看所有行的格式是否正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM