コード例 #1
0
def main():
    args = get_args()
    check_args(args)

    if args.mode.lower() == "train":
        # Load the training data.
        X, y = load_data(args.data)

        # Create the model.
        # TODO: Add other algorithms as necessary.
        if args.algorithm.lower() == 'useless':
            model = models.Useless()
        elif args.algorithm.lower() == 'sumoffeatures':
            model = models.SumOfFeatures()
        elif args.algorithm.lower() == 'perceptron':
            model = models.Perceptron()
        else:
            raise Exception('The model given by --model is not yet supported.')

#########################################QUESTION!
        I = args.online_training_iterations
        n = args.online_learning_rate

        # Train the model.
        if args.algorithm.lower() == 'perceptron':
                model.fit(X, y,n,I)
        else:
                model.fit(X, y)

        # Save the model.
        try:
            with open(args.model_file, 'wb') as f:
                pickle.dump(model, f)
        except IOError:
            raise Exception("Exception while writing to the model file.")        
        except pickle.PickleError:
            raise Exception("Exception while dumping model pickle.")
            
    elif args.mode.lower() == "test":
        # Load the test data.
        X, y = load_data(args.data)

        # Load the model.
        try:
            with open(args.model_file, 'rb') as f:
                model = pickle.load(f)
        except IOError:
            raise Exception("Exception while reading the model file.")
        except pickle.PickleError:
            raise Exception("Exception while loading model pickle.")

        # Compute and save the predictions.
        y_hat = model.predict(X)
        invalid_label_mask = (y_hat != 0) & (y_hat != 1)
        if any(invalid_label_mask):
            raise Exception('All predictions must be 0 or 1, but found other predictions.')
        np.savetxt(args.predictions_file, y_hat, fmt='%d')
            
    else:
        raise Exception("Mode given by --mode is unrecognized.")
コード例 #2
0
def main():
    args = get_args()
    check_args(args)

    if args.mode.lower() == "train":
        # Load the training data.
        X, y = load_data(args.data)

        # Create and train the model.
        if args.algorithm.lower() == 'useless':
            model = models.Useless()
            model.fit(X, y)
        elif args.algorithm.lower() == 'lambda_means':
            model = models.LambdaMeans()
            model.fit(X,
                      y,
                      lambda0=args.cluster_lambda,
                      iterations=args.clustering_training_iterations)
        elif args.algorithm.lower() == 'stochastic_k_means':
            model = models.StochasticKMeans()
            model.fit(X,
                      y,
                      num_clusters=args.number_of_clusters,
                      iterations=args.clustering_training_iterations)
        else:
            raise Exception('The model given by --model is not yet supported.')

        # Save the model.
        try:
            with open(args.model_file, 'wb') as f:
                pickle.dump(model, f)
        except IOError:
            raise Exception("Exception while writing to the model file.")
        except pickle.PickleError:
            raise Exception("Exception while dumping model pickle.")

    elif args.mode.lower() == "test":
        # Load the test data.
        X, y = load_data(args.data)

        # Load the model.
        try:
            with open(args.model_file, 'rb') as f:
                model = pickle.load(f)
        except IOError:
            raise Exception("Exception while reading the model file.")
        except pickle.PickleError:
            raise Exception("Exception while loading model pickle.")

        # Compute and save the predictions.
        y_hat = model.predict(X)
        np.savetxt(args.predictions_file, y_hat, fmt='%d')

    else:
        raise Exception("Mode given by --mode is unrecognized.")
