Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

scikit learn - Unable to plot 2 classes in Linear Discriminant Analysis in Python using sklearn

Thanks for reading my question - I would greatly appreciate any input!

I am currently working on a LDA problem in Python - I'm a little new to ML, so that might be one reason why I am running into this problem. Regardless, here it is:

I have a classification problem, for short we'll call it T and non-T. I have a dataframe called PODall which contains my data and their labels (0 (non-T) vs 1 (T)).

I have used the sklearn LDA module to run this analysis. I am able to get a classification accuracy, etc., just unable to actually plot my data for visualization.

I have borrowed code from https://sebastianraschka.com/Articles/2014_python_lda.html#principal-component-analysis-vs-linear-discriminant-analysis, to be able to visualise my data, namely the plotting function:

X_lda_sklearn = sklearn_lda.fit_transform(X, y)

def plot_scikit_lda(X, title):

    ax = plt.subplot(111)
    for label,marker,color in zip(
        range(1,4),('^', 's', 'o'),('blue', 'red', 'green')):

        plt.scatter(x=X[:,0][y == label],
                    y=X[:,1][y == label] * -1, # flip the figure
                    marker=marker,
                    color=color,
                    alpha=0.5,
                    label=label_dict[label])

    plt.xlabel('LD1')
    plt.ylabel('LD2')

    leg = plt.legend(loc='upper right', fancybox=True)
    leg.get_frame().set_alpha(0.5)
    plt.title(title)

    # hide axis ticks
    plt.tick_params(axis="both", which="both", bottom="off", top="off",  
            labelbottom="on", left="off", right="off", labelleft="on")

    # remove axis spines
    ax.spines["top"].set_visible(False)  
    ax.spines["right"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)    

    plt.grid()
    plt.tight_layout
    plt.show()
plot_step_lda()
plot_scikit_lda(X_lda_sklearn, title='Default LDA via scikit-learn')

When I run this, I get the error that X is a one-dimensional array, and therefore X[:,1] errors.

If I add one more class, ie. "pre-T", "T", and "post-T", I am able to plot this visualisation.

If I need to clarify my problem, please let me know!!

Thanks!

~CJ

question from:https://stackoverflow.com/questions/65644516/unable-to-plot-2-classes-in-linear-discriminant-analysis-in-python-using-sklearn

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...