x = data[:, 0].reshape(-1, 1)
时间: 2024-01-19 10:02:14 浏览: 40
这段代码是将一个二维数组 `data` 的第一列提取出来,并将其转换为一个列向量,即 shape 为 `(n, 1)` 的数组。其中,`data[:, 0]` 表示取 `data` 数组的所有行的第一列,`reshape(-1, 1)` 表示将这个一维数组转换为列向量,其中 `-1` 表示自动计算其它维度的大小以保证总元素数不变。
相关问题
程序执行提示AttributeError: 'point_cloud_generator' object has no attribute 'widthself',优化程序class point_cloud_generator(): def __init__(self, rgb_file, depth_file, save_ply, camera_intrinsics=[784.0, 779.0, 649.0, 405.0]): self.rgb_file = rgb_file self.depth_file = depth_file self.save_ply = save_ply self.rgb = cv2.imread(rgb_file) self.depth = cv2.imread(self.depth_file, -1) print("your depth image shape is:", self.depth.shape) self.width = self.rgb.shape[1] self.height = self.rgb.shape[0] self.camera_intrinsics = camera_intrinsics self.depth_scale = 1000 def compute(self): t1 = time.time() depth = np.asarray(self.depth, dtype=np.uint16).T # depth[depth==65535]=0 self.Z = depth / self.depth_scale fx, fy, cx, cy = self.camera_intrinsics X = np.zeros((self.width, self.height)) Y = np.zeros((self.width, self.height)) for i in range(self.width): X[i, :] = np.full(X.shape[1], i) self.X = ((X - cx / 2) * self.Z) / fx for i in range(self.height): Y[:, i] = np.full(Y.shape[0], i) self.Y = ((Y - cy / 2) * self.Z) / fy data_ply = np.zeros((6, self.width * self.height)) data_ply[0] = self.X.T.reshape(-1)[:self.widthself.height] data_ply[1] = -self.Y.T.reshape(-1)[:self.widthself.height] data_ply[2] = -self.Z.T.reshape(-1)[:self.widthself.height] img = np.array(self.rgb, dtype=np.uint8) data_ply[3] = img[:, :, 0:1].reshape(-1)[:self.widthself.height] data_ply[4] = img[:, :, 1:2].reshape(-1)[:self.widthself.height] data_ply[5] = img[:, :, 2:3].reshape(-1)[:self.widthself.height] self.data_ply = data_ply t2 = time.time() print('calcualte 3d point cloud Done.', t2 - t1)
在代码中有两处出现了 `self.widthself.height`,这应该是想要写成 `self.width * self.height`。你需要修改这两处,如下所示:
```
data_ply[0] = self.X.T.reshape(-1)[:self.width * self.height]
data_ply[1] = -self.Y.T.reshape(-1)[:self.width * self.height]
data_ply[2] = -self.Z.T.reshape(-1)[:self.width * self.height]
img = np.array(self.rgb, dtype=np.uint8)
data_ply[3] = img[:, :, 0:1].reshape(-1)[:self.width * self.height]
data_ply[4] = img[:, :, 1:2].reshape(-1)[:self.width * self.height]
data_ply[5] = img[:, :, 2:3].reshape(-1)[:self.width * self.height]
```
这样就可以避免出现 `AttributeError: 'point_cloud_generator' object has no attribute 'widthself'` 的错误了。
新数据前面多了一列无用的,每列用逗号隔开,改代码data = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-0-ratio.txt') y = data.iloc[:, :-1].values.reshape(-1, 1) X = data.iloc[:, -1].values.reshape(-1, 1) regressor = LinearRegression() regressor.fit(X, y) y_pred = regressor.predict(X) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0])) plt.scatter(X, y, color='blue') plt.plot(X, y_pred, color='red') data2 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-5-ratio.txt') y2 = data2.iloc[:, :-1].values.reshape(-1, 1) X2 = data2.iloc[:, -1].values.reshape(-1, 1) regressor2 = LinearRegression() regressor2.fit(X2, y2) y2_pred = regressor2.predict(X2) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0])) plt.scatter(X2, y2, color='green') plt.plot(X2, y2_pred, color='orange') plt.legend(['Regression Line 2', 'Observations 2']) #3 data3 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-10-ratio.txt') y3 = data3.iloc[:, :-1].values.reshape(-1, 1) X3 = data3.iloc[:, -1].values.reshape(-1, 1) regressor3 = LinearRegression() regressor3.fit(X3, y3) y3_pred = regressor3.predict(X3) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0])) plt.scatter(X3, y3, color='purple') plt.plot(X3, y3_pred, color='yellow') plt.title('Linear Regression') plt.xlabel('Independent Variable') plt.ylabel('Dependent Variable') plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3']) plt.show()
你想问如何处理新数据前面多出来的一列无用的数据,对吗?可以使用`usecols`参数来指定读取哪些列,例如`usecols=[1]`来只读取第二列数据。修改后的代码如下:
```
data = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-0-ratio.txt', usecols=[1, 2])
y = data.iloc[:, :-1].values.reshape(-1, 1)
X = data.iloc[:, -1].values.reshape(-1, 1)
regressor = LinearRegression()
regressor.fit(X, y)
y_pred = regressor.predict(X)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0]))
plt.scatter(X, y, color='blue')
plt.plot(X, y_pred, color='red')
data2 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-5-ratio.txt', usecols=[1, 2])
y2 = data2.iloc[:, :-1].values.reshape(-1, 1)
X2 = data2.iloc[:, -1].values.reshape(-1, 1)
regressor2 = LinearRegression()
regressor2.fit(X2, y2)
y2_pred = regressor2.predict(X2)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0]))
plt.scatter(X2, y2, color='green')
plt.plot(X2, y2_pred, color='orange')
plt.legend(['Regression Line 2', 'Observations 2'])
data3 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-10-ratio.txt', usecols=[1, 2])
y3 = data3.iloc[:, :-1].values.reshape(-1, 1)
X3 = data3.iloc[:, -1].values.reshape(-1, 1)
regressor3 = LinearRegression()
regressor3.fit(X3, y3)
y3_pred = regressor3.predict(X3)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0]))
plt.scatter(X3, y3, color='purple')
plt.plot(X3, y3_pred, color='yellow')
plt.title('Linear Regression')
plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')
plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3'])
plt.show()
```