X = extract_from_json_as_np_array("x", classification_training)
Y = extract_from_json_as_np_array("y", classification_training)

## Leer de classificaties
x_2 = X[...,0]
y_2 = X[...,1]
plt.axis([min(x_2), max(x_2), min(y_2), max(y_2)])

## Train de data in een Logistic Regression model, maak de y_predict aan gebaseerd op de getrainde data
regression = LogisticRegression().fit(X, Y)
Y_predict = regression.predict(X)

## Bereken de accuracy score gebaseerd op je Y_predict t.o.v daadwerkelijke Y
initialRegressionScore = accuracy_score(Y, Y_predict)
print("Initial accuracy score (Logistic Regression): " + str(float(initialRegressionScore) * 100) + "%")

## vergelijk Y_predict met de echte Y om te zien hoe goed je getraind hebt, 0 is paars & 1 is geel
plt.scatter(x_2, y_2, c = Y_predict, s = 10)
if show_diff:
    plt.show()

if start_test:
    classification_test = data.classification_test()
    X_test = extract_from_json_as_np_array("x", classification_test)

    ## Voorspelt nu echt de Y-waarden
    Z = regression.predict(X_test)

    classification_test = data.classification_test(Z.tolist())
    print("Trained decision tree accuracy score (Logistic Regression): " + str(float(classification_test) * 100) + "%")
clf_lg = LogisticRegression(max_iter=5000).fit(
    X, Y)  #Gaat zelf bedenken of dit punt 0 of 1 is door alleen de X te geven?
Y_lg_pred = clf_lg.predict(X[:, :])

clf_dt = tree.DecisionTreeClassifier()
clf_dt = clf_dt.fit(X, Y)
Y_dt_pred = clf_dt.predict(X[:, :])

predict_1 = accuracy_score(Y, Y_lg_pred)
predict_2 = accuracy_score(Y, Y_dt_pred)

print(predict_1)
print(predict_2)

# haal data op om te testen
classification_test = data.classification_test()
# testen doen we 'geblinddoekt' je krijgt nu de Y's niet
X_test = extract_from_json_as_np_array("x", classification_test)

# TODO: voorspel de Y-waarden
#Y_test = clf.predict(X_test)
#clf_lg = LogisticRegression(max_iter=5000).fit(X_test, Y_test)
Y_lg_pred = clf_lg.predict(X_test[:, :])

#clf_dt = tree.DecisionTreeClassifier()
#clf_dt = clf_dt.fit(X_test, Y_test)
Y_dt_pred = clf_dt.predict(X_test[:, :])

#Y_lg_pred of Y_dt_pred

Z = Y_dt_pred  # dit is een gok dat alles 0 is... kan je zelf voorspellen hoeveel procent er goed voorspeld is?
Example #3
0
tree_model = tree.DecisionTreeClassifier()

# Train the model using the training sets
tree_fit = tree_model.fit(X, Y)

# Make predictions using the testing set
tree_y_pred = tree_fit.predict(X)

for i in range(len(x)):
    if tree_y_pred[i] == 0:
        plt.plot(x[i], y[i], 'r.')
    if tree_y_pred[i] == 1:
        plt.plot(x[i], y[i], 'b.')

plt.axis([min(x), max(x), min(y), max(y)])
plt.show()

# haal data op om te testen
classification_test = data.classification_test()
# testen doen we 'geblinddoekt' je krijgt nu de Y's niet
X_test = extract_from_json_as_np_array("x", classification_test)

yPredictReg = regr_fit.predict(X_test)
dTreePredict = tree_fit.predict(X_test)

classification_test = data.classification_test(yPredictReg.tolist())
print("Classificatie accuratie (Regression): " + str(classification_test))

classification_test = data.classification_test(dTreePredict.tolist())
print("Classificatie accuratie (Decision tree): " + str(classification_test))
Example #4
0
# Ontdek de clusters mbv kmeans en teken een plot met kleurtjes
kmeans = KMeans(n_clusters=2, random_state=0).fit_predict(X)
plt.scatter(X[:,0], X[:,1], c=kmeans, s=10)
plt.show()

# SUPERVISED LEARNING

# haal data op voor classificatie
classification_training = data.classification_training()

# extract de data x = array met waarden, y = classificatie 0 of 1
X = extract_from_json_as_np_array("x", classification_training)
Y = extract_from_json_as_np_array("y", classification_training)

# haal data op om te testen
classification_test = data.classification_test()
# testen doen we 'geblinddoekt' je krijgt nu de Y's niet
X_test = extract_from_json_as_np_array("x", classification_test)

# Voorspel de Y-waarden (Decisiontree)
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier().fit(X, Y)
pred = clf.predict(X)
acc = accuracy_score(Y, pred)
print("Eigen data test (Decision tree): " + str(acc))

dataTest = clf.predict(X_test)

# Stuur je voorspelling naar de server om te kijken hoe goed je het gedaan hebt
classification_test = data.classification_test(dataTest.tolist())
print("Echte data test (Decision tree) " + str(classification_test))