吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 542|回复: 8
收起左侧

[求助] PY中如何没能输出呢?

[复制链接]
jtwc 发表于 2023-9-12 17:32
各位老师,PY中定义了输出,如何没能输出呢?谢谢了
[Python] 纯文本查看 复制代码
import pandas as pd
import matplotlib.pyplot as plt
from keras import layers
import tensorflow as tf
from keras.models import Model
from keras.layers import Dense, Dropout, LSTM, Input, Activation, concatenate
import numpy as np
from keras import optimizers
from sklearn import preprocessing


np.random.seed(4)
tf.random.set_seed(4)
history_points = 50

def csv_to_dataset(csv_path):
    data = pd.read_csv(csv_path)
    data = data.drop('date', axis=1)
    data = data.drop(0, axis=0)
    data_normaliser = preprocessing.MinMaxScaler()
    data_normalised = data_normaliser.fit_transform(data)

    # using the last {history_points} open high low close volume data points, predict the next open value
    ohlcv_histories_normalised = np.array(
        [data_normalised[i: i + history_points].copy() for i in range(len(data_normalised) - history_points)])
    next_day_open_values_normalised = np.array(
        [data_normalised[:, 0][i + history_points].copy() for i in range(len(data_normalised) - history_points)])
    next_day_open_values_normalised = np.expand_dims(next_day_open_values_normalised, -1)
    next_day_open_values = np.array([data[:, 0][i + history_points].copy() for i in range(len(data) - history_points)])
    next_day_open_values = np.expand_dims(next_day_open_values_normalised, -1)
    y_normaliser = preprocessing.MinMaxScaler()
    y_normaliser.fit(np.expand_dims(next_day_open_values))

    assert ohlcv_histories_normalised.shape[0] == next_day_open_values_normalised.shape[0]
    return ohlcv_histories_normalised, next_day_open_values_normalised, next_day_open_values, y_normaliser

    ohlcv_histories, next_day_open_values, unscaled_y, y_normaliser = csv_to_dataset('daily.csv')
    test_split = 0.9  # the percent of data to be used for testing
    n = int(ohlcv_histories.shape[0] * test_split)
    # splitting the dataset up into train and test sets
    ohlcv_train = ohlcv_histories[:n]
    y_train = next_day_open_values[:n]
    ohlcv_test = ohlcv_histories[n:]
    y_test = next_day_open_values[n:]
    unscaled_y_test = unscaled_y[n:]

    lstm_input = Input(shape=(history_points, 5), name='lstm_input')
    x = LSTM(50, name='lstm_0')(lstm_input)
    x = Dropout(0.2, name='lstm_dropout_0')(x)
    x = Dense(64, name='dense_0')(x)
    x = Activation('sigmoid', name='sigmoid_0')(x)
    x = Dense(1, name='dense_1')(x)
    output = Activation('linear', name='linear_output')(x)
    model = Model(inputs=lstm_input, outputs=output)
    adam = optimizers.Adam(lr=0.0005)
    model.compile(optimizer=adam, loss='mse')
    from keras.utils import plot_model
    # plot_model(model, to_file='model.png')
    model.fit(x=ohlcv_train, y=y_train, batch_size=32, epochs=50, shuffle=True, validation_split=0.1)
    evaluation = model.evaluate(ohlcv_test, y_test)
    print(evaluation)

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

马了顶大 发表于 2023-9-12 17:40
你在函数内定义了输出,但是没调用函数
知心 发表于 2023-9-12 17:40
函数需要调用才会执行,你只是定义了没有调用
 楼主| jtwc 发表于 2023-9-12 17:46
 楼主| jtwc 发表于 2023-9-12 17:48
知心 发表于 2023-9-12 17:40
函数需要调用才会执行,你只是定义了没有调用

老师,该如何改呢?
马了顶大 发表于 2023-9-12 17:54
jtwc 发表于 2023-9-12 17:46
老师,如何调用呢?

在最下面顶格写 csv_to_dataset(这里填csv文件路径)
 楼主| jtwc 发表于 2023-9-12 18:04
马了顶大 发表于 2023-9-12 17:54
在最下面顶格写 csv_to_dataset(这里填csv文件路径)

