我一直得到这个错误的任何赞赏
我一直在尝试,但出现了值错误https://colab.research.google.com/drive/1jEmsG9WWRpUmuU92URD0PxtzWkpETlY3?usp=sharing
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import datetime
df = pd.read_csv('/content/covid_19_india.csv')
df['split'] = np.random.randn(df.shape[0], 1)
msk = np.random.rand(len(df)) <= 0.7
training = df[msk]
test = df[~msk]
xtrain = training.drop('Sno', axis=1)
ytrain = training.loc[:, 'Sno']
xtest = test.drop('Sno', axis=1)
ytest = test.loc[:, 'Sno']
model = GaussianNB()
model.fit(xtrain, ytrain)
pred = model.predict(xtest)
mat = confusion_matrix(pred, ytest)
names = np.unique(pred)
sns.heatmap(mat, square=True, annot=True, fmt='d', cbar=False,
xticklabels=names, yticklabels=names)
plt.xlabel('Truth')
plt.ylabel('Predicted')
ValueError Traceback(最近的调用最后)in()31 32#训练模型---
6帧/usr/local/lib/python3。6/地区包/numpy/core/_asarray。按顺序排列的py(a、D类型、顺序)83 84“---
ValueError:无法将字符串转换为浮点:“30/01/20”
有什么帮助吗
错误消息称“30/01/20”字符串无法转换为浮动。
因此,您的DataFrame似乎包含一个带有日期的列。
请注意,当read_csv读取源数据时,它试图将数字列转换为int或浮动,但其他列(不能以这种方式转换)保留为字符串,这些列具有对象数据类型。
从标识包含日期的列开始。然后,要将此列转换为datetime,最早在读取阶段,将parse_dates参数传递给read_csv,并提供要转换的列名列表。
那么至少转换为float应该没有问题。
正如在评论和这个问题的另一个答案中提到的那样,数据集中有一列日期格式为字符串。你有几个选择。
为了便于讨论,假设您的日期位于名为df['dates']
的列中。如果您不想使用date列,只需删除它即可。
df.drop('date', axis=1)
另一个选项是将此列转换为日期时间格式。这可以使用应用()
和datetime.datetime.strptime
来完成。如果你是一个有抱负的数据科学家,你应该阅读,然后加入书签https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior.我保证,这很方便。
from datetime import datetime
df['date'] = df['date'].apply(lambda d:datetime.strptime(d, '%m/%d/%y')