按照书上的代码将我自己的数据分为X,y。再代入书上的后续的回归代码。出现报错。
import csv
import pandas
import numpy as np
import matplotlib.pyplot as plt
with open('data.csv','r')as f:
reader_N2 = csv.reader(f)
data_N2 = []
for row in reader_N2:
float_row = [float (value) for value in row]
data_N2.append(float_row)
print(data_N2)
X_train = [row[:10] for row in data_N2]
Y_train = [row[10:] for row in data_N2]
print(X_train,Y_train)
X_train = np.array(X_train)
Y_train = np.array(Y_train)
X_train,X_test,Y_train,Y_test = X_train[:1600],X_train[1600:],Y_train[:1600],Y_train[1600:]
print(X_train.shape)
print(Y_train.shape)
print(X_test.shape)
print(Y_test.shape)
X_train_1 = np.hsplit(X_train,10)[0]
X_train_2 = np.hsplit(X_train,10)[1]
X_train_3 = np.hsplit(X_train,10)[2]
X_train_4 = np.hsplit(X_train,10)[3]
X_train_5 = np.hsplit(X_train,10)[4]
X_train_6 = np.hsplit(X_train,10)[5]
X_train_7 = np.hsplit(X_train,10)[6]
X_train_8 = np.hsplit(X_train,10)[7]
X_train_9 = np.hsplit(X_train,10)[8]
X_train_10 = np.hsplit(X_train,10)[9]
Y_train = Y_train.flatten()
print(Y_train)
Y_test = Y_test.flatten()
print(Y_test)
Y_train = np.where(Y_train > 0, 1, -1)
Y_test = np.where(Y_test > 0, 1, -1)
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
bag_clf = BaggingClassifier(
DecisionTreeClassifier(random_state=42), n_estimators=500,
max_samples=100, bootstrap=True, n_jobs=-1, random_state=42)
bag_clf.fit(X_train_1, Y_train.astype("int"))
y_pred = bag_clf.predict(X_test_1)
from sklearn.metrics import accuracy_score
print(accuracy_score(Y_test.astype("int"), y_pred))
tree_clf = DecisionTreeClassifier(random_state=42)
tree_clf.fit(X_train_1, Y_train.astype("int"))
y_pred_tree = tree_clf.predict(X_test_1)
print(accuracy_score(Y_test.astype("int"), y_pred_tree))
from matplotlib.colors import ListedColormap
def plot_decision_boundary(clf, X_train_1, Y_train, axes=[-3, 3, -1, 1], alpha=0.5, contour=True):
x1s = np.linspace(axes[0], axes[1], 100)
x2s = np.linspace(axes[2], axes[3], 100)
x1, x2 = np.meshgrid(x1s, x2s)
X_new = np.c_[x1.ravel(), x2.ravel()]
y_pred = clf.predict(X_new).reshape(x1.shape)
custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])
plt.contourf(x1, x2, y_pred, alpha=0.3, cmap=custom_cmap)
if contour:
custom_cmap2 = ListedColormap(['#7d7d58','#4c4c7f','#507d50'])
plt.contour(x1, x2, y_pred, cmap=custom_cmap2, alpha=0.8)
plt.plot(X_train_1[:, 0][Y_train == -1], X_train_1[:, 1][Y_train == -1], "yo", alpha = alpha)
plt.plot(X_train_1[:, 0][Y_train == 1], X_train_1[:, 1][Y_train == 1], "bs", alpha = alpha)
plt.axis(axes)
plt.xlabel(r"$x_1$", fontsize=18)
plt.ylabel(r"$x_2$", fontsize=18, rotation=0)
程序到这里都运行正常,结果在下面一步显示报错。请问有前辈可以指点一下吗?
plt.figure(figsize=(11,4))
plt.subplot(121)
plot_decision_boundary(tree_clf, X_train_1, Y_train)
plt.title("Decision Tree", fontsize=14)
plt.subplot(122)
plot_decision_boundary(bag_clf, X_train_1, Y_train)
plt.title("Decision Trees with Bagging", fontsize=14)
save_fig("decision_tree_without_and_with_bagging_plot")
plt.show()