Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import csv # To read .csv files. import numpy as np #Reading the file with open('Red_Light_Camera_Violations.csv','r') as f: reader = csv.DictReader(f) dataset = list(reader) file

import csv # To read .csv files.

import numpy as np

#Reading the file

with open('Red_Light_Camera_Violations.csv','r') as f: reader = csv.DictReader(f) dataset = list(reader) file = open('C:/Red_Light_Camera_Violations.csv' , 'rb') for line in file.readlines(): print(line)

#data is stored as a dictionary with keys and values.

# Getting date and number of voilations

t1 = [] #temp array to store the date t2 = [] #temp array to store the voilations for row in dataset: t1.append(row['VIOLATION DATE']); t2.append(row['VIOLATIONS']);

#splitting the date into month, day and year

for j in range(len(t1)): t1[j] = t1[j].split('/')

from collections import defaultdict

temp = defaultdict(int) for i in range(len(t1)): temp[int(t1[i][0])] = temp[int(t1[i][0])]+int(t2[i]) #temp dictionary stores the month and it's voilations

# Creating X and Y arrays

X=[] Y=[] for i in temp: X.append(i) #stores month Y.append(temp[i]) #stores voilations

#converting to numpy arrays

X = np.array(X) Y = np.array(Y)

#fitting into linear equation ax+b and plotting using matlab.plt

Z = np.polyfit(X,Y,1)

p = np.poly1d(Z)

import matplotlib.pyplot as plt

xp = np.linspace(-2, 10, 100)

plt.plot(X, Y, '.', xp, p(xp), '-')

plt.show()

#Function to find the day of the week

def dayofweek(data): d = int(data[1]) m = int(data[0]) y = int(data[2]) if m = 3: dayofweek -= 2 dayofweek = dayofweek%7 return dayofweek

# creating X1 and Y1 arrays and temp1 dictionary to store day and voilations

X1 = [] Y1= [] temp1 = defaultdict(int) for i in range(len(t1)): temp1[dayofweek(t1[i].split('/'))+1] = temp1[dayofweek(t1[i].split('/'))+1]+int(t2[i])

for i in temp: X1.append(i) Y1.append(temp[i]) X1 = np.array(X1) Y1= np.array(Y1)

#Polynomial 7 function fitting

Z = np.polyfit(X,Y,7) p = np.poly1d(Z)

# plotting

import matplotlib.pyplot as plt xp = np.linspace(-2, 8, 100) plt.plot(X1, Y1, '.', xp, p(xp), '-') plt.show()

image text in transcribed

I have moved the file from the directory folder at first, and then to the desktop. I still get the same error and I'm not sure what's wrong with it.

? Python 3.6.5 Shell File Edit Shell Debug Options Window Help Python 3.6.5 (v3.6.5:59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Inte 1)1 on win32 Type "copyright", "credits" or "license for more information. RESTART: C:\Users\kevin Desktop\ Python Projectslproject 2.py Traceback (most recent call last) File "C:Users\kevin\Desktop\Python Projects project 2.py", line 7, in

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions