当我运行SVM时,即使使用固定的random_state=42
,我也会得到不同的结果。
我有10个类和200个例子的数据集。我的数据集dim_dataset=(200,2048)
这是我的代码:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn import svm
import random
random.seed(42)
def shuffle_data(x,y):
idx = np.random.permutation(len(x))
x_data= x[idx]
y_labels=y[idx]
return x_data,y_labels
d,l=shuffle_data(dataset,true_labels) # dim_d=(200,2048) , dim_l=(200,)
X_train, X_test, y_train, y_test = train_test_split(d, l, test_size=0.30, random_state=42)
# hist intersection kernel
gramMatrix = histogramIntersection(X_train, X_train)
clf_gram = svm.SVC(kernel='precomputed', random_state=42).fit(gramMatrix, y_train)
predictMatrix = histogramIntersection(X_test, X_train)
SVMResults = clf_gram.predict(predictMatrix)
correct = sum(1.0 * (SVMResults == y_test))
accuracy = correct / len(y_test)
print("SVM (Histogram Intersection): " + str(accuracy) + " (" + str(int(correct)) + "/" + str(len(y_test)) + ")")
# libsvm linear kernel
clf_linear_kernel = svm.SVC(kernel='linear', random_state=42).fit(X_train, y_train)
predicted_linear = clf_linear_kernel.predict(X_test)
correct_linear_libsvm = sum(1.0 * (predicted_linear == y_test))
accuracy_linear_libsvm = correct_linear_libsvm / len(y_test)
print("SVM (linear kernel libsvm): " + str(accuracy_linear_libsvm) + " (" + str(int(correct_linear_libsvm)) + "/" + str(len(y_test)) + ")")
# liblinear linear kernel
clf_linear_kernel_liblinear = LinearSVC(random_state=42).fit(X_train, y_train)
predicted_linear_liblinear = clf_linear_kernel_liblinear.predict(X_test)
correct_linear_liblinear = sum(1.0 * (predicted_linear_liblinear == y_test))
accuracy_linear_liblinear = correct_linear_liblinear / len(y_test)
print("SVM (linear kernel liblinear): " + str(accuracy_linear_liblinear) + " (" + str(
int(correct_linear_liblinear)) + "/" + str(len(y_test)) + ")")
我的代码出了什么问题?
答案 0 :(得分:0)
使用numpy.random.seed()
代替简单random.seed
,如下所示:
np.random.seed(42)
Scikit内部使用numpy生成随机数,因此只做random.seed不会影响numpy的行为,这仍然是随机的。
请参阅以下链接以便更好地理解: