值错误:第一个参数必须是序列>散点图Python

2024-09-23 00:17:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在努力绘制我的线性回归输出。我发现了一个类似的问题,建议确保数据类型被设置为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步----------------

^{pr2}$

---------------第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)

---------------第4步------------------

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)

---------------第5步----------------(我在这里得到值错误)

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.

Tags: ofinimportnumberdatamatplotlibasargs
2条回答

也许你应该检查一下你的csv文件。如果使用旧的Excel版本生成它,可能会出现这种错误。我解决了这个问题,将我的csv加载到Googlespreadsheets,然后再次将其导出为(更好的)csv文件。一些csv文件类型和python的某些版本似乎有一种奇怪的不兼容性。这里有一个关于这个问题的有价值的讨论:Excel to CSV with UTF8 encoding。希望有帮助。在

我建议在绘制X和Y值之前检查它们。代码的其余部分都向前看,所以问题很可能就在那里。在

散点图需要X和Y的值数组

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

试试这个看看能不能用

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

相关问题 更多 >