当前位置: 首页>>代码示例>>Python>>正文


Python Features.getLength方法代码示例

本文整理汇总了Python中Features.getLength方法的典型用法代码示例。如果您正苦于以下问题:Python Features.getLength方法的具体用法?Python Features.getLength怎么用?Python Features.getLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Features的用法示例。


在下文中一共展示了Features.getLength方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import Features [as 别名]
# 或者: from Features import getLength [as 别名]
def main():
    arguments = validateInput(sys.argv)
    maxIterations, regularization, stepSize, lmbd, featureSet = arguments
    print maxIterations, regularization, stepSize, lmbd, featureSet

    trainData = readFile('train.csv')
    validationData = readFile('validation.csv')
    testData = readFile('test.csv')

    trainSize = trainData.shape[0]
    validationSize = validationData.shape[0]
    testSize = testData.shape[0]

    print "Number of training examples: " + str(trainSize)

    # Extract Features
    Features.extractFeatures(trainData[:,0], featureSet)
    print "Extracted Features:"
    if featureSet == 1 or featureSet == 3:
        print "Unigram: " + str(Features.getLength(1))
    if featureSet == 2 or featureSet == 3:
        print "Bigram: " + str(Features.getLength(2))

    # Construct Input Matrices X
    xTrain = Features.getMatrix(trainData[:,0], featureSet)
    print "Train Matrix built"

    xValidation = Features.getMatrix(validationData[:,0], featureSet)
    print "Validation Matrix built"

    xTest = Features.getMatrix(testData[:,0], featureSet)
    print "Test Matrix built"
    
    yTrain = extractLabel(trainData[:,1])
    yVailidation = extractLabel(validationData[:,1])
    yTest = extractLabel(testData[:,1])

    # Train the model
    theta = GD(xTrain, yTrain, trainSize, maxIterations, regularization, stepSize, lmbd, featureSet)
    print "Final Theta: " + str(theta)

    # Classify
    trainResult = predict(xTrain, trainSize, theta, featureSet)
    print "Train Result: " + str(trainResult)
    validationResult = predict(xValidation, validationSize, theta, featureSet)
    print "Validation Result: " + str(validationResult)
    testResult = predict(xTest, testSize, theta, featureSet)
    print "Test Result: " + str(testResult)

    # Performance
    print "\nPerformance on training data:"
    performance(trainResult, trainData[:,1])
    print "\nPerformance on validation data:"
    performance(validationResult, validationData[:,1])
    print "\nPerformance on test data:"
    performance(testResult, testData[:,1])
开发者ID:aparolia,项目名称:MLProjects,代码行数:58,代码来源:GD.py

示例2: classify

# 需要导入模块: import Features [as 别名]
# 或者: from Features import getLength [as 别名]
def classify(data, weights, featureSet, algorithm):
    length = Features.getLength(featureSet)
    results = np.zeros(data.shape[0])
    for i in range(data.shape[0]):
        if algorithm == 1:
            vector = Features.getVector(data[i,0], featureSet)
            vector.append(length)
            results[i] = predict_one(weights, vector, 0)
        else:
            vector = Features.getVector(data[i,0], featureSet)
            results[i] = predict_one(weights, vector, length)
        
    return results    
开发者ID:aparolia,项目名称:MLProjects,代码行数:15,代码来源:main.py

示例3: perceptron

# 需要导入模块: import Features [as 别名]
# 或者: from Features import getLength [as 别名]
def perceptron(data, maxIterations, featureSet):
    length = Features.getLength(featureSet)
    rate = 0.1
    weights = np.zeros((1,length+1))
    i = 0
    while i < maxIterations:
        for j in range(data.shape[0]):
            #print weights
            vector = Features.getVector(data[j,0], featureSet)
            vector.append(length)
            sign = predict_one(weights, vector, 0)
            if (data[j,1] == '+' and sign == -1) or (data[j,1] == '-' and sign == 1):
                for index in vector:
                    weights[0,index] = weights[0,index] - rate*sign
        i += 1
    return weights 
开发者ID:aparolia,项目名称:MLProjects,代码行数:18,代码来源:main.py

示例4: winnow

# 需要导入模块: import Features [as 别名]
# 或者: from Features import getLength [as 别名]
def winnow(data, maxIterations, featureSet):
    length = Features.getLength(featureSet)
    threshold = length
    weights = np.ones((1,length))
    i = 0
    while i < maxIterations:
        for j in range(data.shape[0]):
            #print weights
            vector = Features.getVector(data[j,0], featureSet)
            sign = predict_one(weights, vector, threshold)
            if data[j,1] == '+' and sign == -1:
                for index in vector:
                    weights[0,index] = weights[0,index]*2
            elif data[j,1] == '-' and sign == 1:
                for index in vector:
                    weights[0,index] = weights[0,index]/2
        i += 1
    return weights
开发者ID:aparolia,项目名称:MLProjects,代码行数:20,代码来源:main.py

示例5: GD

# 需要导入模块: import Features [as 别名]
# 或者: from Features import getLength [as 别名]
def GD(x, y, m, maxIterations, regularization, stepSize, lmbd, featureSet):

    n = Features.getLength(featureSet) + 1
    theta = np.zeros(n)

    for i in range(0, maxIterations):
        gradient = np.zeros(n)
        for j in range(m):
            if y[j]*dotProduct(theta, x[j]) <= 1:
                for item in x[j]:
                  gradient[item] = gradient[item] + y[j]
        bias = gradient[0]
        if regularization == 'l2':
          gradient = gradient - lmbd*theta
        elif regularization == 'l1':
          gradient = gradient - lmbd*np.sign(theta)
        gradient[0] = bias
        theta = theta + stepSize*gradient
        print "Theta: " + str(theta)

    return theta
开发者ID:aparolia,项目名称:MLProjects,代码行数:23,代码来源:GD.py

示例6: main

# 需要导入模块: import Features [as 别名]
# 或者: from Features import getLength [as 别名]
def main():
    arguments = validateInput(sys.argv)
    algorithm, maxIterations, featureSet = arguments
    print algorithm, maxIterations, featureSet

    # ====================================
    # WRITE CODE FOR YOUR EXPERIMENTS HERE
    # ====================================

    trainData = readFile('train.csv')
    validationData = readFile('validation.csv')
    testData = readFile('test.csv')

    # Extract features
    Features.extractFeatures(trainData[:,0], featureSet)

    length = Features.getLength(featureSet)

    # Learn (Get weight vector)
    if algorithm == 1:
        weights = perceptron(trainData, maxIterations, featureSet)
    else:
        weights = winnow(trainData, maxIterations, featureSet)

    # Classify
    trainResult = classify(trainData, weights, featureSet, algorithm)
    validationResult = classify(validationData, weights, featureSet, algorithm)
    testResult = classify(testData, weights, featureSet, algorithm)

    # Performance
    print "\nPerformance on training data:"
    performance(trainResult, trainData[:,1])
    print "\nPerformance on validation data:"
    performance(validationResult, validationData[:,1])
    print "\nPerformance on test data:"
    performance(testResult, testData[:,1])
开发者ID:aparolia,项目名称:MLProjects,代码行数:38,代码来源:main.py


注:本文中的Features.getLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。