np.hstack(),np.vstack()解读

np.hstack()是把矩阵进行行连接
np.vstack()是把矩阵进行列连接

在这里插入图片描述

行连接np.hstack()代码示例①:

import numpy as np
a=np.array([1,2,3])
b=np.array([2,3,4])
aa=[1,2,3]  # 列表也可以作为参数传入
bb=[2,3,4]
print("np.hstack((a,b)):",type(np.hstack((a,b))),"\n",np.hstack((a,b)))
print("np.hstack((a,b)):",type(np.hstack((aa,bb))),"\n",np.hstack((aa,bb)))
c=np.array([[1],
            [2],
            [3]])
d=np.array([[2],
            [3],
            [4]])
print("np.hstack((c,d)):\n",np.hstack((c,d)))
np.hstack((a,b)): <class 'numpy.ndarray'> 
 [1 2 3 2 3 4]
np.hstack((a,b)): <class 'numpy.ndarray'> 
 [1 2 3 2 3 4]
np.hstack((c,d)):
 [[1 2]
 [2 3]
 [3 4]]

行连接np.hstack()代码示例②:

import numpy as np

s=[1,1,1,1]
a=2
r=3
s_=[4,4,4,4]
output = np.hstack((s, [a, r], s_))
print(output)
[1 1 1 1 2 3 4 4 4 4]

同理列连接np.vstack()也类似

在这里插入图片描述

列连接np.vstack()代码示例:以下代码仅仅把hstack改成了vstack。(在pycharm中可用ctrl+r批量替换)

import numpy as np
a=np.array([1,2,3])
b=np.array([2,3,4])
aa=[1,2,3]  # 列表也可以作为参数传入
bb=[2,3,4]
print("np.vstack((a,b)):",type(np.vstack((a,b))),"\n",np.vstack((a,b)))
print("np.vstack((a,b)):",type(np.vstack((aa,bb))),"\n",np.vstack((aa,bb)))
c=np.array([[1],
            [2],
            [3]])
d=np.array([[2],
            [3],
            [4]])
print("np.vstack((c,d)):\n",np.vstack((c,d)))
np.vstack((a,b)): <class 'numpy.ndarray'> 
 [[1 2 3]
 [2 3 4]]
np.vstack((a,b)): <class 'numpy.ndarray'> 
 [[1 2 3]
 [2 3 4]]
np.vstack((c,d)):
 [[1]
 [2]
 [3]
 [2]
 [3]
 [4]]
  • 21
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在您提供的代码中,有一些问题需要修正。首先,您将特征列存储在`features`变量中,但在生成测试数据网格时,您使用了`features['feature1']`等特征名,这可能是导致KeyError的原因。另外,您没有给出训练模型和进行预测的代码。以下是修正后的示例代码: ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import RandomForestRegressor from mpl_toolkits.mplot3d import Axes3D # 读取数据 data = pd.read_excel('your_data.xlsx') # 替换为您的Excel文件路径 # 提取特征和目标变量 features = data[["Bridge length (m)", "Pier type", "Foundation type", "Hole", "Span (m)", "Bearing type", "Plane linear"]] target = data['Vulnerability grade'] # 训练随机森林回归模型 rf = RandomForestRegressor(n_estimators=100, random_state=0) rf.fit(features, target) # 生成测试数据网格 x = np.linspace(features['Bridge length (m)'].min(), features['Bridge length (m)'].max(), 100) y = np.linspace(features['Pier type'].min(), features['Pier type'].max(), 100) z = np.linspace(features['Foundation type'].min(), features['Foundation type'].max(), 100) X_test = np.meshgrid(x, y, z) X_test = np.vstack((X_test[0].flatten(), X_test[1].flatten(), X_test[2].flatten())).T # 使用随机森林模型进行预测 test_predicted = rf.predict(X_test) # 绘制预测结果的空间曲面 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(features['Bridge length (m)'], features['Pier type'], features['Foundation type'], c=target, cmap='coolwarm', label='Training Data') ax.scatter(X_test[:, 0], X_test[:, 1], X_test[:, 2], c=test_predicted, cmap='coolwarm', label='Predicted Data') ax.set_xlabel('Bridge length (m)') ax.set_ylabel('Pier type') ax.set_zlabel('Foundation type') ax.legend() plt.show() ``` 请注意,根据您的数据和需求,您可能需要调整代码中的特征列名、文件路径以及其他绘图参数。确保特征列和目标变量与您的数据一致,并且选择合适的特征列用于绘制空间曲面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值