NOTE_numpy np.ravel()

这篇博客详细介绍了NumPy库中数组操作的方法,包括ravel()函数如何将多维数组展开为一维列向量,以及c_和r_两个操作符如何分别按行和列组合数组元素。通过实例展示了这些方法的具体应用,帮助读者深入理解NumPy中的数组操作技巧。

NOTE_numpy np.ravel()

ravel将数组展开(deploy)后生成的是列向量
c_表示(expression)按行(row) combine
r_表示(expression)按列(column) combine

import numpy as np
a = np.array([[1, 2, 3], [3, 4, 9]])
print(a.ravel())
b = np.r_[a.ravel(), np.array([1, 2, 3, 4, 5, 6])]
print(b)
c = np.c_[a.ravel(), np.array([1, 2, 3, 4, 5, 6])]
print(c)
[1 2 3 3 4 9]
[1 2 3 3 4 9 1 2 3 4 5 6]
[[1 1]
 [2 2]
 [3 3]
 [3 4]
 [4 5]
 [9 6]]
(base) rock@rock-Precision-5530:~/PyGRDECL$ python convert_grdecl_to_vtk.py [Input] Reading ECLIPSE/PETREL file "./ExampleData/dome.grdecl" .... Grid Type=CornerPoint Grid Dimension(NX,NY,NZ): (20 x 20 x 4) NumOfGrids=1600 NumOfKeywords=11 Reading Keywords [SPECGRID] [PORO] [PERMX] [PERMY] [PERMZ] .....Done! [Geometry] Converting GRDECL to Paraview Hexahedron mesh data.... NumOfPoints 12800 NumOfCells 1600 Traceback (most recent call last): File "/home/rock/PyGRDECL/convert_grdecl_to_vtk.py", line 7, in <module> Model.GRDECL2VTK() File "/home/rock/PyGRDECL/GRDECL2VTK.py", line 161, in GRDECL2VTK self.Update() File "/home/rock/PyGRDECL/GRDECL2VTK.py", line 168, in Update self.AppendScalarData2VTK(keyword,data) #VTK will automatically overwrite the data with the same keyword ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/PyGRDECL/GRDECL2VTK.py", line 381, in AppendScalarData2VTK data = ns.numpy_to_vtk(numpy_array.ravel(order='F'),deep=True, array_type=vtk.VTK_FLOAT) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/vtkmodules/util/numpy_support.py", line 164, in numpy_to_vtk arr_dtype = get_numpy_array_type(vtk_typecode) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/vtkmodules/util/numpy_support.py", line 94, in get_numpy_array_type return get_vtk_to_numpy_typemap()[vtk_array_type] ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/vtkmodules/util/numpy_support.py", line 74, in get_vtk_to_numpy_typemap _vtk_np = {vtkConstants.VTK_BIT:numpy.bool, ^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/numpy/__init__.py", line 324, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'bool'. `np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'bool_'?
03-27
import pandas as pd import numpy as np import os import tushare as ts from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from xgboost import XGBRegressor from keras.models import Sequential from keras.layers import Dense, LSTM from sklearn.metrics import mean_squared_error, r2_score import matplotlib.pyplot as plt # 设置 Tushare Token 并初始化 API ts.set_token('7b77182b414a6b5a5893aa670f659492f14cecce0d2b7cb80dd160de') # 替换为你的 Tushare Token pro = ts.pro_api() # 下载数据并保存到本地文件 file_path = 'pingan_stock_data.csv' if not os.path.exists(file_path): data = pro.daily(ts_code='000001.SZ', start_date='20220101', end_date='20241130') data.to_csv(file_path, index=False) print(f"文件已下载并保存为 {file_path}。") # 读取数据 data = pd.read_csv(file_path) data.rename(columns={'close': 'Close', 'open': 'Open', 'high': 'High', 'low': 'Low', 'vol': 'Volume'}, inplace=True) # 标准化列名[^1] # 计算技术指标作为特征 data['MA_5'] = data['Close'].rolling(window=5).mean() data['MA_10'] = data['Close'].rolling(window=10).mean() data['RSI'] = data['Close'].rolling(window=14).apply(lambda x: RSI(x)[-1], raw=False) # 自定义 RSI 函数[^2] # 添加价格变化和波动率 data['Price_Change'] = data['Close'].pct_change() data['Volatility'] = data['Close'].rolling(window=10).std() # 删除缺失值 data.dropna(inplace=True) # 筛选特征 features = ['MA_5', 'MA_10', 'RSI', 'Price_Change', 'Volatility'] X = data[features].values y = data['Close'].values # 归一化处理 scaler_X = MinMaxScaler() scaler_y = MinMaxScaler() X_scaled = scaler_X.fit_transform(X) y_scaled = scaler_y.fit_transform(y.reshape(-1, 1)) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.2, random_state=42) # 训练 XGBoost 模型 xgb_model = XGBRegressor(objective='reg:squarederror', n_estimators=100, learning_rate=0.1, max_depth=5) xgb_model.fit(X_train, y_train.ravel()) # 预测与反归一化 xgb_predictions_scaled = xgb_model.predict(X_test) xgb_predictions = scaler_y.inverse_transform(xgb_predictions_scaled.reshape(-1, 1)).flatten() # 评估性能 xgb_mse = mean_squared_error(y_test_actual, xgb_predictions) xgb_r2 = r2_score(y_test_actual, xgb_predictions) print(f"XGBoost MSE: {xgb_mse:.4f}, XGBoost R²: {xgb_r2:.4f}") # 准备 LSTM 输入数据 def create_dataset(X, y, time_steps=1): Xs, ys = [], [] for i in range(len(X) - time_steps): v = X[i:(i + time_steps)] Xs.append(v) ys.append(y[i + time_steps]) return np.array(Xs), np.array(ys) time_steps = 10 X_lstm, y_lstm = create_dataset(X_scaled, y_scaled, time_steps) X_train_lstm, X_test_lstm, y_train_lstm, y_test_lstm = train_test_split(X_lstm, y_lstm, test_size=0.2, random_state=42) # 构建 LSTM 模型 model = Sequential() model.add(LSTM(50, return_sequences=True, input_shape=(X_train_lstm.shape[1], X_train_lstm.shape[2]))) model.add(LSTM(50, return_sequences=False)) model.add(Dense(25)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mean_squared_error') # 训练模型 model.fit(X_train_lstm, y_train_lstm, batch_size=64, epochs=10) # 预测与反归一化 lstm_predictions_scaled = model.predict(X_test_lstm) lstm_predictions = scaler_y.inverse_transform(lstm_predictions_scaled) # 评估性能 lstm_mse = mean_squared_error(y_test_actual, lstm_predictions) lstm_r2 = r2_score(y_test_actual, lstm_predictions) print(f"LSTM MSE: {lstm_mse:.4f}, LSTM R²: {lstm_r2:.4f}") # 绘制预测结果 plt.figure(figsize=(10, 5)) plt.plot(y_test_actual, label='Actual Price') plt.plot(xgb_predictions, label='XGBoost Predictions') plt.plot(lstm_predictions, label='LSTM Predictions') plt.title('Model Performance Comparison') plt.xlabel('Time Step') plt.ylabel('Closing Price') plt.legend() plt.savefig('model_performance.png') plt.close() - ImportError Traceback (most recent call last) File C:\ProgramData\anaconda3\Lib\site-packages\numpy\_core\__init__.py:23 22 try: ---> 23 from . import multiarray 24 except ImportError as exc: File C:\ProgramData\anaconda3\Lib\site-packages\numpy\_core\multiarray.py:10 9 import functools ---> 10 from . import overrides 11 from . import _multiarray_umath File C:\ProgramData\anaconda3\Lib\site-packages\numpy\_core\overrides.py:8 7 from .._utils._inspect import getargspec ----> 8 from numpy._core._multiarray_umath import ( 9 add_docstring, _get_implementing_args, _ArrayFunctionDispatcher) 12 ARRAY_FUNCTIONS = set() ImportError: DLL load failed while importing _multiarray_umath: 找不到指定的模块。 During handling of the above exception, another exception occurred: ImportError Traceback (most recent call last) Cell In[4], line 5 3 import os 4 import tushare as ts ----> 5 from sklearn.model_selection import train_test_split 6 from sklearn.preprocessing import MinMaxScaler 7 from xgboost import XGBRegressor File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\__init__.py:83 69 # We are not importing the rest of scikit-learn during the build 70 # process, as it may not be compiled yet 71 else: (...) 77 # later is linked to the OpenMP runtime to make it possible to introspect 78 # it and importing it first would fail if the OpenMP dll cannot be found. 79 from . import ( 80 __check_build, # noqa: F401 81 _distributor_init, # noqa: F401 82 ) ---> 83 from .base import clone 84 from .utils._show_versions import show_versions 86 __all__ = [ 87 "calibration", 88 "cluster", (...) 129 "show_versions", 130 ] File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\base.py:19 17 from ._config import config_context, get_config 18 from .exceptions import InconsistentVersionWarning ---> 19 from .utils import _IS_32BIT 20 from .utils._estimator_html_repr import estimator_html_repr 21 from .utils._metadata_requests import _MetadataRequester File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\__init__.py:22 20 from ._bunch import Bunch 21 from ._estimator_html_repr import estimator_html_repr ---> 22 from ._param_validation import Interval, validate_params 23 from .class_weight import compute_class_weight, compute_sample_weight 24 from .deprecation import deprecated File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\_param_validation.py:15 12 from scipy.sparse import csr_matrix, issparse 14 from .._config import config_context, get_config ---> 15 from .validation import _is_arraylike_not_scalar 18 class InvalidParameterError(ValueError, TypeError): 19 """Custom exception to be raised when the parameter of a class/method/function 20 does not have a valid type or value. 21 """ File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\validation.py:28 26 from .. import get_config as _get_config 27 from ..exceptions import DataConversionWarning, NotFittedError, PositiveSpectrumWarning ---> 28 from ..utils._array_api import _asarray_with_order, _is_numpy_namespace, get_namespace 29 from ._isfinite import FiniteStatus, cy_isfinite 30 from .fixes import _object_dtype_isnan File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\_array_api.py:9 6 import scipy.special as special 8 from .._config import get_config ----> 9 from .fixes import parse_version 12 def _check_array_api_dispatch(array_api_dispatch): 13 """Check that array_api_compat is installed and NumPy version is compatible. 14 15 array_api_compat follows NEP29, which has a higher minimum NumPy version than 16 scikit-learn. 17 """ File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\fixes.py:18 16 import numpy as np 17 import scipy ---> 18 import scipy.stats 19 import threadpoolctl 21 import sklearn File C:\ProgramData\anaconda3\Lib\site-packages\scipy\stats\__init__.py:608 1 """ 2 .. _statsrefmanual: 3 (...) 603 604 """ 606 from ._warnings_errors import (ConstantInputWarning, NearConstantInputWarning, 607 DegenerateDataWarning, FitError) --> 608 from ._stats_py import * 609 from ._variation import variation 610 from .distributions import * File C:\ProgramData\anaconda3\Lib\site-packages\scipy\stats\_stats_py.py:37 35 from numpy import array, asarray, ma 36 from numpy.lib import NumpyVersion ---> 37 from numpy.testing import suppress_warnings 39 from scipy.spatial.distance import cdist 40 from scipy.ndimage import _measurements File C:\ProgramData\anaconda3\Lib\site-packages\numpy\testing\__init__.py:11 8 from unittest import TestCase 10 from . import _private ---> 11 from ._private.utils import * 12 from ._private.utils import (_assert_valid_refcount, _gen_alignment_data) 13 from ._private import extbuild File C:\ProgramData\anaconda3\Lib\site-packages\numpy\testing\_private\utils.py:23 20 import concurrent.futures 22 import numpy as np ---> 23 from numpy._core import ( 24 intp, float32, empty, arange, array_repr, ndarray, isnat, array) 25 from numpy import isfinite, isnan, isinf 26 import numpy.linalg._umath_linalg File C:\ProgramData\anaconda3\Lib\site-packages\numpy\_core\__init__.py:49 25 import sys 26 msg = """ 27 28 IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! (...) 47 """ % (sys.version_info[0], sys.version_info[1], sys.executable, 48 __version__, exc) ---> 49 raise ImportError(msg) 50 finally: 51 for envkey in env_added: ImportError: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html Please note and check the following: * The Python version is: Python3.11 from "C:\ProgramData\anaconda3\python.exe" * The NumPy version is: "1.24.3" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: DLL load failed while importing _multiarray_umath: 找不到指定的模块。 ​
06-21
import numpy as np from numpy.linalg import lstsq def compute_dTdt(T_nodes: np.ndarray, dt: float) -> np.ndarray: """ Centered finite difference for each column (node). T_nodes: (N, 2) -> columns: [T_s, T_r] returns dTdt shape (N, 2) """ N, n = T_nodes.shape dTdt = np.zeros_like(T_nodes) if N < 2: raise ValueError("Need at least 2 time points") # centered differences for interior if N > 2: dTdt[1:-1] = (T_nodes[2:] - T_nodes[:-2]) / (2.0 * dt) # forward/backward for edges dTdt[0] = (T_nodes[1] - T_nodes[0]) / dt dTdt[-1] = (T_nodes[-1] - T_nodes[-2]) / dt return dTdt def est_CG(T_nodes: np.ndarray, P_inputs: np.ndarray, Tamb: np.ndarray, dt: float): """ Estimate C_s, C_r, g_sr, g_sa, g_ra from time-series data. Inputs: T_nodes : (N,2) -> [T_s, T_r] P_inputs: (N,2) -> [P_s, P_r] (W) Tamb : (N,) -> ambient temperature (°C) dt : scalar time step (s) Returns: C_diag : array([C_s, C_r]) G_mat : 2x2 conductance matrix G (watts/K) g_vals : dict with keys 'g_sr','g_sa','g_ra' residuals_norm : norm of LS residual (for diagnostics) """ N, n = T_nodes.shape assert n == 2 assert P_inputs.shape == (N, 2) assert Tamb.shape[0] == N dTdt = compute_dTdt(T_nodes, dt) # (N,2) # Build linear system: for each time step i we have two equations: # Eq_s: C_s * dT_s + g_sr*(T_s - T_r) + g_sa*(T_s - T_amb) = P_s # Eq_r: C_r * dT_r + g_sr*(T_r - T_s) + g_ra*(T_r - T_amb) = P_r # # Unknown vector theta = [C_s, C_r, g_sr, g_sa, g_ra] (5 unknowns) # For each time step we build row_s and row_r (length 5), and stack. rows = [] targets = [] T_s = T_nodes[:, 0] T_r = T_nodes[:, 1] P_s = P_inputs[:, 0] P_r = P_inputs[:, 1] for i in range(N): # equation for stator (row_s) # Coeffs multiply unknowns [C_s, C_r, g_sr, g_sa, g_ra] row_s = np.array([ dTdt[i, 0], # multiplies C_s 0.0, # C_r not present in eq_s (T_s[i] - T_r[i]), # multiplies g_sr (T_s[i] - Tamb[i]), # multiplies g_sa 0.0 # g_ra not present in eq_s ], dtype=float) rows.append(row_s) targets.append(P_s[i]) # equation for rotor (row_r) row_r = np.array([ 0.0, # C_s not present in eq_r dTdt[i, 1], # multiplies C_r (T_r[i] - T_s[i]), # multiplies g_sr (note sign) 0.0, (T_r[i] - Tamb[i]) # multiplies g_ra ], dtype=float) rows.append(row_r) targets.append(P_r[i]) Phi = np.vstack(rows) # (2N, 5) y = np.vstack(targets).ravel() # (2N,) # Solve least squares theta, *_ = lstsq(Phi, y, rcond=None) C_s, C_r, g_sr, g_sa, g_ra = theta # Build matrices C_diag = np.array([C_s, C_r]) # Conductance matrix G (positive diag, negative off-diag) G = np.array([ [g_sa + g_sr, -g_sr], [-g_sr, g_ra + g_sr] ]) # Diagnostics residuals = Phi @ theta - y res_norm = np.linalg.norm(residuals) / np.sqrt(len(y)) g_vals = {"g_sr": g_sr, "g_sa": g_sa, "g_ra": g_ra} return C_diag, G, g_vals, res_norm def calc_T(C_diag, G, P_inputs, Tamb, T0=None, dt=1.0): """ Forward Euler simulation: C dT/dt = -G (T - Tamb) + P => dT/dt = C^{-1} ( -G(T - Tamb) + P ) C_diag: [C_s, C_r] G: 2x2 conductance matrix P_inputs: (N,2) Tamb: (N,) """ N = P_inputs.shape[0] invC = np.diag(1.0 / C_diag) T = np.zeros((N, 2)) if T0 is None: T[0] = Tamb[0] # a reasonable init else: T[0] = T0 for i in range(1, N): rhs = - G @ (T[i-1] - np.array([Tamb[i-1], Tamb[i-1]])) + P_inputs[i-1] dT = invC @ rhs T[i] = T[i-1] + dt * dT return T代码注释
11-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值