gpt4 book ai didi

python - 如何绘制线性回归?

转载 作者:行者123 更新时间:2023-12-01 01:02:29 31 4
gpt4 key购买 nike

我想学习如何使用Plotly和Python进行数据分析。我一直用this website作为引用。

我当前的代码如下所示:

from plotly import tools
import plotly as py
import plotly.graph_objs as go

py.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)

p1 = go.Scatter(x=X_test,
y=y_test,
mode='markers',
marker=dict(color='black')
)

p2 = go.Scatter(x=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)

但是,我的输出看起来像

this

如果我逐行关注该网站,我会得到以下结果:

from plotly import tools
import plotly as py
import plotly.graph_objs as go

py.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)

但是会产生以下错误:

---------------------------------------------------------------------------
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)
<ipython-input-84-5895927e91e2> in <module>
21 return k
22
---> 23 p1 = go.Scatter(x=data_to_plotly(X_test),
24 y=y_test,
25 mode='markers',

<ipython-input-84-5895927e91e2> in data_to_plotly(x)
17
18 for i in range(0, len(x)):
---> 19 k.append(x[i][0])
20
21 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

我是 Plotly 新手。我该如何解决这个问题?

编辑:我的 X_test 看起来像这样:

X_test

最佳答案

我看到X_train是一个数据框,Plotly实际上对Pandas非常友好,plotly的pandas example gallery中有几个例子因此您不必在诸如 data_to_plotly 之类的函数之间进行处理(遗憾的是该教程看起来相当过时)。在这种情况下,散点图应该类似于

p1 = go.Scatter(x=X_test['Explained by: GDP per capita'],
y=y_test, # Assuming y_test is a numpy array or pandas series
# if it is also a dataframe you have to specify the column
mode='markers',
marker=dict(color='black')
)

p2 = go.Scatter(x=X_test['Explained by: GDP per capita'],
y=regr.predict(X_test),
mode='lines',
line=dict(color='blue', width=3)
)

关于python - 如何绘制线性回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663788/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com