谢谢老师,改后又出错:Traceback (most recent call last):
  File "E:/副本 (2).py", line 65, in <module>
    csv_to_dataset(r"D:\\daily.csv")
  File "E:/副本 (2).py", line 32, in csv_to_dataset
    y_normaliser.fit(np.expand_dims(next_day_open_values))
  File "<__array_function__ internals>", line 4, in expand_dims
TypeError: _expand_dims_dispatcher() missing 1 required positional argument: 'axis'
张益达_zyl 发表于 2023-9-12 21:37
看样子你的代码应该如下:
import pandas as pd
import matplotlib.pyplot as plt
from keras import layers
import tensorflow as tf
from keras.models import Model
from keras.layers import Dense, Dropout, LSTM, Input, Activation, concatenate
import numpy as np
from keras import optimizers
from sklearn import preprocessing

np.random.seed(4)
tf.random.set_seed(4)
history_points = 50

def csv_to_dataset(csv_path):
    data = pd.read_csv(csv_path)
    data = data.drop("date", axis=1)
    data = data.drop(0, axis=0)
    data_normaliser = preprocessing.MinMaxScaler()
    data_normalised = data_normaliser.fit_transform(data)

    # using the last {history_points} open high low close volume data points, predict the next open value
    ohlcv_histories_normalised = np.array(
        [
            data_normalised[i : i + history_points].copy()
            for i in range(len(data_normalised) - history_points)
        ]
    )
    next_day_open_values_normalised = np.array(
        [
            data_normalised[:, 0][i + history_points].copy()
            for i in range(len(data_normalised) - history_points)
        ]
    )
    next_day_open_values_normalised = np.expand_dims(
        next_day_open_values_normalised, -1
    )
    next_day_open_values = np.array(
        [
            data[:, 0][i + history_points].copy()
            for i in range(len(data) - history_points)
        ]
    )
    next_day_open_values = np.expand_dims(next_day_open_values_normalised, -1)
    y_normaliser = preprocessing.MinMaxScaler()
    y_normaliser.fit(np.expand_dims(next_day_open_values))

    assert (
        ohlcv_histories_normalised.shape[0] == next_day_open_values_normalised.shape[0]
    )
    return (
        ohlcv_histories_normalised,
        next_day_open_values_normalised,
        next_day_open_values,
        y_normaliser,
    )

if __name__ == "__main__":
    ohlcv_histories, next_day_open_values, unscaled_y, y_normaliser = csv_to_dataset(
        'daily.csv'
    )
    test_split = 0.9  # the percent of data to be used for testing
    n = int(ohlcv_histories.shape[0] * test_split)
    # splitting the dataset up into train and test sets
    ohlcv_train = ohlcv_histories[:n]
    y_train = next_day_open_values[:n]
    ohlcv_test = ohlcv_histories[n:]
    y_test = next_day_open_values[n:]
    unscaled_y_test = unscaled_y[n:]

    lstm_input = Input(shape=(history_points, 5), name="lstm_input")
    x = LSTM(50, name="lstm_0")(lstm_input)
    x = Dropout(0.2, name="lstm_dropout_0")(x)
    x = Dense(64, name="dense_0")(x)
    x = Activation("sigmoid", name="sigmoid_0")(x)
    x = Dense(1, name="dense_1")(x)
    output = Activation("linear", name="linear_output")(x)
    model = Model(inputs=lstm_input, outputs=output)
    adam = optimizers.Adam(lr=0.0005)
    model.compile(optimizer=adam, loss="mse")
    from keras.utils import plot_model

    # plot_model(model, to_file='model.png')
    model.fit(
        x=ohlcv_train,
        y=y_train,
        batch_size=32,
        epochs=50,
        shuffle=True,
        validation_split=0.1,
    )
    evaluation = model.evaluate(ohlcv_test, y_test)
    print(evaluation)
 楼主| jtwc 发表于 2023-9-12 22:59
张益达_zyl 发表于 2023-9-12 21:37
看样子你的代码应该如下:
[md]```Python
import pandas as pd

谢谢老师,
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-28 12:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表