python-3.x - 如何为更大的数据集运行此功能(9 个功能)
问题描述
我是 python 新手。在我的代码中,我试图从头开始实现支持向量机。该代码以前有 2 个功能和 2 个类(1 和 -1),有 6 个实例(每个类),并且运行良好。我正在尝试使用 6 个实例(每个类)为 9 个功能和 2 个类(1 和 -1)实现相同的代码,它给了我一个值错误,我似乎无法修复它。我正在使用 Python 3.6.3 版本,感谢您的帮助。
#This is my dictionary/dataset
data_dict = {-1: np.array([[1, 7, 4, 1, 9, 1, 5, 6, 7],
[2, 8, 6, 0, 8, 6, 8, 5, 2],
[3, 8, 7, 3, 2, 5, 4, 4, 8], ]),
1: np.array([[5, 1, 8, 2, 6, 4, 0, 2, -3],
[6, -1, 5, -2, 6, -3, 0, 5, 3],
[7, 3, 0, 4, 10, -6, 9, 8, 2], ])}
#Call to the function
svm = Support_Vector_Machine()
svm.fit(data=data_dict)
#Function fit
def fit(self, data):
self.data = data
#Some more code here
#w_t and b intialized here
for i in self.data:
for xi in self.data[i]:
yi = i
if not yi * (np.dot(w_t, xi) + b) >= 1:
found_option = False
# print(xi,':',yi*(np.dot(w_t,xi)+b))
if found_option:
opt_dict[np.linalg.norm(w_t)] = [w_t, b]
错误信息:
在模块中
svm.fit(data=data_dict)
合身
if not yi * (np.dot(w_t, xi) + b) >= 1:
ValueError: shapes (2,) and (9,) not aligned: 2 (dim 0) != 9 (dim 0)
解决方案
谢谢你。我想到了。问题在于数组 w_t 的大小和形状。w_t 有 2 个元素,我试图将它与 9 个元素的数组相乘。我修复了它,现在它工作正常。
推荐阅读
- html - 如何使用弹性包装?
- python - Django 中间件 - 排除特定路径
- git - 如何在 git 中维护 2 个仅在 1 个提交中不同的分支
- c++ - VSCode:Visual Studio Code 中不需要的 C++ 关键字自动完成
- docker - 如何在一个节点上运行两个 Zookeeper 服务器
- php - 从作业表中删除时 Laravel 队列死锁
- azure - 无法启用 Azure 更改跟踪/库存(配置管理)
- ionic-framework - 阻止 ActionSheet 在 Ionic 3 中关闭
- flutter - 如何在行和文本之间使用空格?
- hdf5 - 无法通过切片在 hdf5 文件中设置数据集名称