我是机器学习的新手,并开始在 Kaggle 上解决泰坦尼克号问题。我编写了一个简单的算法来预测测试数据的结果。
我的问题/困惑是,每次我使用相同的数据集和相同的步骤执行算法时,得分都会发生变化(代码中的最后一条语句)。我无法理解这种行为?
代码:
# imports
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
# load data
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
results = pd.read_csv('gender_submission-orig.csv')
# prepare training and test dataset
y = train['Survived']
X = train.drop(['Survived', 'SibSp', 'Ticket', 'Cabin', 'Embarked', 'Name'], axis=1)
test = test.drop(['SibSp', 'Ticket', 'Cabin', 'Embarked', 'Name'], axis=1)
y_test = results['Survived']
X = pd.get_dummies(X)
test = pd.get_dummies(test)
# fill the missing values
age_median = X['Age'].median()
fare_median = X['Fare'].median()
X['Age'] = X['Age'].fillna(age_median)
test['Age'].fillna(age_median, inplace=True)
test['Fare'].fillna(fare_median, inplace=True)
# train the classifier and predict
clf = DecisionTreeClassifier()
clf.fit(X, y)
predict = clf.predict(test)
# This is the score which changes with execution.
print(round(clf.score(test, y_test) * 100, 2))
慕丝7291255
相关分类