Matlab train函数


matlab> model = train(training_label_vector, training_instance_matrix [,'liblinear_options', 'col']);

            An m by 1 vector of training labels. (type must be double)
            An m by n matrix of m training instances with n features.
            It must be a sparse matrix. (type must be double)
            A string of training options in the same format as that of LIBLINEAR.
            if 'col' is set, each column of training_instance_matrix is a data instance. Otherwise each row is a data instance.

matlab> [predicted_label, accuracy, decision_values/prob_estimates] = predict(testing_label_vector, testing_instance_matrix, model [, 'liblinear_options', 'col']);
matlab> [predicted_label] = predict(testing_label_vector, testing_instance_matrix, model [, 'liblinear_options', 'col']);

            An m by 1 vector of prediction labels. If labels of test
            data are unknown, simply use any random values. (type must be double)
            An m by n matrix of m testing instances with n features.
            It must be a sparse matrix. (type must be double)
            The output of train.
            A string of testing options in the same format as that of LIBLINEAR.
            if 'col' is set, each column of testing_instance_matrix is a data instance. Otherwise each row is a data instance.

Returned Model Structure

The 'train' function returns a model which can be used for future
prediction.  It is a structure and is organized as [Parameters, nr_class,
nr_feature, bias, Label, w]:

        -Parameters: Parameters (now only solver type is provided)
        -nr_class: number of classes; = 2 for regression
        -nr_feature: number of features in training data (without including the bias term)
        -bias: If >= 0, we assume one additional feature is added to the end
            of each data instance.
        -Label: label of each class; empty for regression
        -w: a nr_w-by-n matrix for the weights, where n is nr_feature
            or nr_feature+1 depending on the existence of the bias term.
            nr_w is 1 if nr_class=2 and -s is not 4 (i.e., not
            multi-class svm by Crammer and Singer). It is
            nr_class otherwise.

If the '-v' option is specified, cross validation is conducted and the
returned model is just a scalar: cross-validation accuracy for
classification and mean-squared error for regression. If the '-C' option
is specified, the best parameter C is found by cross validation. The
returned model is a two dimensional vector, where the first value is
the best C and the second value is the corresponding cross-validation
accuracy. The parameter selection utility is supported by only -s 0
and -s 2.

Result of Prediction

The function 'predict' has three outputs. The first one,
predicted_label, is a vector of predicted labels. The second output,
accuracy, is a vector including accuracy (for classification), mean
squared error, and squared correlation coefficient (for regression).
The third is a matrix containing decision values or probability
estimates (if '-b 1' is specified). If k is the number of classes
and k' is the number of classifiers (k'=1 if k=2, otherwise k'=k), for decision values,
each row includes results of k' binary linear classifiers. For probabilities,
each row contains k values indicating the probability that the testing instance is in
each class. Note that the order of classes here is the same as 'Label'
field in the model structure.

Other Utilities

A matlab function libsvmread reads files in LIBSVM format:

[label_vector, instance_matrix] = libsvmread('data.txt');

Two outputs are labels and instances, which can then be used as inputs
of svmtrain or svmpredict.

A matlab function libsvmwrite writes Matlab matrix to a file in LIBSVM format:

libsvmwrite('data.txt', label_vector, instance_matrix]

