SVM sklearn 上的随机种子产生不同的结果

2024-03-23

当我运行 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)) + ")")

我的代码有什么问题吗?


Use numpy.random.seed() https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.seed.html而不是简单的random.seed像这样:

np.random.seed(42)

Scikit 内部使用 numpy 生成随机数,因此仅执行 random.seed 不会影响 numpy 的行为,它仍然是随机的。

请参阅以下链接以更好地理解:

  • 我应该使用“random.seed”或“numpy.random.seed”来控制“scikit-learn”中的随机数生成吗? https://stackoverflow.com/questions/31057197/should-i-use-random-seed-or-numpy-random-seed-to-control-random-number-gener
  • http://scikit-learn.org/stable/developers/utilities.html#validation-tools http://scikit-learn.org/stable/developers/utilities.html#validation-tools
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SVM sklearn 上的随机种子产生不同的结果 的相关文章

随机推荐

  • IntellijIdea 中未解决的参考 Kotlinx。即使添加库后

    我只是想尝尝 Kotlin 协程 不幸的是 我无法使用该库kotlinx in IntelliJ 我已经通过下载了该库Maven 尝试使缓存无效 重建项目 尝试在新项目中使用它 Even IntelliJ正在建议kotlinx在自动完成中
  • 有没有办法在 Windows 中安装 therubyracer?

    有没有办法安装 therubyracer gem 即在 Windows 中运行 RoR 项目所需的方法 我尝试过正常方式 但不行 然后我尝试安装 cygwin 然后尝试安装 gem 但仍然没有运气 有人在windows下成功安装过这个吗 我
  • Leaflet OSM:多边形上的中心地图视图

    我想生成一个 html 文件 包括Leaflet库来显示OpenStreetMap用多边形查看 地图上的多边形应居中 为此 我遵循this https github com Leaflet Leaflet issues 1196讨论 但我仍
  • 上标下划线随文本向上移动

    我有类似以下的 HTML 代码 div We have winner of 1 div
  • JVM 源代码中的“intrinsify”是什么意思?

    intrinsify 是否意味着 JVM 的源代码有些 保守 但 JIT 编译器可以在 JVM 预热时进行一些优化 例如 UNSAFE ENTRY void Unsafe SetOrderedObject JNIEnv env jobjec
  • Three.js - 从点缩放圆柱体

    是否可以从特定点开始增加 Y 轴上圆柱体的比例 与圆柱体从其原点向上和向下生长到新比例不同 它只是像条形图一样从顶部向上 向下生长 当前代码 function animate render cylinder scale y 0 1 requ
  • PHP代码格式化程序/美化程序和一般的PHP美化[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 你知道有什么好的工具可以很好地格式化凌乱的 php 代码吗 最好是 Aptana Eclipse 的脚
  • 将 Spark 中的字符串数组转换为字节数组并使用 UDF 将其检索回来

    我正在尝试将 Spark 中的字符串数组转换为字节数组 然后将字节数组重新转换为字符串数组 但是 我没有按照我的预期取回字符串数组 这是代码 UDFs for converting Array String to byte array an
  • OrientDB地理定位

    我正在使用 OrientDB for NoSQL 数据库 但我不知道如何准确查询地理位置 我已阅读具体文档 东方数据库函数 https github com orientechnologies orientdb wiki SQL Where
  • python 中是否有终止线程的规范方法?

    我想在 python 中强制终止线程 我不想设置事件并等待线程检查它并退出 我正在寻找一个简单的解决方案 例如kill 9 这是否可以在没有肮脏的黑客 例如使用私有方法操作等 的情况下做到这一点 如果您不介意代码运行速度慢十倍左右 则可以使
  • 如何从 CodeIgniter 中的 URL 中删除“index.php”?

    我该如何删除index php从我的URL http en wikipedia org wiki Uniform Resource Locator在代码点火器中 我删除了index php从我的配置文件中 我已经运行了我的rewrite m
  • 离线Python脚本中的OAuth和redirect_uri

    我目前正在尝试编写一个 Python 脚本 该脚本将使用 Deviantart 的 API 自动随机播放我最喜欢的歌曲 为此 我需要首先登录我的脚本 Deviantart 使用 OAuth2 身份验证 这需要一个 redirect uri
  • 类型错误:$ (...).bootstrapTable 不是函数

    table class table table bordered table striped table hover table
  • 使用 Makecert 设置密钥使用属性

    是否可以使用 makecert 或我可以用来生成自己的测试证书的任何其他工具来设置密钥用法属性 我感兴趣的原因是 用于 BizTalk Server AS2 传输的证书需要使用数字签名的密钥进行签名 并使用数据加密或密钥加密进行加密 解密
  • Windows 中的 Nutch:无法设置路径权限

    我尝试在 Windows 计算机上使用 Solr 和 Nutch 但收到以下错误 Exception in thread main java io IOException Failed to set permissions of path
  • 如何使用 Google 地图 V2 删除单个标记? [复制]

    这个问题在这里已经有答案了 从地图上删除标记的唯一方法是明确的 但是它会清除地图上的所有标记 我只想删除单个标记或一组标记 我怎样才能做到这一点 添加标记后 可以获得其参考 Marker marker map addMarker The M
  • FastCGI超时错误MAMP Pro 4

    我研究了较旧的问题 https craftcms stackexchange com questions 4661 fastcgi error when accessing a local development site using ma
  • 如何找到RecyclerView中当前可见item的位置?

    我正在开发一个使用 RecyclerView 幻灯片显示图像的应用程序 我使用下面的代码水平滚动回收器视图 RecyclerView LayoutManager mLayoutManager new LinearLayoutManager
  • 使用 Jetty 以编程方式部署 servlet

    我有一个 servlet 希望使用 Jetty 以编程方式部署它 该 servlet 使用 Spring 并且它的 web xml 指向 Spring 上下文 XML 文件 正如您所期望的那样 目前 我只是尝试 Jetty 文档中的示例代码
  • SVM sklearn 上的随机种子产生不同的结果

    当我运行 SVM 时 即使使用固定的值 我也会得到不同的结果random state 42 我有 10 个类和一个包含 200 个示例的数据集 我的数据集的维度dim dataset 200 2048 这是我的代码 import numpy