import pandas as pd
csv_bestand = 'https://drive.google.com/uc?export=download&id=1mD8IJEoSssFTMJyUU8SFJcUV5DwKazUu'
tabel = pd.read_csv(csv_bestand) #makkelijker om csv te lezen. Voor excel nog wat andere modules nodig
tabel
#eigen voorspelling
def voorspellen(verdeling):
#importeren, herhaling ivm functie
import pandas as pd
csv_bestand = 'https://drive.google.com/uc?export=download&id=1mD8IJEoSssFTMJyUU8SFJcUV5DwKazUu'
tabel = pd.read_csv(csv_bestand) #makkelijker om csv te lezen. Voor excel nog wat andere modules nodig
#punten in blok 3 bepalen
punten_lijst_b3=[]
for studentx in range(len(tabel)):
punten=0
if tabel.values[studentx][3] >= 5.5:
punten+=3
cijfer_graaf=tabel.values[studentx][4]*0.2+tabel.values[studentx][5]*0.8
if cijfer_graaf >= 5.5:
punten+=3
for vakken in range (6,24-5): #-5 is t/m blok 3 !!!
if tabel.values[studentx][vakken] >= 5.5:
punten+=3
punten_lijst_b3.append(punten)
#wiskunde vakken 2/3 minimaal
wis = []
wis_punten=0
if tabel.values[studentx][6]>5.4:
wis_punten+=1
if tabel.values[studentx][11]>5.4:
wis_punten
if tabel.values[studentx][17]>5.4:
wis_punten+=1
#voorspelling inc wiskunde vakken
bsa_voorspelling=[]
for verdelingen in range(len(punten_lijst_b3)):
if punten_lijst_b3[verdelingen]>=verdeling and tabel.values[verdelingen][2]!='uitgesteld' and wis_punten>1:
bsa_voorspelling.append(1)
elif tabel.values[verdelingen][2]!='uitgesteld':
bsa_voorspelling.append(0)
bsa_1_0=[]
for uitslag in range(len(tabel)):
if tabel.values[uitslag][2] == 'positief':
bsa_1_0.append(1)
elif tabel.values[uitslag][2] == 'negatief':
bsa_1_0.append(0)
uitkomst=[]
for v in range(len(bsa_1_0)):
if bsa_1_0[v] == bsa_voorspelling[v]:
uitkomst.append("ja")
else: uitkomst.append("nee")
bsa_punten = tabel['Aantal behaalde punten (STP) binnen programma'].tolist()
from sklearn.metrics import accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
return [accuracy_score(bsa_1_0, bsa_voorspelling),recall_score(bsa_1_0, bsa_voorspelling), precision_score(bsa_1_0, bsa_voorspelling)]
lijstje=[]
for pp in range(36,46,3):
lijstje.append([voorspellen(pp),pp])
#sorteert op nauwkeurigheid
lijstje_2 = sorted(lijstje, key=lambda x: x[0])
#print deze lijst van lage naar hoge nauwkeurigheid
print(lijstje_2)
print(f'De verdeling op {lijstje_2[-1][1]} punten is optimaal')
[[[0.5887096774193549, 0.39285714285714285, 1.0], 45], [[0.8548387096774194, 0.7976190476190477, 0.9852941176470589], 42], [[0.8951612903225806, 1.0, 0.865979381443299], 36], [[0.9516129032258065, 0.9523809523809523, 0.975609756097561], 39]]
De verdeling op 39 punten is optimaal
verdeling_optimaal = lijstje_2[-1][1]
# importeren
import pandas as pd
csv_bestand = 'https://drive.google.com/uc?export=download&id=1mD8IJEoSssFTMJyUU8SFJcUV5DwKazUu'
tabel = pd.read_csv(csv_bestand) # makkelijker om csv te lezen. Voor excel nog wat andere modules nodig
# punten in blok 3 bepalen
punten_lijst_b3 = []
for studentx in range(len(tabel)):
punten = 0
if tabel.values[studentx][3] >= 5.5:
punten += 3
cijfer_graaf = tabel.values[studentx][4] * 0.2 + tabel.values[studentx][5] * 0.8
if cijfer_graaf >= 5.5:
punten += 3
for vakken in range(6, 24 - 5): # -5 is t/m blok 3 !!!
if tabel.values[studentx][vakken] >= 5.5:
punten += 3
punten_lijst_b3.append(punten)
#wiskunde vakken 2/3 minimaal
wis = []
wis_punten=0
if tabel.values[studentx][6]>5.4:
wis_punten+=1
if tabel.values[studentx][11]>5.4:
wis_punten
if tabel.values[studentx][17]>5.4:
wis_punten+=1
# inc wiskunde vakken
bsa_voorspelling = []
for verdelingen in range(len(punten_lijst_b3)):
if punten_lijst_b3[verdelingen] >= verdeling_optimaal and wis_punten>1 and tabel.values[verdelingen][2] != 'uitgesteld':
bsa_voorspelling.append(1)
elif tabel.values[verdelingen][2] != 'uitgesteld':
bsa_voorspelling.append(0)
bsa_1_0 = []
for uitslag in range(len(tabel)):
if tabel.values[uitslag][2] == 'positief':
bsa_1_0.append(1)
elif tabel.values[uitslag][2] == 'negatief':
bsa_1_0.append(0)
from sklearn.metrics import accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
daadwerkelijk = pd.Series(bsa_1_0,name='Actual')
gok = pd.Series(bsa_voorspelling, name='Predicted')
#df_confusion = pd.crosstab(daadwerkelijk,gok)
df_confusion = pd.crosstab(daadwerkelijk,gok, rownames=['Actual'], colnames=['Predicted'], margins=True)
df_confusion
!pip install pydotplus==2.0.2
#eventueel nodig indien niet reeds geïnstalleerd
Collecting pydotplus==2.0.2
Downloading pydotplus-2.0.2.tar.gz (278 kB)
|████████████████████████████████| 278 kB 12.0 MB/s
Requirement already satisfied: pyparsing>=2.0.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from pydotplus==2.0.2) (2.4.7)
Building wheels for collected packages: pydotplus
Building wheel for pydotplus (setup.py) ... done
Created wheel for pydotplus: filename=pydotplus-2.0.2-py3-none-any.whl size=24575 sha256=2f62b6b5d075d30bba9878c86aec21c87173a723a9cb6a165e3cb746c4bd5eae
Stored in directory: /root/.cache/pip/wheels/1e/7b/04/7387cf6cc9e48b4a96e361b0be812f0708b394b821bf8c9c50
Successfully built pydotplus
Installing collected packages: pydotplus
Successfully installed pydotplus-2.0.2
def boom(max_diepte):
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
from sklearn import tree
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
#link verloopt 20 juni
csv_bestand = 'https://dehaagsehogeschool-my.sharepoint.com/personal/18138926_student_hhs_nl/_layouts/15/download.aspx?e=O5mjLX&share=ERiaCEWj38hGiQWdOkeL9UYBEY6bQdgZqZ2h3bAiUS8g8Q'
boom = pd.read_csv(csv_bestand,usecols = [i for i in range(3,23)])
col_names = boom.columns.tolist()
boom = boom.fillna(0)
boom = boom.astype(int) #gehele getallen van gemaakt anders werkt code niet...
feature_cols = col_names[:-4]
X = boom[feature_cols] # Features
y = boom["MODEL-TH|Modelleren theorie"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=10) # 70% training and 30% test
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
clf = DecisionTreeClassifier(max_depth=max_diepte)
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
data = tree.export_graphviz(clf, out_file=None, feature_names=feature_cols)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mod_boom_' +str(max_diepte) +'_.png')
img=pltimg.imread('mod_boom_' +str(max_diepte) +'_.png')
imgplot = plt.imshow(img)
return ("Nauwkeurigheid:",metrics.accuracy_score(y_test, y_pred), str(max_diepte))
#print bomen en hun nauwkeurigheid
for diepte in range(1,12):
print(boom(diepte))
('Nauwkeurigheid:', 0.33962264150943394, '1')
('Nauwkeurigheid:', 0.2641509433962264, '2')
('Nauwkeurigheid:', 0.32075471698113206, '3')
('Nauwkeurigheid:', 0.3584905660377358, '4')
('Nauwkeurigheid:', 0.33962264150943394, '5')
('Nauwkeurigheid:', 0.37735849056603776, '6')
('Nauwkeurigheid:', 0.4339622641509434, '7')
('Nauwkeurigheid:', 0.3584905660377358, '8')
('Nauwkeurigheid:', 0.37735849056603776, '9')
('Nauwkeurigheid:', 0.33962264150943394, '10')
('Nauwkeurigheid:', 0.4339622641509434, '11')