【发布时间】:2018-03-21 13:08:00
【问题描述】:
我使用以下代码。我想为相同的随机种子获得相同的结果。我使用相同的随机种子(在这种情况下为 1)并得到不同的结果。 代码如下:
import pandas as pd
import numpy as np
from random import seed
# Load scikit's random forest classifier library
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
seed(1) ### <-----
file_path = 'https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data'
dataset2 = pd.read_csv(file_path, header=None, sep=',')
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
#Encoding
y = le.fit_transform(dataset2[60])
dataset2[60] = y
train, test = train_test_split(dataset2, test_size=0.1)
y = train[60]
y_test = test[60]
clf = RandomForestClassifier(n_jobs=100, random_state=0)
features = train.columns[0:59]
clf.fit(train[features], y)
# Apply the Classifier we trained to the test data
y_pred = clf.predict(test[features])
# Decode
y_test_label = le.inverse_transform(y_test)
y_pred_label = le.inverse_transform(y_pred)
from sklearn.metrics import accuracy_score
print (accuracy_score(y_test_label, y_pred_label))
# Two following results:
# 0.761904761905
# 0.90476190476
【问题讨论】:
-
据我所知,
train, test = train_test_split(dataset2, test_size=0.1)行没有随机种子集。
标签: python-2.7 random seed