AttributeError: 'NoneType' object has no attribute 'sqrt'

Traceback (most recent call last):
  File "C:\Users\appleyuchi\Desktop\todolist\LDA\datagenerate.py", line 30, in
    plt.scatter(X_new[:, 0], X_new[:, 1],marker='o',c=y)
  File "D:\software\python27\lib\site-packages\matplotlib\pyplot.py", line 3434, in scatter
    edgecolors=edgecolors, data=data, **kwargs)
  File "D:\software\python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 2352, in scatter
    patches = Axes.scatter(self, xs, ys, s=s, c=c, *args, **kwargs)
  File "D:\software\python27\lib\site-packages\matplotlib\__init__.py", line 1898, in inner
    return func(ax, *args, **kwargs)
  File "D:\software\python27\lib\site-packages\matplotlib\axes\_axes.py", line 4034, in scatter
    alpha=alpha
  File "D:\software\python27\lib\site-packages\matplotlib\collections.py", line 907, in __init__
    self.set_sizes(sizes)
  File "D:\software\python27\lib\site-packages\matplotlib\collections.py", line 880, in set_sizes
    scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor

AttributeError: 'NoneType' object has no attribute 'sqrt'

代码来自:https://blog.csdn.net/lhz76ttw1u/article/details/60768981

错误重现的代码:

#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np  
np.set_printoptions(threshold=np.inf)  

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# %matplotlib inline
from sklearn.datasets.samples_generator import make_classification
X, y = make_classification(n_samples=1000, n_features=3, n_redundant=0, n_classes=3, n_informative=2,
                           n_clusters_per_class=1,class_sep =0.5, random_state =10)
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=30, azim=20)
plt.scatter(X[:, 0], X[:, 1], X[:, 2],marker='o',c=y)
print"type(X)=",type(X)

# plt.show()#弹出一个绘制好的空间图形
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(X)
print pca.explained_variance_ratio_
print pca.explained_variance_
X_new = pca.transform(X)
print"-"*30
print"type(X_new)=",type(X_new)
print "X_new\n=",X_new
plt.scatter(X_new[:, 0], X_new[:, 1],marker='o',c=y)
plt.show()
解决方案,注释掉Axes3D那句话,注释后完整代码如下:
#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np  
np.set_printoptions(threshold=np.inf)  

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# %matplotlib inline
from sklearn.datasets.samples_generator import make_classification
X, y = make_classification(n_samples=1000, n_features=3, n_redundant=0, n_classes=3, n_informative=2,
                           n_clusters_per_class=1,class_sep =0.5, random_state =10)
fig = plt.figure()
# ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=30, azim=20)
plt.scatter(X[:, 0], X[:, 1], X[:, 2],marker='o',c=y)
print"type(X)=",type(X)

# plt.show()#弹出一个绘制好的空间图形
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(X)
print pca.explained_variance_ratio_
print pca.explained_variance_
X_new = pca.transform(X)
print"-"*30
print"type(X_new)=",type(X_new)
print "X_new\n=",X_new
plt.scatter(X_new[:, 0], X_new[:, 1],marker='o',c=y)
plt.show()
运行结果: AttributeError: 'NoneType' object has no attribute 'sqrt'_第1张图片

你可能感兴趣的:(Python)