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

标签 python pandas numpy matplotlib

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

相关文章:

python - Django - 在不同通用 View 中设置和访问 session 变量

python - 从 Pandas 中引用 CSV

python - Panda.run_sql_query 在 .. 结构中的 .. 位置?

python - TA-Lib numpy "AssertionError: real is not double"

python - 如何从 l=string 中提取 0207 而不是 207?

python - python : cannot instantiate more than one or two variables (SyntaxError raised)

python - 替换 Pandas 数据框中的值(不包括缺失值)

python - 删除 Python Pandas 中多列中的所有重复行

javascript - tensorflow.js 有没有办法输入一个以 -1 作为形状值之一的形状

python - 如何检查连续相同的值和值的计数同时出现 pandas