[Python] Use of classifiers on a simple dataset. Error using score

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I 'm new to ML and I am trying to use classifiers on a simple dataset


    labels = [1, 0, 1, 0, 1, 0]
    data = pd.DataFrame({
    'Delta': delta_energy,
    'Theta': theta_energy,
    'Alpha': alpha_energy,
    'Beta': beta_energy,
    'Gamma': gamma_energy,
    'Label': labels
    })


    but I've encountered an erorr - AttributeError: 'NoneType' object has no attribute 'split' after trying to run this code

    clf_index = 5
    feature_indexes = [0,3]
    names = [
    "Nearest Neighbors",
    "Linear SVM",
    "RBF SVM",
    "Gaussian Process",
    "Decision Tree",
    "Random Forest",
    "Neural Net",
    "AdaBoost",
    "Naive Bayes",
    "QDA",
    ]
    classifiers = [
    KNeighborsClassifier(3),
    SVC(kernel="linear", C=0.025, random_state=42),
    SVC(gamma=2, C=1, random_state=42),
    GaussianProcessClassifier(1.0 * RBF(1.0), random_state=42),
    DecisionTreeClassifier(max_depth=5, random_state=42),
    RandomForestClassifier(
    max_depth=5, n_estimators=10, max_features=1, random_state=42
    ),
    MLPClassifier(max_iter=1000, random_state=42),
    AdaBoostClassifier(algorithm="SAMME", random_state=42),
    GaussianNB(),
    QuadraticDiscriminantAnalysis(),
    ]
    table_data = (data.values[:,feature_indexes], data[data.columns[clf_index]])
    datasets = [table_data]
    figure = plt.figure(figsize=(27, 3))
    i = 1
    for ds_cnt, ds in enumerate(datasets):
    X, y = ds
    X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
    )

    x_range = abs(X[:, 0].max()) - abs(X[:, 0].min())
    y_range = abs(X[:, 1].max()) - abs(X[:, 1].min())

    x_min, x_max = X[:, 0].min() - 0.05*x_range, X[:, 0].max() + 0.05*x_range
    y_min, y_max = X[:, 1].min() - 0.05*y_range, X[:, 1].max() + 0.05*y_range

    # just plot the dataset first, red and blue = 0000FF color. Red = 0. Blue = 1 status
    cm = plt.cm.RdBu
    cm_bright = ListedColormap(["#FF0000", "#0000FF"])
    ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
    if ds_cnt == 0:
    ax.set_title("Input data")
    # Plot the training points
    ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors="k")
    # Plot the testing points
    ax.scatter(
    X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6, edgecolors="k"
    )
    ax.set_xlim(x_min, x_max)
    ax.set_ylim(y_min, y_max)
    ax.set_xlabel(data.columns[feature_indexes[0]])
    ax.set_ylabel(data.columns[feature_indexes[1]])
    i += 1
    for name, clf in zip(names, classifiers):
    ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
    clf = make_pipeline(StandardScaler(), clf)
    clf.fit(X_train, y_train)
    score = clf.score(X_test, y_test)
    DecisionBoundaryDisplay.from_estimator(
    clf, X, cmap=cm, alpha=0.8, ax=ax, eps=0.5
    )
    # Plot the training points
    ax.scatter(
    X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors="k"
    )
    # Plot the testing points
    ax.scatter(
    X_test[:, 0],
    X_test[:, 1],
    c=y_test,
    cmap=cm_bright,
    edgecolors="grey",
    alpha=0.4,
    )
    ax.set_xlim(x_min, x_max)
    ax.set_ylim(y_min, y_max)
    ax.set_xticks(())
    ax.set_yticks(())
    if ds_cnt == 0:
    ax.set_title(name)
    ax.text(
    x_max - x_range*0.1,
    y_min + y_range*0.1,
    ("%.2f" % score).lstrip("0"),
    size=25,
    horizontalalignment="right",
    )
    i += 1

    plt.tight_layout()
    plt.show()


    in this specific place: clf = make_pipeline(StandardScaler(), clf) clf.fit(X_train, y_train) How can i fix it? i ve tried to change dataset but it still doesn't work

    Continue reading...

Compartilhe esta Página