- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在努力绘制我的线性回归输出。我发现了类似的问题,建议确保数据类型设置为 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/
我有几个 pandas 数据框。我想在单独的散点图中相互绘制几列,并将它们组合为图中的子图。我想相应地标记每个子图。我在让子图标签正常工作方面遇到了很多麻烦,直到我发现据我所知,有两种直接从数据帧进行
使用 Dask 分布式分散广播列表的合适方法是什么? 案例 1 - 包装列表: [future_list] = client.scatter([my_list], broadcast=True) 情况
使用 Dask 分布式分散广播列表的合适方法是什么? 案例 1 - 包装列表: [future_list] = client.scatter([my_list], broadcast=True) 情况
我有一个包含数组元素的列表: [array([2.40460915, 0.85513601]), array([1.80998096, 0.97406986]), array([2.14505475,
我想显示一些点。这是我的代码: plt.scatter(y[:,0],y[:,1],c=col) plt.show() 并作为 col我有: Col: [1 1 0 1 1 1 1 0 0 0 1
当我在 OSX Yosemite 上运行它时,生成的颜色条有奇怪的白线(见下图)。有什么方法可以生成没有这些难看的线条的颜色条吗? import pylab import numpy x = nump
我已成功绘制了两个图表,但当我绘制第三个图表时,出现无效语法错误。我是否遗漏了一些非常明显的东西? x=df['time'] d=df['dist'] x2=df2['time'] d2=df2['d
我的每个进程都有一个值数组 v 和一个大小相同的进程 ID pid 数组。 pid[i] 指定要将项目 v[i] 发送到哪个进程。 我需要实现一个分散操作(当然还有相应的聚集操作)。 进程 ID 数组
如何使用核心图绘制有间隙的散点图?我正在使用核心绘图库。一切正常,但现在我想在数据线的开头或中间做一个有间隙的散点图?有人帮我吗? 最佳答案 让数据源返回nil 或[NSNull null]。例如,如
我有一些代码可以绘制一些点。我将 ax.scatter 替换为 ax.plot,这样我就可以单独控制每个点的颜色。但是,当我进行此更改时,x 轴和 y 轴的范围似乎会增加。 我无法确定为什么会这样。我
从我复制到自己服务器的已知公共(public)数据集开始。 数据集在这里:https://www.kaggle.com/imdevskp/corona-virus-report/download im
我正在使用 Bokeh 将 ~700 次模拟的结果与使用散点图的另一组结果进行对比。我想使用悬停工具通过分配标识模拟参数的自定义索引来定性地确定数据中的模式。 在下面的代码中,x和 y是来自 Pand
这个问题在这里已经有了答案: matplotlib scatter edge without specifying edgecolor (1 个回答) 3年前关闭。 我想通过使用 2 个索引 [Chi
我希望更好地理解流行的 EIP 分散聚集中的多个分离器、转换器和聚合方法。 用例是我发送一些可以对应于多种不同类型的 XML。我想分割 header 中指定的类型(每种类型不同)并通过与每种类型对应的
考虑以下示例(取自 Stackoverflow 上的另一篇文章): require(ggplot2) d <- data.frame(x = c(102856,17906,89697,74384,91
我有许多带有形状的矩阵 w1、w2、w3...wn (k*n1 、k*n2、k*n3...k*nn) 和 x1、x2、x3...xn 具有形状(n1*m、n2*m、n3*m...nn*m >). 我想
假设我的数据按以下方式组织: x_values = [6.2, 3.6, 7.3, 3.2, 2.7] y_values = [1.5, 3.2, 5.4, 3.1, 2.8] colours = [
我对 Spring Integration 还是很陌生,我尝试使用 IntetrationFlowDefinition.scatterGather() 但无济于事。总体思路是: 将一个String作为
我有一个谷歌分散图,我希望当鼠标悬停在圆圈上时,然后打开一个包含自定义字符串的信息窗口。 例如教程 https://developers.google.com/chart/interactive/do
好吧,我了解 JFreeChart 和其他人,但我正在编写自己的简单散点图。我已经有了一个箱形图(没有 y 轴标签,但当我在报告中解释它时,这不应该是一个大问题)。 我有一个基本的散点图类,但是我尝试
我是一名优秀的程序员,十分优秀!