首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为更大的数据集运行此函数(9个功能)

如何为更大的数据集运行此函数(9个功能)
EN

Stack Overflow用户
提问于 2019-03-26 02:47:33
回答 1查看 27关注 0票数 0

我是python的新手。在我的代码中,我试图从头开始实现支持向量机。代码之前有2个特性和2个类(1和-1),每个类有6个实例,并且运行良好。我试图为9个特性和2个类(1和-1)实现相同的代码,其中有6个实例(每个类),但它给了我一个值错误,我似乎无法修复它。我正在使用Python版本3.6.3谢谢您的帮助。

代码语言:javascript
复制
  #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]

错误消息:

在模块中

代码语言:javascript
复制
svm.fit(data=data_dict)

契合

代码语言:javascript
复制
if not yi * (np.dot(w_t, xi) + b) >= 1: 
ValueError: shapes (2,) and (9,) not aligned: 2 (dim 0) != 9 (dim 0)
EN

回答 1

Stack Overflow用户

发布于 2019-03-26 23:44:21

谢谢。我想通了。问题出在数组w_t的大小和形状上,w_t有2个元素,我试图将其与9个元素的数组相乘。我修复了它,现在它工作得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55344587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档