【发布时间】:2018-12-15 16:57:33
【问题描述】:
我已经在“Rocks and Mines”数据集上训练了一个分类器 (https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data) 在计算准确度分数时,它似乎总是非常准确(输出为 1.0),我很难相信。是我犯了什么错误,还是朴素贝叶斯这么强大?
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data'
data = urllib.request.urlopen(url)
df = pd.read_csv(data)
# replace R and M with 1 and 0
m = len(df.iloc[:, -1])
Y = df.iloc[:, -1].values
y_val = []
for i in range(m):
if Y[i] == 'M':
y_val.append(1)
else:
y_val.append(0)
df = df.drop(df.columns[-1], axis = 1) # dropping column containing 'R', 'M'
X = df.values
from sklearn.model_selection import train_test_split
# initializing the classifier
clf = GaussianNB()
# splitting the data
train_x, test_x, train_y, test_y = train_test_split(X, y_val, test_size = 0.33, random_state = 42)
# training the classifier
clf.fit(train_x, train_y)
pred = clf.predict(test_x) # making a prediction
from sklearn.metrics import accuracy_score
score = accuracy_score(pred, test_y)
# printing the accuracy score
print(score)
X 是输入,y_val 是输出(我已将“R”和“M”转换为 0 和 1)
【问题讨论】:
-
你能用你把数据分成 X 和 y_val 的部分更新代码吗?
-
@Mufeed 当然,我会更新帖子
-
我的准确度得分为 0.6666
-
删除 train_test_split() 中的随机状态 =42 或给出其他随机值。
-
恭喜您在 SO 中提出第一个问题,MCVE;现在,既然答案解决了您的问题,请接受 - 请参阅What should I do when someone answers my question?
标签: python scikit-learn naivebayes