Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

pandas - How do I print the calculated features in tensorflow (python)

I am a college student who has just started learning Python and doesn't have a lot of coding experience and have been experimenting with TensorFlow out of curiosity. I know that I should become more fluent in learning python before attempting such an ambitious project, but I really want to learn about this experimentally.

So my goal is to take a pre-formatted CSV that has the RSI, RS, MACD, and signal of a stock, and then also if the price of that stock increase the next day (in relation to the prvious day). Wether or not it increased is represented by a 1 or a 0 (1 being an increase, 0 being no change or decrease) so everything is an integer. What I am trying to find is what combination of these indicators leads to the increase. The increase or not is indicated by the class.

So far I have trained the model and tested my test set and gotten it to be 89% accurate, but what I am trying to do is print the combination of values that it found led to the increase. So how might I print the Spy_features that result in Spy_label(class) =1, from the already trained model that has been calculated?

If anymore information is needed, I will gladly provide it, I just feel like I've hit a wall with this aspect of my first project. Most of all I really would like to learn more about Python and machine learning, so a more explanation of how to go about something like this would be greatly appreciated.

import tensorflow as tf

import numpy as np
import pandas as pd
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib



#Data files
SPY_Training = pd.read_csv(r'C:UsersmatthDownloadsSpy_training(noheader).csv',names=["RSI","RS","MACD","signal","Class"])
SPY_Test = pd.read_csv(r'C:UsersmatthDownloadsSpy_test(noheader).csv',names=["RSI","RS","MACD","signal","Class"])

SPY_Test.shape[0], SPY_Training.shape[0]

SPY_Training.head()

SpyTest_features = SPY_Test.copy()
SpyTest_labels = SpyTest_features.pop('Class')

Spy_features = SPY_Training.copy()
Spy_labels = Spy_features.pop('Class')

Spy_features = np.array(Spy_features)
Spy_features

print(Spy_features)

Spy_model = tf.keras.Sequential([
    layers.Dense(64),
    layers.Dense(1)
])

Spy_model.compile(loss = tf.losses.MeanSquaredError(),
                  optimizer= tf.optimizers.Adam(),  metrics=['accuracy'])
Spy_model.fit(Spy_features, Spy_labels, epochs=10)

test_loss, test_acc = Spy_model.evaluate(SpyTest_features, SpyTest_labels, verbose=2)
print('Test accuracy:', test_acc)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In neural networks, every value has a effect on the outcome(either positive or negative).

All your 4 inputs are directly connected to 64 hidden units which are then directly connected to the 1 ouput unit. So one can not say in advance, what value is positively or negatively effecting the output.

You can use matplotlib/seaborn to understand the data by plotting multiple graphs between values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...