我正在尝试制作一个使用 CSV 文件数据预测癌症检测的项目,我已将癌症 CSV 数据分成 2 个文件,分别命名为 X_data.csv 和 Y_data.csv 。请关注下面有兴趣帮助我解决问题的代码,
导入所有需要的库和子库:
import tensorflow as tf
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping
from keras.utils import to_categorical
import keras
import numpy as np
from keras.layers import BatchNormalization
from keras.layers import Dropout
from keras import regularizers
import pandas as pd
import sklearn
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import matplotlib
from matplotlib import pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina'
导入输入 (x) 和输出 (y) 数据,并将它们分配给 df1 和 df2:
df1 = pd.read_csv('X_data.csv')
df2 = pd.read_csv('Y_data.csv')
缩放输入数据:
df1 = preprocessing.scale(df1) //I faced error here
缩放误差如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-aec70d746687> in <module>
1 # Scale input data
2
----> 3 df1 = preprocessing.scale(df1)
~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
71 FutureWarning)
72 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73 return f(**kwargs)
74 return inner_f
75
~/anaconda3/lib/python3.8/site-packages/sklearn/preprocessing/_data.py in scale(X, axis, with_mean, with_std, copy)
139
140 """ # noqa
--> 141 X = check_array(X, accept_sparse='csc', copy=copy, ensure_2d=False,
142 estimator='the scale function', dtype=FLOAT_DTYPES,
143 force_all_finite='allow-nan')
~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
71 FutureWarning)
72 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73 return f(**kwargs)
74 return inner_f
75
~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
597 array = array.astype(dtype, casting="unsafe", copy=False)
598 else:
--> 599 array = np.asarray(array, order=order, dtype=dtype)
600 except ComplexWarning:
601 raise ValueError("Complex data not supported\n"
~/anaconda3/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84 """
---> 85 return array(a, dtype, copy=False, order=order)
86
87
ValueError: could not convert string to float: 'discrete'
答案 0 :(得分:2)
最后一行字面上说明了问题所在。
ValueError: could not convert string to float: 'discrete'
。如果您打印您的数据 (df1.head()
),您会看到 有一些字符串数据 就像错误表明 preprocessing
函数无法处理强>.
因此您必须先执行数据清理(将字符串转换为 int/float,处理任何丢失的数据等)。您可以从 sklearn 或 LabelEncoder()
中寻找诸如 one hot encoder
之类的函数来处理您的字符串到 int 问题。