ipython下np.save的默认保存路径获取

背景介绍

     在使用ipython时会使用np.save()函数以保存数据,可以使用>>pwd获得默认保存路径

Code 分享 Notebook 保存成功 Python 3 (ipykernel) import os import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from tensorflow.keras import layers,losses,optimizers, Sequential from tensorflow.keras.layers import InputLayer, Dense, LSTM, Dropout from tensorflow.keras.models import load_model from tensorflow.keras import Model 0秒 + Code + Markdown stock_data = pd.read_csv('IBM_stock_data.csv') 0秒 + Code + Markdown stock_data.head() 0秒 date Open High Low Close Volume Price Change % 0 1999/11/1 98.50 98.81 96.37 96.75 9551800 0.000000 1 1999/11/2 96.75 96.81 93.69 94.81 11105400 -2.005168 2 1999/11/3 95.87 95.94 93.50 94.37 10369100 -0.464086 3 1999/11/4 94.44 94.44 90.00 91.56 16697600 -2.977641 4 1999/11/5 92.75 92.94 90.19 90.25 13737600 -1.430756 + Code + Markdown scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(stock_data['Close'].values.reshape(-1, 1)) 0秒 + Code + Markdown scaled_data[0] 0秒 array([0.23131139]) + Code + Markdown len(scaled_data) 0秒 6293 + Code + Markdown training_data_len = int(np.ceil(len(scaled_data) * 0.8)) #向上取整 0秒 + Code + Markdown train_data = scaled_data[0:training_data_len] X_train, y_train = [], [] time_step = 10 # 时间窗口,模型基于前10个时间步长进行预测。可以尝试不同长度(如20、30)并观察效果变化。 0秒 + Code + Markdown for i in range(len(train_data) - time_step - 1): X_train.append(train_data[i:(i + time_step), 0]) y_train.append(train_data[i + time_step, 0]) 0秒 + Code + Markdown X_train, y_train = np.array(X_train), np.array(y_train) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1) 0秒 + Code + Markdown X_train.shape 0秒 (5024, 10, 1) + Code + Markdown X_test, y_test = [], [] test_data = scaled_data[training_data_len - time_step:] ​ for i in range(len(test_data) - time_step): X_test.append(test_data[i:(i + time_step), 0]) y_test.append(test_data[i + time_step, 0]) ​ X_test, y_test = np.array(X_test), np.array(y_test) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1) 0秒 + Code + Markdown model = Sequential() model.add(InputLayer(input_shape=(X_train.shape[1], 1))) model.add(LSTM(units=64, return_sequences=True)) # 调整Dropout: 当前设置为0.3,可以尝试在不同的层上使用不同的Dropout值,例如0.2~0.5之间。Dropout的作用是防止过拟合。 model.add(Dropout(0.3)) model.add(LSTM(units=64, return_sequences=True)) model.add(Dropout(0.3)) model.add(LSTM(units=32)) model.add(Dropout(0.2)) # 增加回归层: 如果希望更高的拟合精度,可以添加多个Dense层,例如在输出前再增加一层Dense。 model.add(Dense(units=1)) 3秒 2025-06-21 16:21:14.205566: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory 2025-06-21 16:21:14.205665: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303) 2025-06-21 16:21:14.205686: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (dsw-1161427-779497c87d-j6j54): /proc/driver/nvidia/version does not exist 2025-06-21 16:21:14.206190: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. + Code + Markdown def MyRNN(): model = Sequential([ layers.InputLayer(input_shape=(X_train.shape[1],1)), layers.SimpleRNN(units=64, dropout=0.5, return_sequences=True, unroll=True), layers.SimpleRNN(units=64, dropout=0.5, unroll=True), layers.Dense(1)] ) return model 0秒 + Code + Markdown model = MyRNN() model.compile(optimizer='adam', loss='mean_squared_error') 0秒 + Code + Markdown history = model.fit( X_train, y_train, epochs=2, # 批大小(batch_size): 尝试不同的batch_size,例如16、32、64,以找到训练稳定性和准确性之间的平衡 batch_size=32, #callbacks=[early_stopping, lr_scheduler] ) 12秒 Epoch 1/2 WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7f02c8692040> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Constant' To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING: AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7f02c8692040> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Constant' To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert 157/157 [==============================] - 9s 20ms/step - loss: 0.0396 Epoch 2/2 157/157 [==============================] - 3s 21ms/step - loss: 0.0266 + Code + Markdown model.save('my_model.keras') 0秒 + Code + Markdown train_loss = model.evaluate(X_train, y_train, verbose=0) test_loss = model.evaluate(X_test, y_test, verbose=0) 5秒 WARNING:tensorflow:AutoGraph could not transform <function Model.make_test_function.<locals>.test_function at 0x7f02cc2b5dc0> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Constant' To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING: AutoGraph could not transform <function Model.make_test_function.<locals>.test_function at 0x7f02cc2b5dc0> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Constant' To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert + Code + Markdown # 训练集上计算的损失值,数值越小表示模型在训练数据上的拟合效果越好 print(f"Training Loss: {train_loss:.4f}") # 测试集上计算的损失值,反映了模型在未见过的数据上的表现。测试损失略高于训练损失,但差距不大,说明模型在新数据上的表现依然良好。 print(f"Testing Loss: {test_loss:.4f}") 0秒 Training Loss: 0.0412 Testing Loss: 0.0480 + Code + Markdown model = load_model('my_model.keras') predictions = model.predict(X_test) predictions = scaler.inverse_transform(predictions) # 反归一化预测值 2秒 WARNING:tensorflow:AutoGraph could not transform <function Model.make_predict_function.<locals>.predict_function at 0x7f0276f14040> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Constant' To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING: AutoGraph could not transform <function Model.make_predict_function.<locals>.predict_function at 0x7f0276f14040> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module 'gast' has no attribute 'Constant' To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert + Code + Markdown import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体 plt.rcParams['axes.unicode_minus'] = False # 正确显示负号 0秒 + Code + Markdown %matplotlib inline 0秒 + Code + Markdown Code train = stock_data[:training_data_len] valid = stock_data[training_data_len:] valid.loc[:, 'Predictions'] = predictions ​ ​ # 绘制图像 plt.figure(figsize=(14, 5)) plt.title('股票价格预测', fontsize=20) # 图表标题改为中文 plt.xlabel('日期', fontsize=14) # X 轴标签改为中文 plt.ylabel('收盘价', fontsize=14) # Y 轴标签改为中文 plt.plot(train['date'], train['Close'], label='训练数据', color='blue') # 训练数据标签改为中文 plt.plot(valid['date'], valid['Close'], label='真实价格', color='green') # 真实价格标签改为中文 plt.plot(valid['date'], valid['Predictions'], label='预测价格', color='red') # 预测价格标签改为中文 plt.legend() # 添加图例 # 添加保存图像的代码 plt.savefig('stock_price_predictions.png') # 保存图像 plt.show() ​ # 计算和输出评估指标 rmse = np.sqrt(np.mean(np.square(predictions - y_test))) mae = np.mean(np.abs(predictions - y_test)) print(f'均方根误差 (RMSE): {rmse}, 平均绝对误差 (MAE): {mae}') # 输出信息改为中文 4分钟58秒 /opt/conda/lib/python3.8/site-packages/pandas/core/indexing.py:1667: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[key] = value findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 32929 (\N{CJK UNIFIED IDEOGRAPH-80A1}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 31080 (\N{CJK UNIFIED IDEOGRAPH-7968}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 20215 (\N{CJK UNIFIED IDEOGRAPH-4EF7}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 26684 (\N{CJK UNIFIED IDEOGRAPH-683C}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 39044 (\N{CJK UNIFIED IDEOGRAPH-9884}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 26085 (\N{CJK UNIFIED IDEOGRAPH-65E5}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 26399 (\N{CJK UNIFIED IDEOGRAPH-671F}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 25910 (\N{CJK UNIFIED IDEOGRAPH-6536}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 30424 (\N{CJK UNIFIED IDEOGRAPH-76D8}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 25454 (\N{CJK UNIFIED IDEOGRAPH-636E}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 30495 (\N{CJK UNIFIED IDEOGRAPH-771F}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /tmp/ipykernel_18835/4137420046.py:16: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. plt.savefig('stock_price_predictions.png') # 保存图像 /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 26085 (\N{CJK UNIFIED IDEOGRAPH-65E5}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 26399 (\N{CJK UNIFIED IDEOGRAPH-671F}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 25910 (\N{CJK UNIFIED IDEOGRAPH-6536}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 30424 (\N{CJK UNIFIED IDEOGRAPH-76D8}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 20215 (\N{CJK UNIFIED IDEOGRAPH-4EF7}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 32929 (\N{CJK UNIFIED IDEOGRAPH-80A1}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 31080 (\N{CJK UNIFIED IDEOGRAPH-7968}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 26684 (\N{CJK UNIFIED IDEOGRAPH-683C}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 39044 (\N{CJK UNIFIED IDEOGRAPH-9884}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 25454 (\N{CJK UNIFIED IDEOGRAPH-636E}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 30495 (\N{CJK UNIFIED IDEOGRAPH-771F}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /opt/conda/lib/python3.8/site-packages/IPython/core/pylabtools.py:152: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) 均方根误差 (RMSE): 105.03158114646604, 平均绝对误差 (MAE): 104.21013102460681误差好大,还有中文显示不成功
最新发布
06-22
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值