我正在使用colab运行我的代码,但似乎无法使用plotly运行我的绘图代码。当colab运行此单元格时,它将跳过并运行下一个单元格。从colab下载文件并在自己的计算机上运行后,jupyter notebook将显示以下错误: 保存操作成功,但是笔记本电脑似乎无效。验证错误为。
但是,如果我创建一个新的jupyter笔记本并运行它,它将起作用。
以下是我的情节代码:
from sklearn.decomposition import PCA
from sklearn import preprocessing
%matplotlib inline
def plot_explained_variance(pca):
import plotly
from plotly.graph_objs import Bar, Line
from plotly.graph_objs import Scatter, Layout
from plotly.graph_objs.scatter import Marker
from plotly.graph_objs.layout import XAxis, YAxis
plotly.offline.init_notebook_mode() # run at the start of every notebook
explained_var = pca.explained_variance_ratio_
cum_var_exp = np.cumsum(explained_var)
plotly.offline.iplot({
"data": [Bar(y=explained_var, name='individual explained variance'),
Scatter(y=cum_var_exp, name='cumulative explained variance')
],
"layout": Layout(xaxis=XAxis(title='Principal components'), yaxis=YAxis(title='Explained variance ratio'))
})
min_max_scaler = preprocessing.MinMaxScaler()
standard_scaler = preprocessing.StandardScaler()
X_scaled = standard_scaler.fit_transform(X)
pca_test = PCA(n_components=X.shape[1])
X_pca_test = pca_test.fit(X_scaled)
plot_explained_variance(pca_test)
非常感谢您。