This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_mining.py
More file actions
97 lines (77 loc) · 2.46 KB
/
data_mining.py
File metadata and controls
97 lines (77 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Program Name: data_mining.py
# Anthony Waldsmith
# 8/03/16
# Python Version 3.4
# Description: Datamining CSV google stocks
# Optional import for versions of python <= 2
from __future__ import print_function
#name: get_data_list
#param: FILE_NAME - the file's name you saved for the stock's prices
#brief: get a list of the stock's records' lists
#return: a list of lists
def get_data_list (FILE_NAME):
f = open(FILE_NAME, "r")
array = []
for line in f:
array += [line]
f.close()
return array
#name: get_monthly_averages
#param: data_list - the list that you will process
#brief: get a list of the stock's monthly averages and their corresponding dates
#return: a list
# THIS COULD BE OPTIMIZED, HOWEVER THIS WAY MAKES A BIT MORE SENSE TO ME
def get_monthly_averages (data_list):
monthly_average_list = []
skippedHeader = False
years = []
months = []
data = []
# For each line of data ...
for line in data_list:
if skippedHeader == True:
section = line.split(',')
date = section[0].split('-')
years += [date[0]]
months += [date[1]]
data += [str(date[0] + "," + date[1] + "," + date[2] + "," + section[6][:-1]).split(',')]
else:
skippedHeader = True
print ("Processing ...")
for year in years:
for month in months:
amount = 0
count = 0
for s in data:
print (s[1])
print (month)
if s[1] == month:
count += 1
amount += float(s[3])
average = (amount / count)
print (average)
monthly_average_list += [year + "," + month + "," + str(average)]
return monthly_average_list
#name: print_info
#param: monthly_average_list - the list that you will process
#brief: print the monthly averages of Google stock
#return: None
def print_info (monthly_average_list):
# show monthly averages of Google stock
index = 0
for s in monthly_average_list:
print("monthly_average_list [%i]\t=\t%s" %(index,s))
index +=1
def main():
data_list = get_data_list("table.csv")
monthly_average_list = get_monthly_averages(data_list)
print_info(monthly_average_list)
main()
# 1) call get_data_list(FILE_NAME) function to get the data list.
# Return into variable data_list
# 2) call get_monthly_averages(data_list) function with variable data_list from above as argument.
# Return into variable monthly_average_list
# 3) call print_info(monthly_average_list) function with variable monthly_average_list from above as argument.
# Show the monthly_average_list as shown in the sample run below