Esempio n. 1
0
import numpy as np
import matplotlib.pyplot as plt
from getDataset import getDataSet
from sklearn.linear_model import LogisticRegression

# Starting codes

# Fill in the codes between "%PLACEHOLDER#start" and "PLACEHOLDER#end"

# step 1: generate dataset that includes both positive and negative samples,
# where each sample is described with two features.
# 250 samples in total.

[X, y] = getDataSet()  # note that y contains only 1s and 0s,

# create figure for all charts to be placed on so can be viewed together
fig = plt.figure()


def func_DisplayData(dataSamplesX, dataSamplesY, chartNum, titleMessage):
    print(dataSamplesY == 0)
    idx1 = (dataSamplesY == 0).nonzero()  # object indices for the 1st class
    idx2 = (dataSamplesY == 1).nonzero()
    ax = fig.add_subplot(1, 3, chartNum)
    fig.set_size_inches(15, 5, forward=True)
    # no more variables are needed
    plt.plot(dataSamplesX[idx1, 0], dataSamplesX[idx1, 1], 'r*')
    plt.plot(dataSamplesX[idx2, 0], dataSamplesX[idx2, 1], 'b*')
    # axis tight
    ax.set_xlabel('x_1')
    ax.set_ylabel('x_2')
Esempio n. 2
0
    # tp    True Positive
    tn, fp, fn, tp = confMatrix.ravel()
    print('True Positive: ' + str(tp))
    print('False Positive: ' + str(fp))
    print('True Negative: ' + str(tn))
    print('False Negative: ' + str(fn))

    recall = tp/(tp + fn)
    precision = tp/(tp + fp)
    accuracy = (tp + tn)/(tp+fp+tn+fn)
    print('Accuracy: {}'.format(accuracy))
    print('Precision: {}'.format(precision))
    print('Recall: {}'.format(recall))

# Creating a random sample set 
X,y = getDataSet()
# Create figure for all charts to be placed on so can be viewed together
fig = plt.figure()
# Plotting all samples
func_DisplayData(X, y, 1, 'All samples')
# Splitting the sample set into training and testing samples
trainX,testX,trainY,testY = train_test_split(X,y,test_size = 0.3)
# Plot the samples you have pickup for training, check to confirm that both negative
#   and positive samples are included.
func_DisplayData(trainX, trainY, 2, 'training samples')
func_DisplayData(testX, testY, 3, 'testing samples')
# Show all charts
plt.show()

# Use sklearn logistic regression model
clf = LogisticRegression()