Skip to content

Instantly share code, notes, and snippets.

@Shoeboxam
Created March 24, 2015 01:44
Show Gist options
  • Save Shoeboxam/38e5752075044b2bc87d to your computer and use it in GitHub Desktop.
Save Shoeboxam/38e5752075044b2bc87d to your computer and use it in GitHub Desktop.
Psych GPA vs work hours faux study
import random
import matplotlib.pyplot as plt
import numpy as np
respondents = 23
random.seed()
gpa = []
hours_worked = []
for point in range(respondents / 3):
gpa_point = random.triangular(1.0, 4.5, 3.5)
worked_point = gpa_point * 10 * random.uniform(0.5, 1.5)
gpa_point = round(gpa_point / .1) * .1
worked_point = round(worked_point / 5) * 5
hours_worked.append(worked_point)
gpa.append(gpa_point)
gpa.append(round(random.triangular(0.0, 4.5, 2.0) / .1) * .1)
hours_worked.append(round(random.triangular(1, 50) / 5) * 5)
gpa.append(round(random.triangular(0.0, 4.8, 4.8) / .1) * .1)
hours_worked.append(round(random.triangular(1, 50) / 5) * 5)
gpa = np.clip(np.array(gpa), 0.0, 4.0)
hours_worked = np.array(hours_worked)
with open('C:\Users\mike_000\Desktop\psych_data.txt', 'w') as outfile:
for index in range(len(gpa)):
outfile.write(hours_worked[index].astype(str) + '-' + gpa[index].astype(str) + '\n')
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Lists of gpa/work hour data
gpa = []
hours_worked = []
# Read data from text file
with open('C:\Users\mike_000\Desktop\psych_data.txt') as infile:
lines = infile.readlines()
print(lines)
for line in lines:
# Split each entry into work hours and gpa via dash delimiter
work_hours_point, gpa_point = line.split('-')
hours_worked.append(work_hours_point)
gpa.append(gpa_point)
# Convert to numpy arrays
hours_worked = np.array(hours_worked).astype(np.float16)
gpa = np.array(gpa).astype(np.float16)
print(np.corrcoef(gpa, hours_worked))
# Labels
plt.title('GPA vs Work Hours')
plt.ylabel("Grade Point Average")
plt.xlabel("Hours Worked")
# Scale axis
plt.axis((0, np.amax(hours_worked), 1.0, 4.0))
# Render plot with data lists on x and y axes
plt.scatter(hours_worked, gpa)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment