コード例 #1
0
ファイル: compare.py プロジェクト: aimreant/svm_tester
def get_accurate_rate(
        data_training_url,
        data_testing_url
):
    """
    This function will firstly get data from two file
    and then train the svm
    lastly return the accurate rate
    :param data_training_url:
    :param data_testing_url:
    :return:
    """

    data_training_input, data_training_output \
        = get_data(data_training_url)

    data_test_input, data_test_output \
        = get_data(data_testing_url)

    clf = svm.SVC()
    clf.fit(data_training_input, data_training_output)

    # get the output from svm
    svm_output = clf.predict(data_test_input)

    # compare standard output and svm output
    # and get the accurate rate
    correct_count = 0
    for (o1, o2) in zip(data_test_output, svm_output):
        if o1 == o2:
            correct_count += 1

    return correct_count / len(data_test_input)
コード例 #2
0
def compute_class_score_histogram(model, label=0, n_classes=29, max_iter=10):

    _, val_generator, _, _ = get_data()

    n_bins = 100
    bins = np.linspace(0, 1, num=n_bins + 1)

    histograms = np.zeros((n_classes, n_bins), dtype=int)

    count = 0

    for X, y in tqdm(val_generator):

        y_true = np.argmax(y, axis=-1)
        y_predict = model.predict(X)
        y_predict = y_predict[..., label]

        for i, class_index in enumerate(y_true):

            histograms[class_index] += np.histogram(y_predict[i], bins=bins)[0]

        count += 1

        if count > len(val_generator):

            break

    return histograms
コード例 #3
0
ファイル: task1.py プロジェクト: Wiltzu/DAKD-EXEC2
def get_full_data():
    original_data, missing_value = read.get_data()
    missing_value_index = get_index(missing_value)
    data = format_data(original_data, missing_value_index)
    m_value = format_data(missing_value, missing_value_index)
    neares_indices = get_nearest_neighbors(data, m_value)

    full_data = predict(original_data, missing_value, neares_indices, missing_value_index)

    return full_data
コード例 #4
0
def fit_peak(detector, source, filename, no_peaks, angle):
    '''Extracts data from a spectrum file, finds the counts in the region of
    interest, and estimate fitted parameters. Adapted from PHYC40870 Space
    Detector Laboratory Curve Fitting With Python, Robert Jeffrey.'''

    if detector == 'NaI':
        channels, counts = get_NaI_data(filename)  # retrieve data

    else:
        start, end = find_limits(
            filename)  # get where data starts and ends in file
        channels, counts = get_data(filename, start, end)  # retrieve data

    channel_min, channel_max = get_roi(detector, source,
                                       no_peaks)  # get region of interest
    _channels, _counts = within_range(channels, counts, channel_min,
                                      channel_max)  # retrive data in roi

    # list Gaussian parameters - interested in first 3
    params = ('mu', 'sigma', 'amplitude', 'slope', 'intercept')

    # restrict Gaussian parameters to be positive
    lower = (0, 0, 0, -np.inf, -np.inf)
    upper = (np.inf, np.inf, np.inf, np.inf, np.inf)
    bounds = (lower, upper)

    # estimate centroid and standard deviation
    initial_guesses = (first_moment(_channels, _counts),
                       second_moment(_channels, _counts), max(_counts), 0, 0)

    # fit gaussian + linear curve to spectrum region of interest: return optimal values for parameters and their covariance
    popt, pcov = fit_spectrum_with_curve_fit(angle,
                                             gaussian_plus_line,
                                             channels,
                                             counts,
                                             channel_range=(channel_min,
                                                            channel_max),
                                             bounds=bounds,
                                             p0=initial_guesses)
    # calculates 1 standard deivation error on parameters
    perr = np.sqrt(np.diag(pcov))

    # plots spectrum with region of interest and fitted Gaussian to region of interest
    # uncomment for spectra
    #fig, ax = plot_result(gaussian_plus_line, detector, source, popt, no_peaks, channels, counts, _channels, _counts, angle, channel_range=(channel_min, channel_max))

    # return ideal fitted parameters and corresponding errors
    return params, popt, perr
コード例 #5
0
def compute_confusion_matrix(model, max_iter=10):

    _, val_generator, _, _ = get_data()

    n_classes = 29

    cf_matrix = np.zeros((n_classes, n_classes))
    i = 0

    for X, y in tqdm(val_generator):

        i += 1
        y_true = np.argmax(y, axis=-1)
        y_predict = model.predict(X)
        y_predict = np.argmax(y_predict, axis=-1)

        cf_matrix += confusion_matrix(y_true,
                                      y_predict,
                                      labels=np.arange(n_classes))

        if i > len(val_generator):
            break

    return cf_matrix
コード例 #6
0
from keras.layers import Input, Conv2D, MaxPool2D, Flatten, Dense, Flatten, Dropout
from keras.models import Model, Sequential
from read import get_data
from save import save_history
from utils import print_score

train_generator, val_generator, image_shape, n_classes = get_data()

net = Sequential()
net.add(
    Conv2D(64,
           kernel_size=4,
           strides=1,
           activation='relu',
           input_shape=image_shape))
net.add(Conv2D(64, kernel_size=4, strides=2, activation='relu'))
net.add(Dropout(0.5))
net.add(Conv2D(128, kernel_size=4, strides=1, activation='relu'))
net.add(Conv2D(128, kernel_size=4, strides=2, activation='relu'))
net.add(Dropout(0.5))
net.add(Conv2D(256, kernel_size=4, strides=1, activation='relu'))
net.add(Conv2D(256, kernel_size=4, strides=2, activation='relu'))
net.add(Flatten())
net.add(Dropout(0.5))
net.add(Dense(512, activation='relu'))
net.add(Dense(n_classes, activation='softmax'))
net.compile(optimizer='adam',
            loss='categorical_crossentropy',
            metrics=["accuracy"])

history = net.fit_generator(train_generator,
コード例 #7
0
ファイル: main.py プロジェクト: arpanmangal/ML-A2
import linear_model 
import gaussian_model
from read import get_data, filter_data

if __name__ == '__main__':
    if (len(sys.argv) < 5):
        print ("Go Away")
        exit(1)

    trainset = sys.argv[1]
    testset = sys.argv[2]
    binaryormulti = sys.argv[3]
    part = sys.argv[4]

    # Read Data
    trainData, testData = get_data (trainset, testset)
    # X, Y, testX, testY = get_data (trainset, testset)
    # print (X.shape, Y.shape, testX.shape, testY.shape)


    if (binaryormulti == '0'):
        # Binary classification
        classA, classB = 1, 2
        X, Y = filter_data (trainData, classA, classB)
        testX, testY = filter_data (testData, classA, classB)

        if (part == 'a'):
            linear_model.binary (X, Y, testX, testY)
        elif (part == 'b'):
            gaussian_model.binary (X, Y, testX, testY)
        elif (part == 'c'):