[Python] How to input a matrix and receive value (or array of values) using machine learning...

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a bunch of binary 8x15 matrixes and I want to determine which index (row and column) of the matrix breaks the pattern.

    Say I have the following matrix (simplified to 5x5 for ease, because the problem remains the same):

    1 1 1 1 0
    1 1 1 1 0
    1 1 0 1 0
    1 1 1 0 0
    1 1 1 0 0


    This matrix should return [3, 3], because that position breaks the pattern of filling the matrix column by column with 1.

    The following example should return [3, 1]:

    1 1 1 1 0
    1 1 1 1 0
    0 1 1 0 0
    1 1 1 0 0
    1 1 1 0 0


    The following example, however, doesn't show that problem, so it should return [-1, -1] or something (suggestions?):

    1 1 1 1 0
    1 1 1 1 0
    1 1 1 1 0
    1 1 1 1 0
    1 1 1 0 0


    How should I approach this problem using machine learning?

    Currently, I have a pandas dataframe with 2 columns: the first one contains the matrix (so an array of 8 arrays of 15 positions; therefore, 8x15 matrix), while the second one contains the expected return / result:

    State Result
    0 [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]... [-1, -1]
    1 [[1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1]... [-1, -1]
    2 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]... [-1, -1]
    3 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]... [-1, -1]
    4 [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]... [-1, -1]


    dtypes:

    State object
    Result object
    dtype: object


    So one entry of the dataframe above would have the first column with a value like:

    [[1, 1, 1, 1, 1, 0, 0, 0],
    [1, 1, 1, 1, 1, 0, 0, 0],
    [1, 1, 1, 1, 1, 0, 0, 0],
    [1, 1, 1, 1, 0, 0, 0, 0],
    [1, 1, 1, 1, 0, 0, 0, 0],
    [1, 1, 0, 1, 0, 0, 0, 0],
    [1, 1, 1, 1, 0, 0, 0, 0],
    [1, 1, 1, 1, 0, 0, 0, 0]]


    And its second column would have the value [6, 3].

    Then I split it in 2 dataframes, X and y:

    X = states_data.drop(columns=['Result'])
    y = states_data[['Result']]


    After that, I tried to use DecisionTreeClassifier with sklearn to solve the problem:

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
    X_train = X_train.reset_index(drop=True)
    X_test = X_test.reset_index(drop=True)
    y_train = y_train.reset_index(drop=True)
    y_test = y_test.reset_index(drop=True)

    model = DecisionTreeClassifier()
    model.fit(X_train, y_train)
    predictions = model.predict(X_test)
    predictions = pd.DataFrame(predictions, columns = ['Result'])

    score = accuracy_score(y_test['Result'], predictions['Result'])
    print(score)


    However, in this line:

    model.fit(X_train, y_train)


    I get this error:

    TypeError: float() argument must be a string or a number, not 'list'


    Can someone help me? What am I doing wrong?

    Some additional information:

    X_train.dtypes returns:

    State object
    dtype: object


    y_train.dtypes returns:

    Result object
    dtype: object


    Both X_train.shape and y_train.shape return:

    (543, 1)

    Continue reading...

Compartilhe esta Página