for i in range(mat.shape[1]): for j in range(mat.shape[0]): val = mat[j, i] attribute = attribute_names[i] candidate = candidate_names[j] if val >= tresh: pos[candidate].append(attribute) elif val <= (-tresh): neg[candidate].append(attribute) return pos, neg session = start_session("sqlite:///database.sqlite") data_counties = load_counties(session, filter_empty=True) counties_mat = np.array( list(map(lambda x: x.get_vector().numerical(), data_counties))) vector_labels = data_counties[0].get_vector().labels(short=True) m = np.corrcoef(counties_mat, rowvar=False) candidates_mat = m[:7, 7:] dem_mat = candidates_mat[:2, :] rep_mat = candidates_mat[2:, :] democrat_candidate_names = vector_labels[:2] republican_candidate_names = vector_labels[2:7] attribute_names = vector_labels[7:]
from dbload import load_counties, load_states, start_session from primaryarea import CountyArea import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression session = start_session("sqlite:///database.sqlite") data_counties = load_counties(session) counties_mat = np.array( list(map(lambda x: x.get_vector().numerical(), data_counties))) data_states = load_states(session) states_mat = np.array( list(map(lambda x: x.get_vector().numerical(), data_states))) vector_labels = data_counties[0].get_vector().labels(short=True) candidate_names = vector_labels[:7] counties_names = list(map(lambda x: x.facts.area_name, data_counties)) states_names = list(map(lambda x: x.facts.area_name, data_states)) election_results = counties_mat[:, :7] attributes = counties_mat[:, 7:] reg = LinearRegression(positive=True).fit(attributes, election_results) actual_state_results = states_mat[:, :7] state_attributes = states_mat[:, 7:] predicted_state_results = reg.predict(state_attributes) for i, candidate_name in enumerate(candidate_names): actual_results = actual_state_results[:, i]