다중 선형 회귀를 실행하려고하는데 결과를 그리는 데 문제가 있습니다. 3D 플롯을 플롯하려고합니다.이 출력을 얻습니다.ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (4,) and requested shape (34,)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train,y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X.iloc[:, 0], X.iloc[:, 1], Y)
ax.plot(X.iloc[:, 0], X.iloc[:, 1], y_pred, color='red')
ax.set_xlabel('Annual Income (k$)')
ax.set_ylabel('Age')
ax.set_zlabel('Spending Score')
plt.show()
플롯 명령은 다음과 같아야합니다.
ax.plot(X_test.iloc[:, 0], X_test.iloc[:, 1], y_pred, color='red')
전체 입력이 아닌 y_pred
하위 집합에 대한 y 값만 포함 하기 때문 입니다.X_test
X
연결된 선 ( ax.plot
)으로 플로팅하는 것은 의미가 없습니다. 입력 데이터가 의미있는 방식으로 정렬되지 않았고 입력 데이터가 정렬 된 경우에도 테스트 세트가 확실히 정렬되지 않았기 때문입니다.
다음과 같이 플롯합니다.
from sklearn.model_selection import train_test_split
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# generate some data as an example.
np.random.seed(1)
n = 20
X = pd.DataFrame(np.random.uniform(size=(n, 2)), columns=['foo', 'bar'])
Y = X['foo'] + 2*X['bar'] + np.random.normal(scale=0.2, size=n)
X_train, X_test, y_train,y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X['foo'], X['bar'], Y, label='data')
for x0, x1, yt, yp in zip(X_test['foo'], X_test['bar'], y_test, y_pred):
ax.plot([x0, x0], [x1, x1], [yt, yp], color='red')
ax.scatter(X_test['foo'], X_test['bar'], y_pred, color='red', marker='s', label='prediction')
ax.set_xlabel('X0')
ax.set_ylabel('X1')
ax.set_zlabel('y')
ax.legend()
fig.show()
시각화를 수행하는 다른 방법이 있습니다. 를 사용 np.meshgrid
하여 X
그리드에 값 을 생성 하고 y
예측 변수에서 값을 가져 와서 이를 사용 plot_wireframe
하고 와이어 프레임으로부터의 수직 거리를 나타내는 수직선을 사용하여 훈련 및 테스트 데이터를 모두 플로팅 할 수 있습니다. 데이터에 따라 의미가 다릅니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다