コード例 #3
0
def main():
    args = get_args()
    check_args(args)

    if args.mode.lower() == "train":
        # Load the training data.
        X, y = load_data(args.data)

        if args.algorithm.lower() == 'useless':
            model = models.Useless()
        elif args.algorithm.lower() == 'logisticregression':
            model = models.LogisticRegression(args.online_learning_rate, 
                args.gd_iterations, args.num_features_to_select)
        else:
            raise Exception('The model given by --model is not yet supported.')

        # Train the model.
        model.fit(X, y)

        # Save the model.
        try:
            with open(args.model_file, 'wb') as f:
                pickle.dump(model, f)
        except IOError:
            raise Exception("Exception while writing to the model file.")        
        except pickle.PickleError:
            raise Exception("Exception while dumping model pickle.")
            
    elif args.mode.lower() == "test":
        # Load the test data.
        X, y = load_data(args.data)

        # Load the model.
        try:
            with open(args.model_file, 'rb') as f:
                model = pickle.load(f)
        except IOError:
            raise Exception("Exception while reading the model file.")
        except pickle.PickleError:
            raise Exception("Exception while loading model pickle.")

        # Compute and save the predictions.
        y_hat = model.predict(X)
        invalid_label_mask = (y_hat != 0) & (y_hat != 1)
        if any(invalid_label_mask):
            raise Exception('All predictions must be 0 or 1, but found other predictions.')
        np.savetxt(args.predictions_file, y_hat, fmt='%d')
            
    else:
        raise Exception("Mode given by --mode is unrecognized.")
コード例 #4
0
ファイル: classify.py プロジェクト: EthanTang0115/ML601.475
def main():
    args = get_args()
    check_args(args)
    
    if args.mode.lower() == "train":
        # Load the training data.
        X, y = load_data(args.data)
        
        # Create the model.
        # TODO: Add other algorithms as necessary.
        if args.algorithm.lower() == 'sumoffeatures':
            model = models.SumOfFeatures()
        elif args.algorithm.lower() == 'perceptron':
            model = models.Perceptron()
        elif args.algorithm.lower() == 'useless':
            model = models.Useless()
        elif args.algorithm.lower() == 'logisticregression':
            model = models.LogisticRegression()
        else:
            raise Exception('The model given by --model is not yet supported.')

        # Select features.
        num_orig_features = X.shape[1]
        index_array = np.empty(1)
        if args.num_features_to_select > 0:
            index_array = select_features(X, y, args.num_features_to_select)
            index_array = np.sort(index_array)
            X_selected = X[:, index_array[0]]
            for i in range(index_array.shape[0]):
                if i != 0:
                    X_selected = hstack([X_selected, X[:,index_array[i]]])
            X = X_selected
        
        # Train the model.
        if args.algorithm.lower() == 'perceptron':
            model.fit(X ,y, args.online_learning_rate, args.online_training_iterations)
        elif args.algorithm.lower() == 'logisticregression':
            model.fit(X, y, args.online_learning_rate, args.gd_iterations, num_orig_features, index_array)
        else:
            model.fit(X, y)

        # Save the model.
        try:
            with open(args.model_file, 'wb') as f:
                pickle.dump(model, f)
        except IOError:
            raise Exception("Exception while writing to the model file.")        
        except pickle.PickleError:
            raise Exception("Exception while dumping model pickle.")
            
    elif args.mode.lower() == "test":
        # Load the test data.
        X, y = load_data(args.data)
        
        # Load the model.
        try:
            with open(args.model_file, 'rb') as f:
                model = pickle.load(f)
        except IOError:
            raise Exception("Exception while reading the model file.")
        except pickle.PickleError:
            raise Exception("Exception while loading model pickle.")

        # Compute and save the predictions.
        y_hat = model.predict(X)
        invalid_label_mask = (y_hat != 0) & (y_hat != 1)
        if any(invalid_label_mask):
            raise Exception('All predictions must be 0 or 1, but found other predictions.')
        np.savetxt(args.predictions_file, y_hat, fmt='%d')
            
    else:
        raise Exception("Mode given by --mode is unrecognized.")
