In [1]:
import numpy as np
import scipy as sp
import pandas as pd
import sys

print(np.version.version)
print(sp.version.version)
print(pd.__version__)
print(sys.version)

import os
cwd = os.getcwd()
cwd
1.10.4
0.17.0
0.18.1
3.5.1 |Anaconda custom (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)]
Out[1]:
'C:\\Users\\Demetriadis\\01-myPython\\Workshop'
In [8]:
# t-Test using lists

import statistics as st

N = 20 
control = [12,13,14,15,16,12,13,14,15,16,13,14,15,16,17,13,14,15,16,17]
treatment = [13,14,15,16,17,13,14,15,16,17,14,15,16,17,18,14,15,16,17,18]

# print(control)
# print(treatment)

mC = st.mean(control)
mT = st.mean(treatment)

sC = st.stdev(control)
sT = st.stdev(treatment)

print('Mean Control:',mC,'StDev Control:', round(sC,4))
print('Mean Treatment:',mT,'StDev Treatment:',round(sT,4))


#----------------------------------------------------------------
import scipy.stats as stats

print('Normality Control:', round(stats.shapiro(control)[1],4))
print('Normality Treatment:',round(stats.shapiro(treatment)[1],4))

print('Between Group Variance:',stats.levene(control,treatment)[1])

t, p = stats.ttest_ind(control, treatment)
print('t = ',round(t,4),'p = ',round(p,4))
if p<=0.05:
    print('Sig.')

# small <= 0.2 Medium <=0.8 LARGE 
es = abs(mC-mT)/sC
print('Effect size = ',round(es,4))
Mean Control: 14.5 StDev Control: 1.539
Mean Treatment: 15.5 StDev Treatment: 1.539
Normality Control: 0.2379
Normality Treatment: 0.2379
Between Group Variance: 1.0
t =  -2.0548 p =  0.0468
Sig.
Effect size =  0.6498
In [8]:
# --- Numpy arrays -----------
# --- Vectorization

import numpy as np

ar1 = np.array([1,2,3,4])
ar2 = np.array([5,6,7,8])

print(ar1 + ar2)
[ 6  8 10 12]
In [12]:
# --- Numpy arrays -----------

alist = [[i+k for k in range(3)] for i in range(4)]
print(alist)

ar1 = np.array(alist)
print(ar1, ar1[2][1])

ar2 = np.arange(16).reshape(2,4,2)
print(ar2)
print(ar2[0,1])

ar3 = np.linspace(0,1,11,True)
print(ar3)

# Scalar programming (βαθμωτός προγραμματισμός)
# Ο τελεστής '+' επιδρά σε βαθμωτά μεγέθη
x = 1
y = 2
asum = x+y

# Vector programming (διανυσματικός προγραμματισμός)
# Ο τελεστής '+' επιδρά σε πίνακα & βαθμωτό μεγέθη
ar4 = ar3+1
print(ar4)

# Ο τελεστής '+' επιδρά σε πίνακες (διανυσματικά μεγέθη)
ar5 = ar3+ar4
print(ar5)


# -- Drawing with matplotlib ----- 
import matplotlib.pyplot as plt
%matplotlib inline

x = np.linspace(0,2*np.pi,100)
y = np.sin(x)
p = plt.plot(x,y)
#plt.show()
[[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]]
[[0 1 2]
 [1 2 3]
 [2 3 4]
 [3 4 5]] 3
[[[ 0  1]
  [ 2  3]
  [ 4  5]
  [ 6  7]]

 [[ 8  9]
  [10 11]
  [12 13]
  [14 15]]]
[2 3]
[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1. ]
[ 1.   1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9  2. ]
[ 1.   1.2  1.4  1.6  1.8  2.   2.2  2.4  2.6  2.8  3. ]
In [4]:
# --- t-Test using numpy arrays ---------------
import numpy as np
import statistics as st

N = 20 
control = np.array([12,13,14,15,16,12,13,14,15,16,13,14,15,16,17,13,14,15,16,17])
treatment = np.array([13,14,15,16,17,13,14,15,16,17,14,15,16,17,18,14,15,16,17,18])

# print(control)
# print(treatment)

mC = st.mean(control)
mT = st.mean(treatment)

sC = st.stdev(control)
sT = st.stdev(treatment)

print('Mean Control:',mC,'StDev Control:', round(sC,4))
print('Mean Treatment:',mT,'StDev Treatment:',round(sT,4))

#----------------------------------------------------------------
import scipy.stats as stats

print('Normality Control:', round(stats.shapiro(control)[1],4))
print('Normality Treatment:',round(stats.shapiro(treatment)[1],4))

print('Between Group Variance:',stats.levene(control,treatment)[1])

t, p = stats.ttest_ind(control, treatment)
print('t = ',round(t,4),'p = ',round(p,4))
if p<=0.05:
    print('Sig.')

es = abs(mC-mT)/sC
print('Effect size = ',round(es,4))
Mean Control: 14.5 StDev Control: 1.539
Mean Treatment: 15.5 StDev Treatment: 1.539
Normality Control: 0.2379
Normality Treatment: 0.2379
Between Group Variance: 1.0
t =  -2.0548 p =  0.0468
Sig.
Effect size =  0.6498
In [5]:
# --- Graph drawing with matplotlib ----------------------

import numpy as np
import scipy.stats as stats

import matplotlib.pyplot as plt
% matplotlib inline

control = np.array([12,13,14,15,16,12,13,14,15,16,13,14,15,16,17,13,14,15,16,17])
treatment = np.array([13,14,15,16,17,13,14,15,16,17,14,15,16,17,18,14,15,16,17,18])

# --- Boxplot -------------------
bplot = [control,treatment]
bpl = plt.boxplot(bplot)
plt.ylim(10,20)
plt.show()

# --- histogram ------------------
#n, bins, patches = plt.hist(control, 6, color='cyan', align='left', alpha=0.5)
arfreq = stats.itemfreq(control)
barloc = np.arange(control.min(),len(arfreq)+control.min())
freqsC = np.array([arfreq[x][1] for x in range(len(arfreq))]) 
width = 0.5
histpl= plt.bar(barloc, freqsC, width, align='center', color='c')
plt.show()
In [6]:
# --- Διάβασμα δεδομένων από αρχείο 
# --- Κάθε απλό δεδομένο είναι σε ξεχωριστή σειρά 

with open('control.txt', 'r') as f:
    control = []
    for grade in f:
        control.append(float(grade))
    print(control)

with open('treatment.txt', 'r') as f:
    treatment = []
    for grade in f:
        treatment.append(float(grade))
    print(treatment)
[12.0, 13.0, 14.0, 15.0, 16.0, 12.0, 13.0, 14.0, 15.0, 16.0, 13.0, 14.0, 15.0, 16.0, 17.0, 13.0, 14.0, 15.0, 16.0, 17.0]
[13.0, 14.0, 15.0, 16.0, 17.0, 13.0, 14.0, 15.0, 16.0, 17.0, 14.0, 15.0, 16.0, 17.0, 18.0, 14.0, 15.0, 16.0, 17.0, 18.0]