精华内容
下载资源
问答
  • 1.导入数据 letter = readtable("J.txt"); letter将成为一个类似excel的表格 通过点属性的方式,可以获取该列数据,下面利用plot绘制出来: plot(letter.X,letter.Y) 结果: axis equal 可以通过该...

    一、总体工作流程

    二、分类工作流程

    1.导入数据

    letter = readtable("J.txt");

    letter将成为一个类似excel的表格

    通过点属性的方式,可以获取该列数据,下面利用plot绘制出来:

    plot(letter.X,letter.Y)

     结果:

    axis equal

    可以通过该命令自动调节坐标系的比例:

    2.处理数据 

    letter = readtable("M.txt")
    letter.X = 1.5*letter.X;
    plot(letter.X,letter.Y)
    axis equal

    假如表中时间为ms级别,将表中的第一个时间作为起点做差,变成相对时间,最后除以1000,将时间单位变为秒。 

    letter.Time = letter.Time - letter.Time(1)
    letter.Time = letter.Time/1000
    
    plot(letter.Time,letter.X)
    plot(letter.Time,letter.Y)

     

     

    这样做的好处就是将横坐标的数量级拉低。

    3.提取特征

    Calculating Features

     

    What aspects of these letters could be used to distinguish a J from an M or a V? Instead of using the raw signals, the goal is to compute values that distill the entire signal into simple, useful units of information known as features.

    • For the letters J and M, a simple feature might be the aspect ratio (the height of the letter relative to the width). A J is likely to be tall and narrow, whereas an M is likely to be more square.
    • Compared to J and M, a V is quick to write, so the duration of the signal might also be a distinguishing feature.

     

    letter = readtable("M.txt");
    letter.X = letter.X*1.5;
    letter.Time = (letter.Time - letter.Time(1))/1000
    plot(letter.X,letter.Y)
    axis equal
    
    dur = letter.Time(end)%%取最后一个时间
    aratio = range(letter.Y)/range(letter.X)

    其中:

    The range function returns the range of values in an array. That is, range(x) is equivalent to max(x)-min(x).

    Viewing Features

    load featuredata.mat
    features

    scatter(features.AspectRatio,features.Duration)

    gscatter(features.AspectRatio,features.Duration,features.Character)

     

    4.Build a Model

    What is a Classification Model?

    A classification model is a partitioning of the space of predictor variables into regions. Each region is assigned one of the output classes. In this simple example with two predictor variables, you can visualize these regions in the plane.

    There is no single absolute “correct” way to partition the plane into the classes J, M, and V. Different classification algorithms result in different partitions.

    下面开始训练模型:准备训练集和测试数据

    load featuredata.mat
    
    features
    
    testdata

    选用knn模型对训练集进行训练:

    knnmodel = fitcknn(features,"Character")

     

    Making Predictions

    Having built a model from the data, you can use it to classify new observations. This just requires calculating the features of the new observations and determining which region of the predictor space they are in.

    predictions = predict(knnmodel,testdata)

     

    The predict function determines the predicted class of new observations.

    predClass = predict(model,newdata)

    The inputs are the trained model and a table of observations, with the same predictor variables as were used to train the model. The output is a categorical array of the predicted class for each observation in newdata.

    The file featuredata.mat contains a table testdata that has the same variables as features. However, the observations in testdata are not included in features.

    Note that testdata contains observations for which the correct class is known (stored in the Character variable). This gives a way to test your model by comparing the classes predicted by the model with the true classes. The predict function will ignore the Character variable when making predictions from the model.

    Algorithm Options

    By default, fitcknn fits a kNN model with k = 1. That is, the model uses just the single closest known example to classify a given observation. This makes the model sensitive to any outliers in the training data, such as those highlighted in the image above. New observations near the outliers are likely to be misclassified.

    You can make the model less sensitive to the specific observations in the training data by increasing the value of k (that is, use the most common class of several neighbors). Often this will improve the model's performance in general. However, how a model performs on any particular test set depends on the specific observations in that set. 

    knnmodel = fitcknn(features,"Character","NumNeighbors",5)
    predictions = predict(knnmodel,testdata)
    

    最后,对比一下预测结果和测试样本: 

    [predictions,testdata.Character]

    6.Evaluate the Model

    ​load featuredata.mat
    testdata
    knnmodel = fitcknn(features,"Character","NumNeighbors",5);
    predictions = predict(knnmodel,testdata)
    iscorrect = predictions == testdata.Character
    accuracy = sum(iscorrect)/numel(predictions)

     结果:

    iswrong = predictions ~= testdata.Character
    misclassrate = sum(iswrong)/numel(predictions)
    [testdata.Character(iswrong) predictions(iswrong)]

     结果:

    Accuracy and misclassification rate give a single value for the overall performance of the model, but it can be useful to see a more detailed breakdown of which classes the model confuses. A confusion matrix shows the number of observations for each combination of true and predicted class.

    A confusion matrix is commonly visualized by shading the elements according to their value. Often the diagonal elements (the correct classifications) are shaded in one color and the other elements (the incorrect classifications) in another color. You can visualize a confusion matrix by using the confusionchart function.

    confusionchart(ytrue,ypred);

    where ytrue is a vector of the known classes and ypred is a vector of the predicted classes.

    confusionchart(testdata.Character,predictions);

    实例

    load featuredata13letters.mat
    features
    testdata

     

    gscatter(features.AspectRatio,features.Duration,features.Character)
    xlim([0 10])

     

    knnmodel = fitcknn(features,"Character","NumNeighbors",5);
    predictions = predict(knnmodel,testdata);
    
    misclass = sum(predictions ~= testdata.Character)/numel(predictions)
    confusionchart(testdata.Character,predictions);

     

    三、导入和预处理数据

    1.Creating Datastores

    Handwriting Sample Files

    Samples of each letter were collected from many different volunteers. Some provided more than one sample of each letter. Each sample was saved in a separate file and all the files were stored in one folder. The file names have the form

    user003_B_2.txt

    This file would contain the second sample of the letter B written by the volunteer designated “user003”.

     

    Use the datastore function to make a datastore to all files containing the letter M. These files have _M_ in their name and a .txt extension. Store the datastore in a variable called letterds

    通过匹配文件名来打开一系列文件,*代表任意字符

    letterds = datastore("*_M_*.txt")

    You can use the read function to import the data from a file in the datastore.
    data = read(ds);
    Using the read function the first time will import the data from the first file. Using it a second time will import the data from the second file, and so on.

    data = read(letterds)
    
    plot(data.X,data.Y)

     

     

    Calling the read function again imports the data from the next file in the datastore.

    data = read(letterds)
    plot(data.X,data.Y)

    The readall function imports the data from all the files in the datastore into a single variable. 

    data = readall(letterds)
    plot(data.X,data.Y)

     2.Adding a Data Transformantion

    Custom Preprocessing Functions

     

    Typically you will want to apply a series of preprocessing operations to each sample of your raw data. The first step to automating this procedure is to make a custom function that applies your specific preprocessing operations. 

    Transformed

    Currently, you still need to call your function manually. To automate your data importing and preprocessing, you want your datastore to apply this function whenever the data is read. You can do this with a transformed datastore. The transform function takes a datastore and a function as inputs. It returns a new datastore as output. This transformed datastore applies the given function whenever it imports data.

    Normalizing Data

    The location of a letter is not important for classifying it. What matters is the shape. A common preprocessing step for many machine learning problems is to normalize the data.

    Typical normalizations include shifting by the mean (so that the mean of the shifted data is 0) or shifting and scaling the data into a fixed range (such as [-1, 1]). In the case of the handwritten letters, shifting both the x and y data to have 0 mean will ensure that all the letters are centered around the same point.

    letterds = datastore("*_M_*.txt");
    data = read(letterds);
    data = scale(data);
    plot(data.X,data.Y)
    axis equal
    plot(data.Time,data.Y)
    ylabel("Vertical position")
    xlabel("Time")
    
    function data = scale(data)
    data.Time = (data.Time - data.Time(1))/1000;
    data.X = 1.5*data.X;
    data.X = data.X - mean(data.X,"omitnan");
    data.Y = data.Y - mean(data.Y,"omitnan");
    end

    preprocds = transform(letterds,@scale)
    data = readall(preprocds)
    plot(data.Time,data.Y)

     

    四、特征工程

    Statistical Functions

    Measures of Central Tendency

    Function Description
    mean Arithmetic mean
    median Median (middle) value
    mode Most frequent value
    trimmean Trimmed mean (mean, excluding outliers)
    geomean Geometric mean
    harmean Harmonic mean

    Measures of Spread

    Function Description
    range Range of values (largest – smallest)
    std Standard deviation
    var Variance
    mad Mean absolute deviation
    iqr Interquartile range (75th percentile minus 25th percentile)

    Measures of Shape

    Function Description
    skewness Skewness (third central moment)
    kurtosis Kurtosis (fourth central moment)
    moment Central moment of arbitrary order

    1. Calculating Summary Statistic(Quantifying Letter Shapes)

    Descriptive Statistics

    The handwriting samples have all been shifted so they have zero mean in both horizontal and vertical position. What other statistics could provide information about the shape of the letters? Different letters will have different distributions of points. Statistical measures that describe the shape of these distributions could be useful features.

     

    load sampleletters.mat
    plot(b1.Time,b1.X)
    hold on
    plot(b2.Time,b2.X)
    hold off
    plot(b1.Time,b1.Y)
    hold on
    plot(b2.Time,b2.Y)
    hold off

    aratiob = range(b1.Y)/range(b1.X)
    medxb = median(b1.X,"omitnan")
    medyb = median(b1.Y,"omitnan")
    devxb = mad(b1.X)
    devyb = mad(b1.Y)

    aratiov = range(v1.Y)/range(v1.X)
    medxd = median(d1.X,"omitnan")
    medyd = median(d1.Y,"omitnan")
    devxm = mad(m1.X)
    devym = mad(m1.Y)

     

     

    plot(b1.X,b1.Y,b2.X,b2.Y)
    axis([-1 1 -1 1])
    axis equal
    plot(d1.X,d1.Y,d2.X,d2.Y)
    axis([-1 1 -1 1])
    axis equal

    2.Finding Peaks 

    Local minima and maxima are often important features of a signal. The islocalmin and islocalmax functions take a signal as input and return a logical array the same length as the signal.
    idx = islocalmin(x);
    The value of idx is true whenever the corresponding value in the signal is a local minimum.

    load sampleletters.mat
    plot(m1.Time,m1.X)
    
    idxmin = islocalmin(m1.X)
    idxmax = islocalmax(m1.X)
    
    plot(m1.Time,m1.X)
    hold on
    plot(m1.Time(idxmin),m1.X(idxmin),"o")
    plot(m1.Time(idxmax),m1.X(idxmax),"s")
    hold off

    Local minima and maxima are defined by computing the prominence of each value in the signal. The prominence is a measure of how a value compares to the other values around it. You can obtain the prominence value of each point in a signal by obtaining a second output from islocalmin or islocalmax.

    [idx,p] = islocalmin(x);

    [idx,prom] = islocalmin(m1.X);
    plot(m1.Time,prom)

     

     By default, islocalmin and islocalmax find points with any prominence value above 0. This means that a maximum is defined as any point that is larger than the two values on either side of it. For noisy signals you might want to consider only minima and maxima that have a prominence value above a given threshold.

    idx = islocalmin(x,"MinProminence",threshvalue)
    
    When choosing a threshold value, note that prominence values can range from 0 to range(x).
    idxmin = islocalmin(m1.X,"MinProminence",0.1);
    idxmax = islocalmax(m1.X,"MinProminence",0.1);
    nnz(idxmin) 
    sum(idxmin)
    

    3.Computing Derivative 

    Approximating Velocity

    An important aspect of detecting letters written on a tablet is that there is useful information in the rhythm and flow of how the letters are written. To describe the shape of the signals through time, it can be useful to know the velocity of the pen, or, equivalently, the slope of the graph of position through time.

    The raw data recorded from the tablet has only position (not velocity) through time, so velocity must be calculated from the raw data. With discrete data points, this means estimating the velocity by using a finite difference approximation v=Δx/Δt

    load sampleletters.mat
    plot(m2.Time,m2.X)
    grid

    dX = diff(m2.X);
    dT = diff(m2.Time);
    dXdT = dX./dT;
    plot(m2.Time(1:end-1),dXdT)

    maxdx = max(dXdT)
    dYdT = diff(m2.Y)./dT;
    maxdy = max(dYdT)

    Due to limits on the resolution of the data collection procedure, the data contains some repeated values. If the position and the time are both repeated, then the differences are both 0, resulting in a derivative of 0/0 = NaN. However, if the position values are very slightly different, then the derivative will be Inf (nonzero divided by 0).

    Note that max ignores NaN but not Inf because Inf is larger than any finite value. However, for this application, both NaN and Inf can be ignored, as they represent repeated data.

    You can use the standardizeMissing function to convert a set of values to NaN (or the appropriate missing value for nonnumeric data types).

    xclean = standardizeMissing(x,0);
    Here, xclean will be the same as x (including any NaNs), but will have NaN wherever x had the value 0

    dYdT = standardizeMissing(dYdT,Inf);
    maxdy = max(dYdT)

    Try calculating the derivatives of different sample letters. Note that a negative value divided by zero will result in -Inf. You can pass a vector of values to standardizeMissing to deal with multiple missing values at once.

    xclean = standardizeMissing(x,[-Inf 0 Inf]);

    dYdT = standardizeMissing(dYdT,[-Inf 0 Inf]);
    maxdy = max(dYdT)

    4.Calculating Correlations

    Measuring Similarity

    The pair of signals on the left have a significantly different shape to the pair of signals on the right. However, the relationship between the two signals in each pair is similar in both cases: in the blue regions, the upper signal is increasing while the lower signal is decreasing, and vice versa in the yellow regions. Correlation attempts to measure this similarity, regardless of the shape of the signal.

    load sampleletters.mat
    plot(v2.X,v2.Y,"o-")

    For the first half of the letter V, the horizontal and vertical positions have a strong negative linear correlation: when the horizontal position increases, the vertical position decreases proportionally. Similarly, for the second half, the positions have a strong positive correlation: when the horizontal position increases, the vertical position also increases proportionally.
    The corr function calculates the linear correlation between variables.

    C = corr(x,y);
    C = corr(v2.X,v2.Y)

    结果:

    C = NaN
    

    Because both variables contain missing data, C is NaN. You can use the "Rows" option to specify how to avoid missing values.

    C = corr(x,y,"Rows","complete");
    C = corr(v2.X,v2.Y,"Rows","complete")

    结果:

    C = 0.6493
    

    The correlation coefficient is always between -1 and +1.

    • A coefficient of -1 indicates a perfect negative linear correlation
    • A coefficient of +1 indicates a perfect positive linear correlation
    • A coefficient of 0 indicates no linear correlation

    In this case, there is only a moderate correlation because the calculation has been performed on the entire signal. It may be more informative to consider the two halves of the signal separately.

    M = [v2.X(1:11) v2.Y(1:11) v2.X(12:22) v2.Y(12:22)]

    To calculate the correlation between each pair of several variables, you can pass a matrix to the corr function, where each variable is a column of the matrix.

    M = [x y z];
    C = corr(M);
    Cmat = corr(M,"Rows","complete")

    注意:进行列与列之间两两相关计算,由于X与Y的相关性和Y与X的相同,因此,该矩阵是一个对称矩阵。

    The output Cmat is a 4-by-4 matrix of the coefficients of correlation between each pairwise combination of the columns of M. That is, Cmat(j,k) is the correlation of M(:,j) and M(:,k). The matrix is symmetric because the correlation between x and y is the same as the correlation between y and x. The diagonal elements are always equal to 1, because a variable is always perfectly correlated with itself.

    5.Automating Feature Extraction

    Custom Preprocessing Functions

    Once you have determined the features you want to extract, you will need to apply the appropriate calculations to every sample in your data set. The first step to automating this procedure is to make a custom function that takes the data as input and returns an array of features as output.

    Creating a Feature Extraction Function

    Currently the script calculates six features for a given letter (stored in the variable letter). The six features are stored in six separate variables.
    You can use the table function to combine separate variables into a table.

    T = table(x,y,z);
    load sampleletters.mat
    letter = b1;
    
    aratio = range(letter.Y)/range(letter.X)
    idxmin = islocalmin(letter.X,"MinProminence",0.1);
    numXmin = nnz(idxmin)
    idxmax = islocalmax(letter.Y,"MinProminence",0.1);
    numYmax = nnz(idxmax)
    dT = diff(letter.Time);
    dXdT = diff(letter.X)./dT;
    dYdT = diff(letter.Y)./dT;
    avgdX = mean(dXdT,"omitnan")
    avgdY = mean(dYdT,"omitnan")
    corrXY = corr(letter.X,letter.Y,"rows","complete")
    
    featurenames = ["AspectRatio","NumMinX","NumMinY","AvgU","AvgV","CorrXY"];

     By default, the table constructed with the table function has default variable names. To make a table with more useful names, use the 'VariableNames' option.

    T = table(x,y,z,'VariableNames',["X","Y","Z"]);

    Typically you can use either single or double quotes to specify option names. However, because strings can represent data for your table, you need to use single quotes when specifying the 'VariableNames' option.

    feat = table(aratio,numXmin,numYmax,avgdX,avgdY,corrXY)

    feat = table(aratio,numXmin,numYmax,avgdX,avgdY,corrXY,'VariableNames',featurenames)

    At the end of the script, add a local function called extract that takes a single variable, letter, as input and returns a table of features, feat, as output. Copy the code from the beginning of the script and from task 2 to make the body of the function. Test your function by calling it with b2 as input. Store the result in a variable called featB2

    featB2 = extract(b2)
    
    
    function feat = extract(letter)
    aratio = range(letter.Y)/range(letter.X);
    idxmin = islocalmin(letter.X,"MinProminence",0.1);
    numXmin = nnz(idxmin);
    idxmax = islocalmax(letter.Y,"MinProminence",0.1);
    numYmax = nnz(idxmax);
    dT = diff(letter.Time);
    dXdT = diff(letter.X)./dT;
    dYdT = diff(letter.Y)./dT;
    avgdX = mean(dXdT,"omitnan");
    avgdY = mean(dYdT,"omitnan");
    corrXY = corr(letter.X,letter.Y,"rows","complete");
    
    featurenames = ["AspectRatio","NumMinX","NumMinY","AvgU","AvgV","CorrXY"];
    
    feat = table(aratio,numXmin,numYmax,avgdX,avgdY,corrXY,'VariableNames',featurenames);
    end
    
    

    Extracting Features from Multiple Data Files

    Transformed Datastores

     To automate your feature extraction, you want your datastore to apply your extraction function whenever the data is read. As with preprocessing, you can do this with a transformed datastore.

    From the raw data, you will typically need to apply both preprocessing and feature extraction functions. You can apply the transform function repeatedly to add any number of transformations to the datastore to the raw data.

    The script currently applies the scale function to the files in the datastore letterds. The transformed datastore is stored in the variable preprocds.

    letterds = datastore("*.txt");
    preprocds = transform(letterds,@scale)
    
    featds = transform(preprocds,@extract)
    
    function data = scale(data)
    % Normalize time [0 1]
    data.Time = (data.Time - data.Time(1))/(data.Time(end) - data.Time(1));
    % Fix aspect ratio
    data.X = 1.5*data.X;
    % Center X & Y at (0,0)
    data.X = data.X - mean(data.X,"omitnan");
    data.Y = data.Y - mean(data.Y,"omitnan");
    % Scale to have bounding box area = 1
    scl = 1/sqrt(range(data.X)*range(data.Y));
    data.X = scl*data.X;
    data.Y = scl*data.Y;
    end
    
    function feat = extract(letter)
    % Aspect ratio
    aratio = range(letter.Y)/range(letter.X);
    % Local max/mins
    idxmin = islocalmin(letter.X,"MinProminence",0.1);
    numXmin = nnz(idxmin);
    idxmax = islocalmax(letter.Y,"MinProminence",0.1);
    numYmax = nnz(idxmax);
    % Velocity
    dT = diff(letter.Time);
    dXdT = diff(letter.X)./dT;
    dYdT = diff(letter.Y)./dT;
    avgdX = mean(dXdT,"omitnan");
    avgdY = mean(dYdT,"omitnan");
    % Correlation
    corrXY = corr(letter.X,letter.Y,"rows","complete");
    % Put it all together into a table
    featurenames = ["AspectRatio","NumMinX","NumMinY","AvgU","AvgV","CorrXY"];
    feat = table(aratio,numXmin,numYmax,avgdX,avgdY,corrXY,'VariableNames',featurenames);
    end
    

     Use the readall function to read, preprocess, and extract features from all the data files. Store the result in a variable called data.
    There are 12 files and the extract function calculates six features for each. Hence, data should be a 12-by-6 table.
    Visualize the imported data by making a scatter plot of AspectRatio on the x-axis and CorrXY on the y-axis.

    data = readall(featds)
    scatter(data.AspectRatio,data.CorrXY)

     The letters that the data represents are given in the data file names, which are of the form usernnn_X_n.txt. Note that the letter name appears between underscore characters (_X_).
    You can use the extractBetween function to extract text that occurs between given strings.

    extractedtxt = extractBetween(txt,"abc","xyz")

    If txt is the string array ["hello abc 123 xyz","abcxyz","xyzabchelloxyzabc"], then extractedtxt will be [" 123 ","","hello"].

    knownchar = extractBetween(letterds.Files,"_","_")

    For classification problems, you typically want to represent the known label as a categorical variable. You can use the categorical function to convert an array to categorical type.
    xcat = categorical(x)
    By default, the unique values in x will be used to define the set of categories.

    knownchar = categorical(knownchar)

    It is convenient to have the known classes associated with the training data. Recall that you can create new variables in a table by assigning to a variable using dot notation.

    T.newvar = workspacevar
    data.Character = knownchar
    gscatter(data.AspectRatio,data.CorrXY,data.Character)

    五、分类模型

    1.Training a Model

     

    Handwriting Features

    The MAT-file letterdata.mat contains the table traindata which represents feature data for 2906 samples of individual letters. There are 25 features, including statistical measures, correlations, and maxima/minima for the position, velocity, and pressure of the pen.

    load letterdata.mat
    traindata

    histogram(traindata.Character)

    A boxplot is a simple way to visualize multiple distributions.
    boxplot(x,c)
    This creates a plot where the boxes represent the distribution of the values of x for each of the classes in c. If the values of x are typically significantly different for one class than another, then x is a feature that can distinguish between those classes. The more features you have that can distinguish different classes, the more likely you are to be able to build an accurate classification model from the full data set. 

    boxplot(traindata.MADX,traindata.Character)

     Use the command classificationLearner to open the Classification Learner app.

    • Select traindata as the data to use.
    • The app should correctly detect Character as the response variable to predict.
    • Choose the default validation option.
    • Select a model and click the Train button.

    Try a few of the standard models with default options. See if you can achieve at least 80% accuracy.

    Note that SVMs work on binary classification problems (i.e. where there are only two classes). To make SVMs work on this problem, the app is fitting many SVMs. These models will therefore be slow to train.

    Similarly, ensemble methods work by fitting multiple models. These will also be slow to train.

    2.Making Predictions

    The MAT-file letterdata.mat contains traindata, the table of data used to train the model knnmodel. It also contains testdata which is a table of data (with the same features as traindata) that the model has never seen before.
    Recall that you can use the predict function to obtain a model's predictions for new data.

    preds = predict(model,newdata)
    load letterdata.mat
    traindata
    knnmodel = fitcknn(traindata,"Character","NumNeighbors",5,"Standardize",true,"DistanceWeight","squaredinverse");
    testdata

    predLetter = predict(knnmodel,testdata)

    misclassrate = sum(predLetter ~= testdata.Character)/numel(predLetter)

    The response classes are not always equally distributed in either the training or test data. Loss is a fairer measure of misclassification that incorporates the probability of each class (based on the distribution in the data).

    loss(model,testdata)

    testloss = loss(knnmodel,testdata)

     

    3.Investigating Misclassifications(Identifying Common Misclassifications)

    The Confusion Matrix

    For any response class X, you can divide a machine learning model's predictions into four groups:

    • True positives (green) – predicted to be X and was actually X
    • True negatives (blue) – predicted to be not X and was actually not X
    • False positives (yellow) – predicted to be X but was actually not X
    • False negatives (orange) – predicted to be not X but was actually X

     False Negatives

    With 26 letters, you will need to enlarge the confusion chart to make the values visible. If you open the plot in a separate figure, you can resize it as large as you like.

    The row summary shows the false negative rate for each class. This shows which letters the kNN model has the most difficulty identifying (i.e., the letters the model most often thinks are som

     This model has particular difficulty with the letter U, most often mistaking it for M, N, or V.

    Some confusions seem reasonable, such as U/V or H/N. Others are more surprising, such as U/K. Having identified misclassifications of interest, you will probably want to look at some the specific data samples to understand what is causing the misclassification.

    Identifying Common Misclassifications

    When making a confusion chart, you can add information about the false negative and false positive rate for each class by adding row or column summaries, respectively.

    confusionchart(...,"RowSummary","row-normalized");
    load letterdata.mat
    load predmodel.mat
    testdata
    predLetter
    confusionchart(testdata.Character,predLetter);

    confusionchart(testdata.Character,predLetter,"RowSummary","row-normalized");

    Recall that the Files property of a datastore contains the file names of the original data. Hence, when you import the data and extract the features, you can keep a record of which data file is associated with each observation. The string array testfiles contains the file names for the test data. 

    Use the logical array falseneg as an index into testfiles to determine the file names of the observations that were incorrectly classified as the letter U. Store the result in a variable called fnfiles.

    Similarly, use falseneg as an index into predLetter to determine the associated predicted letters. Store the result in a variable called fnpred.

    falseneg = (testdata.Character == "U") & (predLetter ~= "U");
    
    fnfiles = testfiles(falseneg)
    fnpred = predLetter(falseneg)
    

     Use the readtable function to import the data in the fourth element of fnfiles into a table called badU. Visualize the letter by plotting Y against X.

    badU = readtable(fnfiles(4));
    plot(badU.X,badU.Y)
    title("Prediction: "+string(fnpred(4)))

     

     

    4.Investigating Misclassifications(Investigating Features)

    load letterdata.mat
    load predmodel.mat
    traindata
    testdata
    predLetter

    idx = (traindata.Character == "N") | (traindata.Character == "U");
    UorN = traindata(idx,:)
    
    idx = (testdata.Character == "U") & (predLetter ~= "U");
    fnU = testdata(idx,:)

     Categorical variables maintain the full list of possible classes, even when only a subset are present in the data. When examining a subset, it can be useful to redefine the set of possible classes to only those that are in the data. The removecats function removes unused categories.

    cmin = removecats(cfull)
    UorN.Character = removecats(UorN.Character);

     You can use curly braces ({ }) to extract data from a table into an array of a single type.
    datamatrix = datatable{1:10,4:6}
    This extracts the first 10 elements of variables 4, 5, and 6. If these variables are numeric, datamatrix will be a 10-by-3 double array.

    UorNfeat = UorN{:,1:end-1};
    fnUfeat = fnU{:,1:end-1};
    

    parallel coordinates plot shows the value of the features (or “coordinates”) for each observation as a line.
    parallelcoords(data)

    To compare the feature values of different classes, use the "Group" option.
    parallelcoords(data,"Group",classes)

    parallelcoords(UorNfeat,"Group",UorN.Character)

     

    Because a parallel coordinates plot is just a line plot, you can add individual observations using the regular plot function.

    hold on
    plot(fnUfeat(4,:),"k")
    hold off
    

    Use the zoom tool to explore the plot. Note that N and U have similar values for many features. Are there any features that help distinguish these letters from each other? A kNN model uses the distance between observations, where the distance is calculated over all the features. Does this explain why N and U are hard to distinguish, even if there are some features that separate them?

    When plotting multiple observations by groups, it can be helpful to view the median and a range for each group, rather than every individual observation. You can use the "Quantile" option to do this.

    parallelcoords(...,"Quantile",0.2)
    parallelcoords(UorNfeat,"Group",UorN.Character,"Quantile",0.2)

    5. Improving the Model

    Even if your model works well, you will typically want to look for improvements before deploying it for use. Theoretically, you could try to improve your results at any part of the workflow. However, collecting data is typically the most difficult step of the process, which means you often have to work with the data you have.

    Classification workflow

    If you have the option of collecting more data, you can use the insights you have gained so far to inform what new data you need to collect.

    In the handwriting example, volunteers were instructed only to write lower-case letters “naturally”. Investigating the data set reveals that there are often discrete groups within a particular letter, such as a block-letter style and a cursive style. This means that two quite different sets of features can represent the same letter.

    One way to improve the model would be treat these variants as separate classes. However, this would mean having many more than 26 classes. To train such a model, you would need to collect more samples, instruct the volunteers to write both block and cursive style, and label the collected data accordingly.

    Classification workflow 

    If you have the option of collecting more data, you can use the insights you have gained so far to inform what new data you need to collect.

    In the handwriting example, volunteers were instructed only to write lower-case letters “naturally”. Investigating the data set reveals that there are often discrete groups within a particular letter, such as a block-letter style and a cursive style. This means that two quite different sets of features can represent the same letter.

    One way to improve the model would be treat these variants as separate classes. However, this would mean having many more than 26 classes. To train such a model, you would need to collect more samples, instruct the volunteers to write both block and cursive style, and label the collected data accordingly.

    Low accuracy in both your training and testing sets is an indication that your features do not provide enough information to distinguish the different classes. In particular, you might want to look at the data for classes that are frequently confused, to see if there are characteristics that you can capture as new features.

    However, too many features can also be a problem. Redundant or irrelevant features often lead to low accuracy and increase the chance of overfitting – when your model is learning the details of the training rather than the broad patterns. A common sign of overfitting is that your model performs well on the training set but not on new data. You can use a feature selection technique to find and remove features that do not significantly add to the performance of your model.

    You can also use feature transformation to perform a change of coordinates on your features. With a technique such as Principal Component Analysis (PCA), the transformed features are chosen to minimize redundancy and ordered by how much information they contain.Classification workflow

     

    Low accuracy in both your training and testing sets is an indication that your features do not provide enough information to distinguish the different classes. In particular, you might want to look at the data for classes that are frequently confused, to see if there are characteristics that you can capture as new features.

    However, too many features can also be a problem. Redundant or irrelevant features often lead to low accuracy and increase the chance of overfitting – when your model is learning the details of the training rather than the broad patterns. A common sign of overfitting is that your model performs well on the training set but not on new data. You can use a feature selection technique to find and remove features that do not significantly add to the performance of your model.

    You can also use feature transformation to perform a change of coordinates on your features. With a technique such as Principal Component Analysis (PCA), the transformed features are chosen to minimize redundancy and ordered by how much information they contain.

    The Classification Learner app provides an easy way to experiment with different models. You can also try different options. For example, for kNN models, you can vary the number of neighbors, the weighting of the neighbors based on distance, and the way that distance is defined.

    Some classification methods are highly sensitive to the training data, which means you might get very different predictions from different models trained on different subsets of the data. This can be harnessed as a strength by making an ensemble – training a large number of these so-called weak learners on different permutations of the training data and using the distribution of individual predictions to make the final prediction.

    For the handwriting example, some pairs of letters (such as N and V) have many similar features and are distinguished by only one or two key features. This means that a distance-based method such as kNN may have difficulty with these pairs. An alternative approach is to use an ensemble approach known as Error-Correcting Output Coding (ECOC) which use multiple models to distinguish between different binary pairs of classes. Hence, one model can distinguish between N and V, while another can distinguish between N and E, and another between E and V, and so on.Classification workflow

    When trying to evaluate different models, it is important to have an accurate measure of a model's performance. The simplest, and computationally cheapest, way to do validation is holdout – randomly divide your data into a training set and a testing set. This works for large data sets. However, for many problems, holdout validation can result in the test accuracy being dependent on the specific choice of test data.

    You can use k-fold cross-validation to get a more accurate estimate of performance. In this approach, multiple models are trained and tested, each on a different division of the data. The reported accuracy is the average from the different models.

    Accuracy is only one simple measure of the model's performance. It is also important to consider the confusion matrix, false negative rates, and false positive rates. Furthermore, the practical impact of a false negative may be significantly different to that of a false positive. For example, a false positive medical diagnosis may cause stress and expense, but a false negative could be fatal. In these cases, you can incorporate a cost matrix into the calculation of a model's loss.

     Classification workflowClassification workflow

    展开全文
  • 机器学习(1)--线性回归和多项式拟合机器学习(2)逻辑回归 (数学推导及代码实现)机器学习(3)softmax实现Fashion-MNIST分类1.导入MNIST的数据在开始时我们需要导入MNIST的数据,MNIST的数据可以从github上进行...

    机器学习(1)--线性回归和多项式拟合

    机器学习(2)逻辑回归 (数学推导及代码实现)

    机器学习(3)softmax实现Fashion-MNIST分类

    1.导入MNIST的数据

    在开始时我们需要导入MNIST的数据,MNIST的数据可以从github上进行下载Fashion-MNIST数据集,MNIST有四个数据集,分别如下:

    train-images-idx3-ubyte.gz: training set images (9912422 bytes)

    train-labels-idx1-ubyte.gz: training set labels (28881 bytes)

    t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)

    t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

    前两个数据集是训练集,分别是训练集的图片和标签,后两个数据集是测试集,分别是测试集的图片和标签。一共有60000张训练集以及10000张测试集。共10种标签,以0~9数字形式给出,对应的描述如下:

    7ce387c575b2df2a9ab16f495521e299.png

    在第一个数据集中,数据是以2进制给出,其中前128位不包含图片信息,它是关于数据集的一些说明。在图片数据集中(包括数据集和训练集),前面有4个信息,都是由32位二进制数表示,第一个信息是magic number图片位2049,第二个信息是样本数,第三个是行数,第四个是列数。描述如下:

    0a59a9333982f43edbebe1c3bbb173f3.png

    其中训练集样本为60000个,测试集样本为10000个。

    标签数据集,包含两个头文件信息:

    90355ee4516fb922025534d96aa13014.png

    第一个是magic number,第二个是样本个数。下面是matlab导入图片和标签的函数。

    function images1=loadImage()
        filename="train-images-idx3-ubyte";%将下载的数据解解压后去掉后缀名得到。
        fid=fopen(filename,"rb");%打开训练集的图片数据集
        A=fread(fid);%读取训练集中的数据,读取后,数据按照每个字节(8位二进制)转化成了十进制数。
        images1=A(17:length(A));%从17位开始是图片信息
        num=A(5:8);%第二个头文件,样本个数
        row=A(9:12);%第三个头文件,行数
        col=A(13:16);%第四个头文件,列数
    %下面的操作是将32位二进制数转化为十进制
        num=dec2hex(num);
        row=dec2hex(row);
        col=dec2hex(col);
        num1=[];
        row1=[];
        col1=[];
        for i=1:4
            num1=[num1 num(i,:)];
            row1=[row1,row(i,:)];
            col1=[col1 col(i,:)];
        end
        num1=hex2dec(num1);
        col1=hex2dec(col1);
        row1=hex2dec(row1);
    %将图片数据转化为矩阵形式。
        images1=reshape(images1,row1,col1,num1);
        images1=permute(images1,[2,1,3]);
        fclose(fid);
    end

    为了验证转化的是否正确可以利用imwrite函数将矩阵转化为图片看看是否是数字如取第十个矩阵

    a=images1(:,:,50);

    imwrite(uint8(a),'test.tif')%输出文件名为test.tif。

    %imwrite(mat2gray(a), 'test.tif');

    上述得到的数据是

    ,需要转化为
    的形式,以便带入模型中学习,并且需要将数据进行归一化处理
    function [x,x_test,y,y_test]=getData()
        %数据文件同函数在同一目录下
        train_imgs="train-images-idx3-ubyte";      %训练集
        train_labels="train-labels-idx1-ubyte";    %训练集标签
        test_imgs="t10k-images-idx3-ubyte";        %测试集
        test_labels="t10k-labels-idx1-ubyte";      %测试集标签
        x=loadImage(train_imgs);
        x_test=loadImage(test_imgs);
        y=loadLabel(train_labels);
        y_test=loadLabel(test_labels);
        
        x=reshape(x,28*28,60000); 
        x=x/255.0;  %归一化处理
        x=x.';      %转置
        
        x_test=reshape(x_test,28*28,10000);
        x_test=x_test/255.0;
        x_test=x_test.';
    end

    导入标签:

    function labels1=loadLabel()
        filename="train-labels-idx1-ubyte";
        fid=fopen(filename,"rb");
        A=fread(fid);
        labels1=A(9:length(A));
        fclose(fid);
    end

    2.softmax分类

    2.1softmax回归模型[1]

    转化为矩阵形式:

    假设一共有

    个样本,每个样本有
    个特征,
    个标签则:

    为输入样本

    权重

    偏置

    2.2softmax损失函数

    softmax使用交叉熵来计算损失函数

    其中

    表示样本总数,
    表示第
    个样本,
    表示第
    个输出。

    表示第
    个样本的真实标签。只有样本
    标签为
    时,
    ,其他标签位为0;

    损失函数可以表示为:

    对该损失函数进行求导,由于权重是个矩阵,所以涉及到向量对矩阵的求导。

    这里重点是

    的求导, 其中
    ,向量对矩阵的求导就是让向量对矩阵的每一行(或列,这里是对行求导)分别求导,对第
    行求导时:

    对第

    行以外的行求导时:

    又有:

    则:

    假设

    则:

    对于每个样本其标签中只有一个为1,其余为0,则当k标签为1时(其中

    ):

    其中

    ,且只有第k个元素为1其余为0;

    转化为矩阵形式得:

    类似得对b求导得:

    matlab实现:

    %主函数
    function [W,b]=softmax(x,y,p,x_test,y_test)
    len=length(y);
    %将标签表示为热编码得形式
    y1=zeros(len,10);
    % [m,~]=size(x);
    for i=1:len
        y1(i,y(i)+1)=1;
    end
     [m,n]=size(x);
      num_iter=1000;
      alpha=0.1;
      W=ones(n,p);
      b=zeros(1,p);
      for i=1:num_iter
           [Wal,bal]=loss(x,y,W,b);
           W=W-alpha*(Wal)/m;
           b=b-alpha*(bal)/m;
          disp("准确率为:"+test(x_test,y_test,W,b));
      end
    end
    
    %模型函数
    function X=net(X)
     [m,~]=size(X);
     vv=zeros(m,1);
      for i=1:m
          [v,~]=max(X(i,:));
          vv(i,1)=v;
      end
      X=exp(X-vv);
      total=sum(X,2);
      X=X./total;
    end
    
    %损失函数
    function Wal=loss(X,y1,W,b)
     y_=net(X*W+b);
     [m,~]=size(X);
    
       cost=y1-y_;
       Wal=X.'*(y_);
       b=sum(cost);
    end
    
    %测试函数
    function p=test(X,y,W,b)
      X=X/255;
      y_hat=net(X*W+b);
      [~,index]=max(y_hat,[],2);
      sum=0;
      for i=1:length(y)
          if index(i)==y(i)+1
              sum=sum+1;
          end
      end
      p=sum/length(y);
    end

    3.数值得上溢和下溢

    在使用softmax时,分母和分子都用到了

    的指数次方,如果数值过大,计算机在处理时会出现溢出,从而得到NaN的数值,导致在进行梯度计算时出现错误,为防止计算机发生溢出,可将每个样本减去该样本的最大特征值,这样可防止数值过大,同时保证分母有一个数为1,不会发生下溢的情况。

    参考

    1. ^http://zh.d2l.ai/chapter_deep-learning-basics/softmax-regression.html
    展开全文
  • MATLAB官方机器学习入门教程

    千次阅读 2020-04-23 16:22:39
    MATLAB官网入门教程项目描述分类工作流程导入数据1.导入数据2.查看变量处理数据1.矫正单位2.时间归一化特征提取1.分析提取特征2.查看特征建立模型1.创建模型2.做出预测算法选项评估模型1.预测值与真值进行比较2.计算...

    官方教程https://matlabacademy.mathworks.com/

    项目描述

    在这里插入图片描述

    分类工作流程

    导入数据

    1.导入数据

    利用readtable函数导入表格数据或文本数据,然后返回表类型的数据

    %data=readtable('myfile.xlsx')
    
    %导入数据
    letter='J.txt'
    

    2.查看变量

    利用引用的方式查看变量

    %x = mytable.Xdata;
    %y = mytable.Ydata;
    
    %查看letter的X和Y数据,然后可视化
    plot(letter.X,letter.Y)
    
    axis equal 横纵等宽
    

    处理数据

    1.矫正单位

    在这里插入图片描述
    什么意思没懂: 我的理解是写的时候,水平距离写一个单位,等于实际测量时的1.5个单位

    letter = readtable("M.txt")
    letter.X=letter.X*1.5;
    plot(letter.X,letter.Y)
    axis equal
    

    2.时间归一化

    在这里插入图片描述
    没懂: 我的理解是这个时间值,是写一个字母记录的时间数据,左边的图表示的是任一起始时间点开始写完一个字母所用的时间,右边的图表示,(归一化操作)就是都假定从0开始写完一个字母所用的时间,
    可以看到纵坐标表示的是字母书写时垂直坐标的变化

    letter = readtable("M.txt")
    letter.X = 1.5*letter.X;
    plot(letter.X,letter.Y)
    axis equal
    letter.Time = letter.Time - letter.Time(1) %这里表示归一化,即结果只是每个字母持续的时间
    letter.Time = letter.Time/1000 %这里表示换算到毫秒
    

    特征提取

    1.分析提取特征

    分析有哪些可以利用的特征
    在这里插入图片描述
    这里注意到是长宽比,因为写的时候相同字母可以有填满整个数控板,或者只在数控板的一小块区域书写,所以我认为不能单纯的用长度和宽度来衡量,因为没有考虑尺度的问题

    %计算持续时间
    dur=letter.Time(end)
    
    %计算横纵比
    %利用range函数,range函数返回的向量x的取值范围,range(x)=max(x)-min(X),返回的是标量
    aratio=range(letter.Y)/range(letter.X)
    

    2.查看特征

    给定数据包含三个变量,两个特征一个标签,470个收集的数据(来自不同的人)

    load featuredata.mat
    features %将载入的数据重命名为features
    scatter(features.AspectRatio,features.Duration) %画散点图
    

    scattter显示的不是很好,利用gscatter分组显示

    gscatter(features.AspectRatio,features.Duration,features.Character)
    根据标签来利用不同的颜色进行显示
    

    建立模型

    在这里插入图片描述
    注意没有绝对准确的能够将所有类别分清楚的方法

    1.创建模型

    load featuredata.mat
    features
    testdata
    knnmodel=fitcknn(features,"Character")
    %features为导入的表的数据,Character表示表中的标签,(即响应变量,即想让模型预测的类别),范数的输出就是一个拟合好的模型
    %注意这里直接写Character而不是features.Character
    

    2.做出预测

    在这里插入图片描述

    %predClass = predict(model,newdata)
    %输入是一个训练好的模型,以及要预测结果的测试集,注意这个测试集具有与训练集相同的响应变量
    %输出是对每个测试样本的预测结果,组成一个数组
    %给定的测试集中包含正确的分类结果,但是predict函数在预测时会忽略正确的变量,从而进行预测
    
    predictions=predict(knnmodel,testdata)
    

    算法选项

    在这里插入图片描述
    没懂

    knnmodel = fitcknn(features,"Character","NumNeighbors",5)
    predictions = predict(knnmodel,testdata)
    

    评估模型

    1.预测值与真值进行比较

    load featuredata.mat
    testdata
    knnmodel = fitcknn(features,"Character","NumNeighbors",5);
    predictions = predict(knnmodel,testdata)
    
    % 进行评估
    iscorrect=(predictions==testdata.Character)
    

    2.计算精确度与误分类度

    精确度
    iscorrect=(predictions==testdata.Character)
    accuracy=sum(iscorrect)/numel(predictions)
    
    误分类度
    misclassrate=sum(predictions~=testdata.Character)/numel(predictions)
    

    混淆矩阵

    混淆矩阵可视化准确性和误分类性
    利用confusionchart函数可视化

    confusionchart(ytrue,ypred)
    yture 表示已知的真值标签向量
    ypred 表示预测标签(结果)向量
    
    confusionchart(testdata.Character,predictions
    

    导入和预处理数据

    多文本操作

    有一个文件夹存储所有的数据,每个样本对应一个字母文件
    在这里插入图片描述

    1.创建数据存储

    利用datastore函数

    letterds=datastore("*_M_*.txt") %这里使用模糊处理
    

    2.读取数据

    从数据存储中读取数据,利用read函数,调用一次读取一个文件

    data=read(letterds) %data是一个表
    

    读取所有文件,利用readall函数

    data=readall(letterds) %相当于把所有文件中的数据都综合成了一个表
    plot(data.X,data.Y) 
    

    添加数据转换

    1.执行数据预处理

    在这里插入图片描述

    letterds = datastore("*_M_*.txt");
    data = read(letterds);
    data = scale(data);
    plot(data.X,data.Y)
    axis equal
    plot(data.Time,data.Y)
    ylabel("Vertical position")
    xlabel("Time")
    
    function data=scale(data)
        data.Time=(data.Time-data.Time(1))/1000;
        data.X=1.5*data.X;
    end
    

    2.转换后的数据存储

    在这里插入图片描述
    没懂

    preprocds=transform(letterds,@scale) %直接对数据存储进行处理
    
    %读取经处理后的所有数据
    data=readall(preprocds); &变成一个新的表
    plot(data.Time,data.Y)
    

    3.规范化数据

    在这里插入图片描述

    function data=scale(data)
        data.Time=(data.Time-data.Time(1))/1000;
        data.X=1.5*data.X;
        data.X=data.X-mean(data.X,"omitnan"); %控制选项没懂
        data.Y=data.Y-mean(data.Y,"omitnan"); 
    end
    

    特征工程

    统计特性

    在这里插入图片描述

    形状量化

    在这里插入图片描述

    展开全文
  • Matlab机器学习App之Regression Learner使用笔记目录软件与数据准备Regression Learner具体使用合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表...

    Matlab机器学习App之Regression Learner回归分析与模型导出应用


    原创作品,转载请注明出处,谢谢~

    目录

    软件与数据准备

    1. 我之前用的是matlabR2016b,里面没有regression learner这个App,现在使用的是Matlab R2019b,如果你的matlab没有的话可以看看是不是版本问题
    2. 准备需要进行回归分析的数据,我这里是采用的工作空间中的数据
      在这里插入图片描述
      这是我的训练数据,放在工作空间名为gauss_pitch的矩阵中,第一列是变量X1,第二列为变量X2,第三列为因变量Y,一共81行,即81个样本

    Regression Learner具体使用

    1. 打开App新建会话
      在这里插入图片描述
      在这里我们可以选择自己的样本数据,即gauss_pitch这个矩阵,并使用列作为变量,第三列因变量作为我们的响应,前两列作为预测变量,右侧的验证可以根据需求选择交叉验证折数,或者选择留出法验证(一部分作为训练,另一部分用作验证)
      我这里全部采用的如图所示的默认
      在这里插入图片描述
      然后就可以开始我们的会话啦!
    2. 选择自己合适的模型,开始训练!
      在这里插入图片描述
      这里我们可以选择不同的模型对我们的数据进行训练
      我这里选择了高斯过程回归的平方指数模型进行训练!
      在这里插入图片描述
      这里可以非常直观的看到我们训练的记录以及训练的误差,时间等信息
    3. 训练结果分析
      我们可以选择响应图,预测实际图等看模型训练的效果并分析
      在这里插入图片描述
      在这里插入图片描述
      比如这里的响应图,可以根据需要勾选是否显示误差等,X轴的坐标信息也可以更改
    4. 导出训练模型并进一步处理
      这里我遇到一个小问题,因为我的训练数据是三维的,有两个自变量和一个响应,所以我想要画出样本数据的散点图的同时,利用高斯回归模型画出拟合的三维曲面,这就利用到app导出模型的功能。
      在这里插入图片描述
      可以选择是否导出训练数据,这里我选择第一项直接导出模型。
      导出的模型在工作空间被命名为trainedModel,我们可以直接利用该模型,也可以另存为.mat文件方便下次使用该模型(load导入工作空间)
      我这里只用到了trainedModel的predictFcn函数,这里附上写的一段代码
    X1 = -100:0.1:100;
    X2 = 0:0.001:1.5;
    [X1FIT,X2FIT] = meshgrid(X1,X2);
    [m, n] = size(X1FIT);
    
    figure(3);
    scatter3(gauss_pitch(:,1),gauss_pitch(:,2),gauss_pitch(:,3),15,'b','filled');
    hold on;
    Y = zeros(m,n);
    for num = 1:n
        X = [X1FIT(:,num), X2FIT(:,num)];
        Y(:,num) = trainedModel.predictFcn(X);
    end
    mesh(X1FIT, X2FIT, Y);
    

    X1和X2是划分的我感兴趣的范围,希望通过高斯模型绘制曲面的范围,meshgrid的作用就是把X1和X2转变为一个m*n维的矩阵,因此通过for循环,利用模型给预测响应值Y赋值,scatter3绘制出样本数据的散点图;
    predictFcn可以根据待预测数据的输入,预测响应,这里我们的输入是X1和X2的范围;
    最后,mesh,模型预测的曲面就绘制出来了!
    在这里插入图片描述

    最后

    文章中存在的问题欢迎交流指正,如果有更好的使用方法或者使用心得也希望大佬们提出,希望这个笔记也能对大家有所帮助,peace & love~

    展开全文
  • matlab程序内读取该excel.csv,选择导入数据,输出类型选择输出矩阵,(注:选择表作为输出类型则会得到一个table类型的矩阵并包含文字在内,不符合模型训练所需的类型),此时得到一个150×6的矩阵数据,右键选择...
  • 以一个处理手写字母的项目为例子,熟悉Matlab机器学习流程。 导入数据 手写信件被存储为单独的文本文件。每个文件都是用逗号分隔的,并包含四列:时间戳、钢笔的水平位置、钢笔的垂直位置和钢笔的压力。时间戳是...
  • % 导入数据 load sampleletters.mat plot(b1.Time,b1.X) hold on plot(b2.Time,b2.X) hold off plot(b1.Time,b1.Y) hold on plot(b2.Time,b2.Y) hold off % 计算字母b的比例 aratiob = range(b1.Y)/range(b1.X) % ...
  • MATLAB R2018a 统计和机器学习工具箱学习(一) 描述性统计与可视化 该内容被分为三个部分: 一、数据管理(Managing Data);  二、描述性统计(Descriptive Statistics);  三、统计可视化(Statistical ...
  • 我们分享了Pytorch手写数字的训练,当pytorch训练完成后,保存了训练的参数,方便本期使用预训练参数,进行手写数字的识别,我们准备一个手写数字的图片,可以自己在画图软件中,直接写个数字手写数字1、导入第三方...
  • 决策树实现ID3决策树,并在鸢尾花卉Iris数据集上进行5折交叉...(我刚搞了一下,在这没办法上传我的iris.data数据集有需要的网上下载一个就好啦~网上搜索就有)实现过程:首先使用importdata将.data文件导入到Mat...
  • Contents I. 清空环境变量II. 训练集/测试集产生III. 数据归一化IV. BP神经网络创建、训练及仿真测试V....1. 导入数据 load concrete_data.mat 2. 随机产生训练集和测试集 temp = randperm(size(attr
  • 清空命令行和工作区和窗口 ...右击得到导入数据 然后你懂得 具体还有load 、textread、save等命令,以及c语言自带的fopen和fprintf等函数。笔者忙死了,以后写。 程序内部定义 线性间距向量 y = l
  • 机器学习的方法进行数据的多元线性拟合【Matlab程序】思路说明留一法介绍代码和具体过程合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个...
  •  大家好,我是Mac Jiang,今天和大家分享Coursera-NTU-機器學習基石(Machine Learning Foundations)-作业四 Q13-20的MATLAB实现。曾经的代码都是通过C++实现的。可是发现C++实现这些...在数据导入方面也更加...
  • 因为MATLAB自己工具链很庞大,在科学计算和工业界应用非常广泛,所生成的巨量数据也是非常有机器学习的需求。2019b版本在深度学习方面有比较大的更新。比如模型导入工具:net = importKerasNetwork(modelfile)可以...
  • 和其他的Python开发环境相比,它最大的优点就是模仿MATLAB的“工作空间”的功能,可以很方便地观察和修改数组的值。(安装过了就不重新安装了(o°ω°o) Python环境下 在Spyder中导入标准库: import numpy as np...
  • 决策树前言一、实验目的二、实验内容1、导入数据2、划分训练集与测试集3、多元线性回归3.1 多元线性回归系数矩阵3.2 多元线性回归可视化3.3 测试集检验4、线性判别分析4.1 编写线性判别分析4.2 线性判别分析可视化...
  • MATLAB:如何在自定义函数中装载结构体数据不出错 在做机器学习、神经网络、滤波器等等方面我们...在菜单栏中选择导入数据后,右下角选择生成MATLAB代码 此时matlab会自动生成函数文件,代码如下 function importfil...
  • 机器学习实验-logistic模型1 实验原理与代码1.1初始化+数据可视化1.2 计算代价和梯度1.3 通过fminunc函数进行优化1.4 预测1.5 主函数...(1)查阅matlab图像处理工具箱中函数load,熟悉其基本语法学会导入数据; (...
  • 决策树前言一、实验目的二、实验内容1、导入数据2、将数据分为训练集与测试集3、采用训练集建立决策树三、实验结果1、Cart算法绘制决策树后记 前言 五一假期跳过了一次实验课,结果这周五上实验课老师竟然直接跳过了...
  • matlab实现神经网络前言一、实验目的二、实验内容1、导入数据2、划分训练集与测试集3、构建神经网络3.1 函数参数分析3.2 输入、输出矩阵3.3 调用newff()4、BP训练算法5、网络仿真三、实验结果四、拓展实验五、实验...
  • 导入包 import numpy as np import pandas as pd import sklearn.svm import seaborn as sns import scipy.io as sio #读取matlab的mat数据 import matplotlib.pyplot as...1.1 导入数据 mat = sio.loadmat(r'D:\pyt...
  • 1.1 导入数据集   ex3data1.mat中获得一个包含5000个手写数字训练示例的数据集。.mat格式表示数据已保存为原生Octave / MATLAB矩阵格式,而不是像csv文件那样的文本(ASCII)格式。可以使用l.
  • matplotib.pyplot库,类似于MATLAB ,提供多种将数据绘制成图的方法。由于后续章节的一些数据分析结果要用它实现可视化,因此我们有必要用一个简短的例子,解释后面即将用到的所有matplotlib代码:导入该库之后(导入为plt...
  • 多分类问题一.复习二 .题目三.... 它是在MATLAB的本机格式,所以要加载它在Python,我们需要使用一个SciPy工具。 三.代码 1.导入需要的包 import numpy as np import pandas as pd import matplotlib.
  • 机器学习----SVM初学libsvm使用(1)

    千次阅读 2018-03-23 16:39:37
    %%导入数据 load BreastTissue_data.mat %%随机产生训练集和测试集 n=randperm(size(matrix,1));%%matlab中,size(A,1)返回的是矩阵A所对应的行数。%%size(A,2) 该语句返回的时矩阵A的列数。 randperm函数 随机打乱...
  • 首先导入依赖 import numpy as np import matplotlib.pyplot as plt import scipy.io as sio 加载数据, 加载的数据是一个字典 data = sio.loadmat('ex3data1.mat') data,type(data) ({'__header__': b'MATLAB 5.0 ...
  • 任务描述 数据集为手写数字,是一个多分类...首先导入数据,给出的数据集是 Matlab 的 .mat 格式,每个样本是 20 * 20 的灰度图,共 5000 个样本: import numpy as np from scipy.io import loadmat import s...
  • MATLAB自带分类模型APP——classification learner的使用MATLAB对常用的机器学习的分类模型做了集合,也就是形成了它自带的classification learner APP,今天简单概述一下该APP的使用步骤。 1、导入数据 在进行APP的...

空空如也

空空如也

1 2 3
收藏数 46
精华内容 18
关键字:

matlab导入数据机器学习

matlab 订阅