def generate_crime_socioecon_data(crime_data, geo_data, population_data, socio_econ_data): ''' accepts crime_data, geo_data, population_data and socio_econ_data tables as parameters, generate a table with crime rate and other socio-econ data included in and return it ''' geo_pop_data = Question3.merge_data(geo_data, population_data) crime_arrest_rate = Question3.generate_crime_arrest_rate( crime_data, geo_pop_data) crime_arrest_rate = crime_arrest_rate[crime_arrest_rate['Year'] == 2018] crime_socioecon_data =\ crime_arrest_rate.merge(socio_econ_data, left_on='Community Area', right_on='Community Area Number') crime_socioecon_data = crime_socioecon_data.merge(geo_data, left_on='Community Area', right_on='commarea_n') crime_socioecon_data = crime_socioecon_data[[ 'COMMUNITY AREA NAME', 'Community Area', 'Year', 'crime_rate', 'PERCENT HOUSEHOLDS ' 'BELOW POVERTY', 'PERCENT AGED 16+ UNEMPLOYED', 'PERCENT AGED 25+ WITHOUT ' 'HIGH SCHOOL DIPLOMA', 'PERCENT AGED UNDER 18 OR ' 'OVER 64', 'geometry' ]].dropna() return crime_socioecon_data
def test_text_handle(self): t_list1 = [[55, 57, 90, 87, 43, 65, 60], [57, 60, 78, 74, 89, 45, 43], [43, 60, 90, 65, 38, 65, 78]] t_list2 = [[12, 6, 7, 8, 9, 10, 4], [10, 3, 5, 6, 20, 8, 5], [0, 85, 52, 4, 38, 1, 20]] self.assertEqual(Question3.list_handle(t_list1), [89, 65, 38, 74, 43, 45, 78, 55, 87, 57, 90, 60]) self.assertEqual(Question3.list_handle(t_list2), [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 38, 12, 20, 85, 52])
def take_time(array): time_taken = [] for i in range(1, len(array) + 1): sub_list = array[0:i] start_time = time() Question3.sort_list(sub_list) end_time = time() time_diff = end_time - start_time time_taken.append(time_diff) return time_taken
def take_space(array): space_taken = [] for i in range(1, len(array) + 1): sub_list = array[0:i] h.setrelheap() Question3.find_maximum(sub_list) raw_string = repr(h) raw_string = raw_string.split() space = raw_string[10] space_taken.append(space) return space_taken
def take_space(array): space_taken = [] for i in range(1, len(array) + 1): sub_list = array[0:i] h.setrelheap() Question3.make_lowercase(sub_list) raw_string = repr(h) raw_string = raw_string.split() space = int(raw_string[10]) space_taken.append(space) return space_taken
def main(): sns.set() file_names = os.listdir() needed_files = ['sentence_length.csv', 'socio_econ.csv', 'Chicago_shape.zip', 'population.csv'] for file_name in needed_files: if file_name not in file_names: save_useful_files() print('file_saved') crime_data = get_crime_data() print('get crime data') geo_data = get_geo_data() population_data = get_population_data() sentence_length_data = get_sentence_length_data() socio_econ_data = get_socio_econ_data() get_crime_sample(crime_data) q1 = Question1.Question1(crime_data, sentence_length_data, geo_data, socio_econ_data) q1._plot_a_single_year(2018) q1._plot_change('42 43 45') q1._safety_ranking(2018) q2 = Question2.Question2() q2.aggregate_data(crime_data) q2.machine_learning() q2._report_predict('assault', 43, 23) q2._report_predict('theft', 9, 23) q2._report_predict('theft', 9, 10) q2._report_predict('theft', 9, 13) print('The mean square error of the model is ' + str(q2.mes())) Question3.Question3(crime_data, geo_data, population_data) Question4.Question4(crime_data, geo_data, population_data, socio_econ_data)
def main(): sns.set() file_names = os.listdir() needed_files = [ 'sentence_length.csv', 'socio_econ.csv', 'Chicago_shape.zip', 'population.csv' ] for file_name in needed_files: if file_name not in file_names: save_useful_files() print('file_saved') crime_data = get_crime_data() print('get crime data') geo_data = get_geo_data() population_data = get_population_data() sentence_length_data = get_sentence_length_data() socio_econ_data = get_socio_econ_data() # get_crime_sample(crime_data) q1 = Question1.Question1(crime_data, sentence_length_data, geo_data, socio_econ_data) q1.plot_communities_single_year() q1.plot_change_through_years() q1.safety_ranking() q2 = Question2.Question2() q2.aggregate_data(crime_data) q2.machine_learning() q2.predict() print('The mean square error of the model is ' + str(q2.mes())) Question3.Question3(crime_data, geo_data, population_data) Question4.Question4(crime_data, geo_data, population_data, socio_econ_data)
def main(): Question1.q1() Question2.q2() Question3.q3() Question4.q4()
from time import time import Question3 # Estimate how long each algorithm would take for inputs of size 1,000,000. # Algorithm 1 starting_time = time() Question3.find_maximum([1]) ending_time = time() time_difference = ending_time - starting_time print("Time for input size 1: " + str(time_difference)) print("Time for input size 1,000,000: " + str(time_difference)) # Algorithm 2 starting_time1 = time() Question3.make_lowercase("M") ending_time1 = time() time_difference1 = ending_time1 - starting_time1 print("Time for input size 1: " + str(time_difference1)) print("Time for input size 1,000,000: " + str(time_difference1)) # Algorithm 3 starting_time2 = time() Question3.sort_list([1]) ending_time2 = time() time_difference2 = ending_time2 - starting_time2 print("Time for input size 1: " + str(time_difference2)) print("Time for input size 1,000,000: " + str(time_difference2))
import Question3 import Question4 import Question5 print("######Computational Finance - Project 1###########") print("Nitish Ramkumar") # Question1 n = 10000 seed = 2000 Question1.question1(n, seed) # Question2 probs = [0.3, 0.35, 0.2, 0.15] probvals = [-1, 0, 1, 2] Question2.question2(probs, probvals, n, seed) # Question3 n = 44 p = 0.64 tot = 1000 Question3.question3(n, p, tot, seed) # Question 4 lambd = 1.5 n = 10000 Question4.question4(n, lambd, seed) # Question 5 Question5.question5(seed)
import Question1 import Question2 import Question3 import Question4 import Question5 import Question6 # Question1 seed = 2000 rho = Question1.question1(seed) print("The Rho value is {0:.4f}".format(rho)) # Question2 val = Question2.question2(seed) print("The expected value is {0:.4f}".format(val)) # Question3 val = Question3.question3(seed) # Question 4 Question4.question4(seed) # Question 5 Question5.question5(seed) # Question 6 Question6.question6(seed)
import Question1 import Question2 import Question3 import Question4 import Question5 print("######Computational Finance - Project 9###########") print("Nitish Ramkumar") # Question1 Question1.question1() #Question2 Question2.question2() #Question3 market_price = 110000 oas = Question3.question3(market_price) #Question4 Question4.question4(market_price, oas) #Question5 Question5.question5(oas)
import Question3 import json class UserEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Question3.stack): a = obj b = [] while not a.isempty(): b.append(a.pop()) return b return json.JSONEncoder.default(self, obj) def serialize(object): return json.dumps(object, cls=UserEncoder) #Test if __name__ == '__main__': a = Question3.stack() a.push(1) a.push(2) a.push(3) print(serialize(a))