[英]ValueError: could not convert string to float: '���'
我有一個(2M,23)維numpy
數組X
它的dtype <U26
,即26個字符的unicode字符串。
array([['143347', '1325', '28.19148936', ..., '61', '0', '0'],
['50905', '0', '0', ..., '110', '0', '0'],
['143899', '1325', '28.80434783', ..., '61', '0', '0'],
...,
['85', '0', '0', ..., '1980', '0', '0'],
['233', '54', '27', ..., '-1', '0', '0'],
['���', '�', '�����', ..., '�', '��', '���']], dtype='<U26')
當我將其轉換為float數據類型時,使用
X_f = X.astype(float)
我收到如上所述的錯誤。 我正在嘗試找到如何解決' '的字符串格式錯誤。
它是什么意思(它叫什么?),我該如何解決該錯誤?
編輯:關於如何讀取數據的信息:
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.sql.functions import col
def loading_data(dataset):
dataset=sql_sc.read.format('csv').options(header='true', inferSchema='true').load(dataset)
# #changing column header name
dataset = dataset.select(*[col(s).alias('Label') if s == ' Label' else s for s in dataset.columns])
#to change datatype
dataset=dataset.drop('External IP')
dataset = dataset.filter(dataset.Label.isNotNull())
dataset=dataset.filter(dataset.Label!=' Label')#filter Label from label
print(dataset.groupBy('Label').count().collect())
return dataset
# invoking
ds_path = '../final.csv'
dataset=loading_data(ds_path)
type(dataset)
pyspark.sql.dataframe.DataFrame
import numpy as np
np_dfr = np.array(data_preprocessing(dataset).collect())
X = np_dfr[:,0:22]
Y = np_dfr[:,-1]
>> X
array([['143347', '1325', '28.19148936', ..., '61', '0', '0'],
['50905', '0', '0', ..., '110', '0', '0'],
['143899', '1325', '28.80434783', ..., '61', '0', '0'],
...,
['85', '0', '0', ..., '1980', '0', '0'],
['233', '54', '27', ..., '-1', '0', '0'],
['���', '�', '�����', ..., '�', '��', '���']], dtype='<U26')
這意味着string(...)維度在圖中未固定,並且在運行調用之間可能會有所不同。問號符號表示tf.TensorShape
Session.run或eval返回的任何張量都是NumPy數組。
>>> print(type(tf.Session().run(tf.constant([1,2,3]))))
<class 'numpy.ndarray'>
要么:
>>> sess = tf.InteractiveSession()
>>> print(type(tf.constant([1,2,3]).eval()))
<class 'numpy.ndarray'>
或者,等效地:
>>> sess = tf.Session()
>>> with sess.as_default():
>>> print(type(tf.constant([1,2,3]).eval()))
<class 'numpy.ndarray'>
不 )通過Session.run或EVAL(返回的任何張量是一個NumPy的陣列。 例如,稀疏張量作為SparseTensorValue返回:
>>> print(type(tf.Session().run(tf.SparseTensor([[0, 0]],[1],[1,2]))))
<class 'tensorflow.python.framework.sparse_tensor.SparseTensorValue'>
雖然不是最好的解決方案,但我通過將其轉換為pandas數據框並進行合作而獲得了一些成功。
# convert X into dataframe
X_pd = pd.DataFrame(data=X)
# replace all instances of URC with 0
X_replace = X_pd.replace('�',0, regex=True)
# convert it back to numpy array
X_np = X_replace.values
# set the object type as float
X_fa = X_np.astype(float)
array([['85', '0', '0', '1980', '0', '0'],
['233', '54', '27', '-1', '0', '0'],
['���', '�', '�����', '�', '��', '���']], dtype='<U5')
array([[ 8.50e+01, 0.00e+00, 0.00e+00, 1.98e+03, 0.00e+00, 0.00e+00],
[ 2.33e+02, 5.40e+01, 2.70e+01, -1.00e+00, 0.00e+00, 0.00e+00],
[ 0.00e+00, 0.00e+00, 0.00e+00, 0.00e+00, 0.00e+00, 0.00e+00]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.