简体   繁体   English

“ ValueError:x和y必须具有相同的第一维”的不同情况

[英]A different situation of “ValueError: x and y must have same first dimension”

I am wondering why the same code runs differently in Spyder and Pycharm? 我想知道为什么相同的代码在Spyder和Pycharm中的运行方式不同? It shows graph in Spyder, but it doesn't in Pycharm. 它在Spyder中显示图,但在Pycharm中不显示。 Although there is the same error, the results are different. 尽管存在相同的错误,但结果却不同。 I don't know how to deal with it, I look for answers in stackoverflow, the answers all says the lengths of x and y are different, but I don't know how to correct in my example. 我不知道如何处理,我在stackoverflow中寻找答案,所有答案都说x和y的长度不同,但是我不知道如何在示例中更正。

import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt

np.random.seed(1)

N = 30

mean = (1, 2)
cov = [[1.8, 0], [0, 1.8]]
x1 = np.random.multivariate_normal(mean, cov, N)
mean = (2, 1)
x2 = np.random.multivariate_normal(mean, cov, N)

x12 = np.concatenate((x1, x2), axis=0)

mean = (1, -1)
x3 = np.random.multivariate_normal(mean, cov, N)
mean = (1.5, -1.5)
x4 = np.random.multivariate_normal(mean, cov, N)

x34 = np.concatenate((x3, x4), axis=0)

plt.plot(x12[:, 0], x12[:, 1], 'o', color='b')
plt.plot(x34[:, 0], x34[:, 1], 'o', color='r')

X = np.concatenate((x12, x34), axis=0)
X_1 = np.concatenate((np.ones((120, 1)), X), axis=1)

y = np.ones(2*N)
y = np.concatenate((y, 0*y), axis=0)

beta = np.dot(inv(np.dot(X_1.transpose(), X_1)),
              np.dot(X_1.transpose(), y))

intercept = beta[0]  # beta_0
coef = beta[:1]  # beta_1 and beta_2

# Calculate RSS
rss = np.dot(y - np.dot(X_1, beta), y - np.dot(X_1, beta))

print("\nThe estimated model parameters are")
print(intercept)
print(coef)
print(rss)

p1x = -3  # Left x limit
p2x = 6  # Right x Limit
p1y = (0.5 - beta[0] - beta[1]*p1x)/beta[2]  # Y value at p1x (left)
p2y = (0.5 - beta[0] - beta[1]*p2x)/beta[2]  # Y value at p2x (right)
Pts = np.array([[p1x, p1y], [p2x, p2y]])
# Now draw the boundary
plt.plot(Pts[:, 0], Pts[:1], '_', color='black')

Thank u guys for your attention. 谢谢你们的关注。 The following answer did solve the error I posted, but I still can't get any graph in Pycharm. 以下答案确实解决了我发布的错误,但是我仍然无法在Pycharm中获得任何图形。 I have already tried add "plt.show()" at the end, but it doesn't work. 我已经尝试在末尾添加“ plt.show()”,但是它不起作用。 The following result is all I get in console: 以下是我在控制台中得到的所有结果:

The estimated model parameters are
0.394336889526
[ 0.39433689]
15.2577282371

Pts is an array with shape (2,2). Pts是一个形状为(2,2)的数组。 From this array you select the first column by Pts[:, 0] and the first row by Pts[:1] . 从该数组中,通过Pts[:, 0]选择第一列,并通过Pts[:1]第一行。 However the shapes are different. 但是形状不同。 While Pts[:, 0] is a 1D array, Pts[:1] is a 2D array (although only one dimension is filled. Pts[:, 0]是一维数组,而Pts[:1]是2D数组(尽管仅填充了一个维。

Since it's not clear from the question what the purpose of this is, I have to guess that in reality you want to plot the second column of the array agains the first, which would be done using Pts[:,1] 由于尚不清楚该问题的目的是什么,因此我不得不猜测,实际上您想绘制数组的第二列再次绘制第一列,这将使用Pts[:,1]

plt.plot(Pts[:, 0], Pts[:,1], '_', color='black')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM