I am currently struggling with plotting my linear regression output. I found a similar problem and the recommendation was to make sure that the data type was set to int. I have made sure to incorporate that into my code.
I have gone through the code a number of times and the structure seems sound to me. I am open to any and all feedback! Thank you so much for your help!
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).
-------------------Step1-------------------
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
%pylab inline
import matplotlib.pyplot as plt
-------------------Step2-------------------
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)
-------------------Step3-------------------
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------------------- (I get the Value Error here)
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()
Error as follows---->
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.