The instance_matrix must be a sparse matrix. (type must be double)
For windows, `libsvmread.mexw64' and `libsvmwrite.mexw64' are ready in
the directory `..\windows'.

These codes are prepared by Rong-En Fan and Kai-Wei Chang from National
Taiwan University.


Train and test on the provided data heart_scale:

matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale');
matlab> model = train(heart_scale_label, heart_scale_inst, '-c 1');
matlab> [predict_label, accuracy, dec_values] = predict(heart_scale_label, heart_scale_inst, model); % test the training data

Note that for testing, you can put anything in the testing_label_vector.

For probability estimates, you need '-b 1' only in the testing phase:

matlab> [predict_label, accuracy, prob_estimates] = predict(heart_scale_label, heart_scale_inst, model, '-b 1');

Use the best parameter to train (only supported by -s 0 and -s 2):

matlab> best = train(heart_scale_label, heart_scale_inst, '-C -s 0');

matlab> model = train(heart_scale_label, heart_scale_inst, sprintf('-c %f -s 0', best(1))); % use the same solver: -s 0


Train Time


DescriptionnnCity transportation planners are developing a light rail transit system to carry commuters between the suburbs and the downtown area. Part of their task includes scheduling trains on different routes between the outermost stations and the metro center hub. nnnPart of the planning process consists of a simple simulation of train travel. A simulation consists of a series of scenarios in which two trains, one starting at the metro center and one starting at the outermost station of the same route, travel toward each other along the route. The transportation planners want to find out where and when the two trains meet. You are to write a program to determine those results. nnnThis model of train travel is necessarily simplified. All scenarios are based on the following assumptions. nnAll trains spend a fixed amount of time at each station. nAll trains accelerate and decelerate at the same constant rate. All trains have the same maximum possible velocity. nWhen a train leaves a station, it accelerates (at a constant rate) until it reaches its maximum velocity. It remains at that maximum velocity until it begins to decelerate (at the same constant rate) as it approaches the next station. Trains leave stations with an initial velocity of zero (0.0) and they arrive at stations with terminal velocity zero. Adjacent stations on each route are far enough apart to allow a train to accelerate to its maximum velocity before beginning to decelerate. nBoth trains in each scenario make their initial departure at the same time. nThere are at most 30 stations along any route. nInputnnAll input values are real numbers. Data for each scenario are in the following format. nd1 d2 ... dn 0.0nFor a single route, the list of distances (in miles--there are 5,280 feet in a mile) from each station to the metro center hub,separated by one or more spaces. Stations are listed in ascending order, starting with the station closest to the metro center hub (station 1) and continuing to the outermost station. All distances are greater than zero. The list is terminated by the sentinel value 0.0.nvnThe maximum train velocity, in feet/minute.nsnThe constant train acceleration rate in feet/minute².nmnThe number of minutes a train stays in a station.nnThe series of runs is terminated by a data set which begins with the number -1.0.nOutputnnFor each scenario, output consists of the following labeled data. nnnThe number of the scenario (numbered consecutively, starting with Scenario #1). nThe time when the two trains meet in terms of minutes from starting time. All times must be displayed to one decimal place. nThe distance in miles between the metro center hub and the place where the two trains meet. Distances must be displayed to three decimal places. Also, if the trains meet in a station, output the number of the station where they meet. nPrint a blank line after each scenario.nSample Inputnn15.0 0.0n5280.0n10560.0n5.0n3.5 7.0 0.0n5280.0n10560.0n2.0n3.4 7.0 0.0n5280.0n10560.0n2.0n-1.0nSample OutputnnScenario #1n Meeting time: 7.8 minutesn Meeting distance: 7.500 miles from metro center hubnnScenario #2n Meeting time: 4.0 minutesn Meeting distance: 3.500 miles from metro center hub, in station 1nnScenario #3n Meeting time: 4.1 minutesn Meeting distance: 3.400 miles from metro center hub, in station 1 问答

Train Scheduling


Elihc is a strange country occupying a long, narrow strip of land from north to south. There is only one main railway line in this country. This railway line is a straight line. It starts from the northernmost station, traverses the whole country southward, and finishes at the southernmost station. nnUnfortunately, there are too many trains travelling on this railway line all the time. Long delays, even serious railway accidents are quite common here. Now, Railways Bureau of Elihc decides to solve these problems by scheduling the train in a better way, and hires you to write the program. nnThere are (N+1) stations on this railway line, and they are numbered from 0 to N from north to south. There are also M trains numbered from 0 to M-1. Each train has an initial station, a terminal station, expected time of departure and speed limit. Initially, it parks at its initial station. It departs at the expected time or after the expected time, and is bound for its terminal station. The train has to stop at every station on its route. Different trains may have different speed limits. During the journey, a train should always run within its own speed limit (it is allowed to run at any speed not exceeding the limit anywhere). Compared with the railway line, both stations and trains are so small that they can be regarded as points in your scheduling program. nnFor each pair of adjacent stations, the part of the railway line between them is called a section (stations are not included). The positions of the stations are well designed so that the length of each section is exactly S kilometer. All stations are sufficient to park any number of trains. However, due to some financial difficulties, there is just one track for each section. For safety reasons, trains running on the same section should always follow the rules below. nn1. They are running in the same direction. n2. A train can catch up with, but can never pass any other trains in front of it. nnAn officer of Railways Bureau provides you some scheduling strategies, which you have to use in your program: nn1. During scheduling, trains that are not expected to depart yet should be ignored. If a train has already arrived at its terminal station, it will be ignored forever. n2. Once a train is expected to depart from the original station, or arrives at a station except the terminal one, it stops and waits to move on the next section immediately. n3. When a train is stopping in some station, it will not start to move to the next section until (1) no running train is coming from the opposite direction on this section, and (2) No train with smaller number is stopping and waiting to move on this section from station at either end of this section. n4. A train should always run as fast as it can, without passing any train in front running on the same section. nnNow, please help Railways Bureau to schedule the trains and figure out when each train will reach its terminal station. n输入描述:nnThere are several test cases in the input.n nn The first line contains an integer C (1 ≤ C ≤ 10) -- the number of test cases.n nn Each test case begins with three integers N, M, S (1 ≤ N ≤ 10, 1 ≤ M ≤ 10, 1 ≤ S ≤ 1000), indicating the number of stations, the number of trains and the length of section (in kilometer).n nn Then M lines follow, describing trains from number 0 to M-1 in order, one per line. Each line contains four integers O, T, E, L (0 ≤ O ≤ N, 0 ≤ T ≤ N, O ≠ T, 0 ≤ E ≤ 10000, 1 ≤ L ≤ S), indicate that this train travels from station O to station T, expected to depart at minute E, and its speed limit is L kilometer per minute.nnn输出描述:nnFor each test case, output the arrival time (in minute) of each train one per line, in ascending order of train number. Your answers should be rounded up to the nearest integer.nn输入例子:nn2n1 3 100n0 1 0 5n0 1 20 5n1 0 0 5n2 2 100n0 2 0 4n0 2 2 5nn输出例子:nn20n40n60n50n50 问答