Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import datetime
- import matplotlib
- from matplotlib import pyplot
- def parse_time(text):
- return datetime.datetime.strptime(text, '%Y-%m-%d %H:%M:%S.%f')
- def load_csv(filename):
- with open(filename, encoding='utf8') as f:
- return list(csv.reader(f, delimiter=','))
- # id, task, variant, group, time
- messages = load_csv('data/messages.csv')
- # id, message_id, time, status
- checks = load_csv('data/checks.csv')
- # task, variant, group, time, status, achievements
- statuses = load_csv('data/statuses.csv')
- games = load_csv('data/GAMES.csv')
- def convert_string_to_list(text):
- return list(map(int, text[1:-1].split(',')))
- # 3.1
- dates = []
- for row in checks:
- dates.append(parse_time(row[2]))
- days = [0, 0, 0, 0, 0, 0, 0]
- for date in dates:
- days[date.weekday()] += 1
- matplotlib.pyplot.plot(days)
- matplotlib.pyplot.show()
- # 3.2
- hours = []
- for i in range(0, 24):
- hours.append(0)
- for date in dates:
- hours[date.hour] += 1
- #matplotlib.pyplot.plot(hours)
- #matplotlib.pyplot.show()
- # 3.3
- tasks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- for row in messages:
- tasks[int(row[1])] += 1
- print(tasks)
- #matplotlib.pyplot.plot(tasks)
- #matplotlib.pyplot.show()
- # 3.4
- tasks_per_day = {0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}}
- for row in messages:
- tasks_per_day[int(row[1])][parse_time(row[4]).date()] = 0
- for row in messages:
- tasks_per_day[int(row[1])][parse_time(row[4]).date()] += 1
- # create 10 plots for 10 tasks with number of messages per day from 2023-02-10 to 2023-03-25
- for i in range(0, 10):
- x = []
- y = []
- for date in tasks_per_day[i]:
- x.append(date)
- y.append(tasks_per_day[i][date])
- #matplotlib.pyplot.title('3.3')
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.5
- groups = {}
- for row in messages:
- if row[3] not in groups:
- groups[row[3]] = 0
- for row in messages:
- groups[row[3]] += 1
- # sort groups by number of the group
- groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
- # create plot with number of messages per group
- x = []
- y = []
- for group in groups:
- x.append(group)
- y.append(groups[group])
- #matplotlib.pyplot.figure(figsize=(75, 10))
- #matplotlib.pyplot.plot(x, y)
- #matplotlib.pyplot.show()
- # 3.6
- groups = {}
- for row in messages:
- if row[3] not in groups:
- groups[row[3]] = 0
- for row, row1 in zip(messages, checks):
- if row1[3] == '3':
- groups[row[3]] += 1
- groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
- x = []
- y = []
- for group in groups:
- x.append(group)
- y.append(groups[group])
- matplotlib.pyplot.figure(figsize=(15, 10))
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.7
- tasks_status_2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- tasks_status_all = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- for mes, ch in zip(messages, checks):
- if ch[3] == '2':
- tasks_status_2[int(mes[1])] += 1
- if ch[3] == '2' or ch[3] == '3':
- tasks_status_all[int(mes[1])] += 1
- tasks_status_2_3 = []
- for i in range(0, 8):
- tasks_status_2_3.append(tasks_status_all[i] / tasks_status_2[i])
- print(tasks_status_2)
- print(tasks_status_all)
- x = []
- y = []
- for i in range(0, 8):
- x.append(i+1)
- y.append(tasks_status_2_3[i])
- matplotlib.pyplot.figure(figsize=(15, 10))
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.8
- groups = {}
- for row in statuses:
- if row[2] not in groups:
- groups[row[2]] = 0
- if row[4] == '2':
- groups[row[2]] += len(convert_string_to_list(row[5]))
- #print(len(convert_string_to_list(row[5])))
- # sort groups by number of the group
- groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
- # create plot with number of messages per group
- x = []
- y = []
- for group in groups:
- x.append(group)
- y.append(groups[group])
- matplotlib.pyplot.figure(figsize=(75, 10))
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.9
- students = {}
- for row in statuses:
- if row[2] + " вариант: " + row[1] not in students:
- students[row[2] + " вариант: " + row[1]] = 0
- if row[4] == '2':
- students[row[2] + " вариант: " + row[1]] += len(convert_string_to_list(row[5]))
- students = dict(sorted(students.items(), key=lambda item: (item[1]), reverse=True))
- # create plot with number of messages per first 10 students
- x = []
- y = []
- i = 0
- for student in students:
- if i == 10:
- break
- x.append(student)
- y.append(students[student])
- i += 1
- matplotlib.pyplot.figure(figsize=(50, 10))
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.10
- groups = {}
- for row in statuses:
- if row[2] not in groups:
- groups[row[2]] = 0
- if row[4] == '2' and len(convert_string_to_list(row[5])) > 1:
- groups[row[2]] += 1
- groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
- x = []
- y = []
- for group in groups:
- x.append(group)
- y.append(groups[group])
- matplotlib.pyplot.figure(figsize=(75, 10))
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.11
- years = {}
- # split games array by ;
- games = [game[0].split(';') for game in games]
- for game in games:
- if game[3] not in years:
- years[game[3]] = 0
- years[game[3]] += 1
- years = dict(sorted(years.items(), key=lambda item: (item[0])))
- years.popitem()
- x = []
- y = []
- for year in years:
- year = year[1:-1]
- x.append(year)
- y.append(years['"'+year+'"'])
- matplotlib.pyplot.figure(figsize=(20, 10))
- matplotlib.pyplot.plot(x, y)
- matplotlib.pyplot.show()
- # 3.12
- genres = {}
- games = [game[0].split(';') for game in games]
- for game in games:
- if game[1] not in genres:
- genres[game[1]] = {}
- if game[3] not in genres[game[1]]:
- genres[game[1]][game[3]] = 0
- genres[game[1]][game[3]] += 1
- # create plot for each genre
- matplotlib.pyplot.figure(figsize=(20, 10))
- for genre in genres:
- if '"не издана"' in genres[genre]:
- genres[genre].pop('"не издана"')
- x = []
- y = []
- for year in genres[genre]:
- year = year[1:-1]
- x.append(year)
- y.append(genres[genre]['"'+year+'"'])
- matplotlib.pyplot.plot(x, y, label=genre)
- matplotlib.pyplot.legend()
- matplotlib.pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement