首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python分类器

Python分类器
EN

Stack Overflow用户
提问于 2019-12-05 20:36:17
回答 1查看 54关注 0票数 0

我开始了python的职业生涯。我不能处理这个问题

我的代码:

代码语言:javascript
复制
import pandas as pd
import numpy as np
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
dFrame=pd.read_csv('...', header='infer', sep=" ")
#importing the necessary packages
from sklearn.model_selection import train_test_split
feature_cols=["Attr1", "Attr2", "Attr3", "Attr4"]
X=dFrame[feature_cols]
y=dFrame['Decision']
y=dFrame.Decision
X_train, X_test, y_train, y_test = train_test_split(X,y)
NB = GaussianNB()
NB.fit(X_train, y_train)
y_predict = NB.predict(X_test)
print("Accuracy NB: {:.2f}".format(NB.score(X_test, y_test)))

print("Means:", NB. theta_ )
print("Standard deviations:" , NB.sigma_ )
print(X_train)
print(y_train)
def plot_ellipse ( ax , mu , sigma , color = "k" , label = None ):

    import matplotlib.patches
# Compute eigenvalues and associated eigenvectors
    vals , vecs =np.linalg.eigh(sigma)

    # Compute "tilt" of ellipse using first eigenvector
    x , y = vecs[:, 0]
    theta = np.degrees (np.arctan2 ( y , x ))

 # Eigenvalues give length of ellipse along each eigenvector
    w,h = 2 * np.sqrt ( vals )
    ax . tick_params ( axis = 'both' , which = 'major' , labelsize = 20 )
    ellipse = matplotlib.patches.Ellipse ( mu, w, h, theta, color = color, label = label )  # color="k")
    ellipse . set_clip_box ( ax . bbox )
    ellipse . set_alpha ( 0.2 )
    ax . add_artist ( ellipse )
    return ellipse

错误文本:

代码语言:javascript
复制
Traceback (most recent call last):
  File "D:/PhytonProjekty/untitled/proba.py", line 47, in <module>
    plot_ellipse (plt.gca (),NB.theta_[0], np.identity(2)*NB.sigma_[0],color = "red" )
ValueError: operands could not be broadcast together with shapes (2,2) (4,) 

数据表:enter image description here感谢您的帮助

EN

回答 1

Stack Overflow用户

发布于 2019-12-05 21:03:16

np.identity(2)*NB.sigma_[0]中出现错误。

你有四个特征,所以,NB.sigma_[0]的形状是(4,)。您希望使用np.identity(2)将其与shape (2, 2)的单位矩阵相乘。因此,矩阵乘法不起作用,因此出现错误。

你想做像这样的事情吗?

代码语言:javascript
复制
np.identity(4)*NB.sigma_[0]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59195341

复制
相关文章

相似问题

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