gpt4 book ai didi

python - 值错误 : First argument must be a sequence ----> Scatter Plot Python

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:57 25 4
gpt4 key购买 nike

我目前正在努力绘制我的线性回归输出。我发现了类似的问题,建议确保数据类型设置为 int。我已确保将其合并到我的代码中。

我已经多次检查代码,结构对我来说似乎很合理。我愿意接受任何和所有反馈!非常感谢您的帮助!

Please note that the columns (Accident_Severity and Number_of_Casualties) are simply numbers. (i.e. The severity of the accident was 3 and 1 casualty was involved).

----------------步骤1--------------------

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
%pylab inline
import matplotlib.pyplot as plt

----------------步骤2--------------------

raw_data = pd.read_csv("/Users/Maddco12/Desktop/1-6m-accidents-traffic-flow-over-16-years/accidents_2005_to_2007.csv")
dtype={'Number_of_Casualties': int,'Accident_Severity': int}
raw_data.head(4)

----------------步骤3--------------------

filtered_data = raw_data[~np.isnan(raw_data["Accident_Severity"])] #removes rows with NaN in them
filtered_data.head(4)

filtered_data = raw_data[~np.isnan(raw_data["Number_of_Casualties"])] #removes rows with NaN in them
filtered_data.head(4)

----------------Step4--------------------

npMatrix = np.matrix(filtered_data)
X, Y = npMatrix[:,0], npMatrix[:,1]
mdl = LinearRegression().fit(filtered_data[['Number_of_Casualties']],
filtered_data.Accident_Severity)
m = mdl.coef_[0]
b = mdl.intercept_
print "formula: y = {0}x + {1}".format(m, b)

----------------Step5--------------------(我在这里得到值错误)

plt.scatter(X,Y, color='blue')
plt.plot([0,100],[b,m*100+b],'r')
plt.title('Linear Regression Example', fontsize = 20)
plt.xlabel('Number of Casualties', fontsize = 15)
plt.ylabel('Accident Severity', fontsize = 15)
plt.show()

错误如下---->

ValueError                                Traceback (most recent call last)
<ipython-input-10-5bf84a35de3d> in <module>()
----> 1 plt.scatter(X,Y, color='blue')
2 plt.plot([0,100],[b,m*100+b],'r')
3 plt.title('Linear Regression Example', fontsize = 20)
4 plt.xlabel('Number of Casualties', fontsize = 15)
5 plt.ylabel('Accident Severity', fontsize = 15)

/Users/Maddco12/Documents/Python/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
3256 vmin=vmin, vmax=vmax, alpha=alpha,
3257 linewidths=linewidths, verts=verts,
-> 3258 edgecolors=edgecolors, data=data, **kwargs)
3259 finally:
3260 ax.hold(washold)

/Users/Maddco12/Documents/Python/anaconda/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
1817 warnings.warn(msg % (label_namer, func.__name__),
1818 RuntimeWarning, stacklevel=2)
-> 1819 return func(ax, *args, **kwargs)
1820 pre_doc = inner.__doc__
1821 if pre_doc is None:

/Users/Maddco12/Documents/Python/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
3836
3837 # c will be unchanged unless it is the same length as x:
-> 3838 x, y, s, c = cbook.delete_masked_points(x, y, s, c)
3839
3840 scales = s # Renamed for readability below.

/Users/Maddco12/Documents/Python/anaconda/lib/python2.7/site-packages/matplotlib/cbook.pyc in delete_masked_points(*args)
1846 return ()
1847 if (is_string_like(args[0]) or not iterable(args[0])):
-> 1848 raise ValueError("First argument must be a sequence")
1849 nrecs = len(args[0])
1850 margs = []

ValueError: First argument must be a sequence.

最佳答案

我建议在绘制之前检查 X 和 Y 值。您的其余代码看起来很简单,因此问题很可能就在那里。

散点图需要一组 X 和 Y 值

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html

试试这个,看看它是否有效

plt.scatter([X],[Y], color='blue')

关于python - 值错误 : First argument must be a sequence ----> Scatter Plot Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959006/

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