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()
运行结果: