I am trying to use Plotly to plot a scatter plot with linear regression instead of matplotlib
using Python.
I followed this guide:
https://plot.ly/scikit-learn/plot-ols/
I ended up with the following code:
import plotly as py
import plotly.graph_objs as goimport numpy as np
from sklearn import linear_model
from plotly import toolspy.offline.init_notebook_mode(connected=True)
Create linear regression object
regr = linear_model.LinearRegression()
Train the model using the training sets
regr.fit(X_train,y_train)
def data_to_plotly(x):
k =for i in range(0, len(x)): k.append(x[i][0]) return k
p1 = go.Scatter(x=data_to_plotly(X_test),
y=y_test,
mode=βmarkersβ,
marker=dict(color=βblackβ)
)p2 = go.Scatter(x=data_to_plotly(X_test),
y=regr.predict(X_test),
mode=βlinesβ,
line=dict(color=βblueβ, width=3)
)layout = go.Layout(xaxis=dict(ticks=ββ, showticklabels=False,
zeroline=False),
yaxis=dict(ticks=ββ, showticklabels=False,
zeroline=False),
showlegend=False, hovermode=βclosestβ)fig = go.Figure(data=[p1, p2], layout=layout)
py.offline.iplot(fig)
I get the following error:
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2656 try:
β 2657 return self._engine.get_loc(key)
2658 except KeyError:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 0
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
in
27 return k
28
β> 29 p1 = go.Scatter(x=data_to_plotly(X_test),
30 y=y_test,
31 mode=βmarkersβ,in data_to_plotly(x)
23
24 for i in range(0, len(x)):
β> 25 k.append(x[i][0])
26
27 return k~\Anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key)
2925 if self.columns.nlevels > 1:
2926 return self._getitem_multilevel(key)
β 2927 indexer = self.columns.get_loc(key)
2928 if is_integer(indexer):
2929 indexer = [indexer]~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2657 return self._engine.get_loc(key)
2658 except KeyError:
β 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 0
How do i resolve this error?