示例#1
0
def pcaSift():
    inputArr = inputArray.getInputsInArrayForm(0, "sift", True)
    compList = np.asarray(inputArr)
    # row - data
    # colum - variable
    findCov = np.array(inputArr)
    covMat = np.cov(findCov, None, False, False, 0)

    w = np.linalg.eigh(covMat)[0]
    v = np.linalg.eigh(covMat)[1]

    idx = w.argsort()[::-1]
    w = w[idx]
    w = np.diag(w)
    v = v[:, idx]

    reduceToD = input("Enter the D value to reduce the dimensions: ")

    extracted = v[:, :int(reduceToD)]
    extracted = extracted.T
    outputArr = np.dot(extracted, compList.T)

    outputArr = outputArr.T
    outputWriter.formatOutput("sift", outputArr)
    outputWriter.writeDComp("siftDComp.spca", extracted)
    print("Reduced dimension size: ", outputArr.shape)
    print("Wrote output into the file..")
示例#2
0
def pcaHist():
    inputArr = inputArray.getInputsInArrayForm(0, "hist", True)
    compList = np.asarray(inputArr)
    # row - data
    # colum - variable
    findCov = np.array(inputArr)
    covMat = np.cov(findCov, None, False, False, 0)

    w = np.linalg.eigh(covMat)[0]
    v = np.linalg.eigh(covMat)[1]

    idx = w.argsort()[::-1]
    w = w[idx]
    w = np.diag(w)
    v = v[:, idx]
    print(idx)

    reduceToD = input("Enter the D value to reduce the dimensions: ")

    extracted = v[:, :int(reduceToD)]
    extracted = extracted.T
    outputArr = np.dot(extracted, compList.T)

    outputArr = outputArr.T

    fOpen = open("histPca.out", "w")
    for i in range(len(outputArr)):
        for j in range(len(outputArr[i])):
            fOpen.write("%s," % outputArr[i][j])
        fOpen.write("\n")
    fOpen.close()

    print("Reduced dimension size: ", outputArr.shape)
    print("Wrote output into the file..")
示例#3
0
def kmeansHist():
    print("Enter the number of dimensions ")
    d = int(input())
    histArray = inputArray.getInputsInArrayForm(0, "hist", True)
    kmeans = KMeans(n_clusters=d, random_state=0).fit(histArray)
    clusterArray = kmeans.cluster_centers_
    reducedVectors = []
    for i in range(len(histArray)):
        newVector = []
        for j in range(d):
            p = numpy.dot(histArray[i], clusterArray[j])
            newVector.append(p)
        reducedVectors.append(newVector)
    outputWriter.writeDComp("histDComp.hkmeans", clusterArray)
    print("Output vector generated of dimension:")
    print(len(reducedVectors), len(reducedVectors[0]))
    outputWriter.formatOutput("kmeansHist", reducedVectors)