コード例 #5
0
ファイル: classify.py プロジェクト: bigdayangyu/ml-project
def main():
    args = get_args()
    check_args(args)

    if args.mode.lower() == "train":
        # Load the training data.
        X, y = load_data(args.data)
        # print(type(args.data))

        # Create the model.
        # TODO: Add other algorithms as necessary.
        models.Perceptron(args.online_learning_rate, args.online_training_iterations)
  
        models.Logistic(args.online_learning_rate, args.online_training_iterations)
        models.nb(args.independent_mode, args.training_iterations, args.latent_states)

    

        # Create model for each algorithm 
        if args.algorithm.lower() == 'useless':
            model = models.Useless()
        elif args.algorithm.lower() == 'perceptron':
            model = models.Perceptron(args.online_learning_rate,args.online_training_iterations)
        elif args.algorithm.lower() == 'logistic':
            model = models.Logistic(args.online_learning_rate,args.online_training_iterations)
        elif args.algorithm.lower() == 'pegasos':
            model = models.Pegasos(args.online_learning_rate, args.online_training_iterations, args.pegasos_lambda)
        elif args.algorithm.lower() == 'nb':
            model = models.nb(args.independent_mode, args.training_iterations, args.latent_states)


        else:
            raise Exception('The model given by --model is not yet supported.')

        # Train the model.
        model.fit(X, y)

        # Save the model.
        try:
            with open(args.model_file, 'wb') as f:
                pickle.dump(model, f)
        except IOError:
            raise Exception("Exception while writing to the model file.")        
        except pickle.PickleError:
            raise Exception("Exception while dumping model pickle.")
            
    elif args.mode.lower() == "test":
        # Load the test data.
        X, y = load_data(args.data)

        # Load the model.
        try:
            with open(args.model_file, 'rb') as f:
                model = pickle.load(f)
        except IOError:
            raise Exception("Exception while reading the model file.")
        except pickle.PickleError:
            raise Exception("Exception while loading model pickle.")

        # Compute and save the predictions.
        y_hat = model.predict(X)
        # invalid_label_mask = (y_hat != 0) & (y_hat != 1)
        # if any(invalid_label_mask):
        #     raise Exception('All predictions must be 0 or 1, but found other predictions.')
        if np.issubdtype(type(y[0]), np.dtype(int)):
            np.savetxt(args.predictions_file, y_hat, fmt='%d')
        else:
            np.savetxt(args.predictions_file, y_hat, fmt='%s')

            
    else:
        raise Exception("Mode given by --mode is unrecognized.")
コード例 #6
0
def main():
    args = get_args()
    check_args(args)

    if args.mode.lower() == "train":
        # Load the training data.
        X, y = load_data(args.data)

        # Create the model.
        # TODO: Add other algorithms as necessary.
        if args.algorithm.lower() == 'adaboost':
            model = models.Adaboost(args.num_boosting_iterations)
            model.fit(X, y)
        elif args.algorithm.lower() == 'logisticregression':
            model = models.LogisticRegression(args.online_learning_rate,
                                              args.num_features_to_select,
                                              args.gd_iterations)
            model.fit(X, y)
        elif args.algorithm.lower() == 'sumoffeatures':
            model = models.SumOfFeatures()
            model.fit(X, y)
        elif args.algorithm.lower() == 'perceptron':
            model = models.Perceptron(args.online_learning_rate,
                                      args.online_training_iterations)
            model.fit(X, y)
        elif args.algorithm.lower() == 'lambda_means':
            model = models.LambdaMeans()
            model.fit(X,
                      y,
                      lambda0=args.cluster_lambda,
                      iterations=args.clustering_training_iterations)
        elif args.algorithm.lower() == 'stochastic_k_means':
            model = models.StochasticKMeans()
            model.fit(X,
                      y,
                      num_clusters=args.number_of_clusters,
                      iterations=args.clustering_training_iterations)
        elif args.algorithm.lower() == 'useless':
            model = models.Useless()
            model.fit(X, y)
        else:
            raise Exception('The model given by --model is not yet supported.')

        # Save the model.
        try:
            with open(args.model_file, 'wb') as f:
                pickle.dump(model, f)
        except IOError:
            raise Exception("Exception while writing to the model file.")
        except pickle.PickleError:
            raise Exception("Exception while dumping model pickle.")

    elif args.mode.lower() == "test":
        # Load the test data.
        X, y = load_data(args.data)

        # Load the model.
        try:
            with open(args.model_file, 'rb') as f:
                model = pickle.load(f)
        except IOError:
            raise Exception("Exception while reading the model file.")
        except pickle.PickleError:
            raise Exception("Exception while loading model pickle.")

        # Compute and save the predictions.
        y_hat = model.predict(X)
        invalid_label_mask = (y_hat != 0) & (y_hat != 1)
        if any(invalid_label_mask):
            raise Exception(
                'All predictions must be 0 or 1, but found other predictions.')
        np.savetxt(args.predictions_file, y_hat, fmt='%d')

    else:
        raise Exception("Mode given by --mode is unrecognized.")