我对keras和python都是新手。我有一个具有不同序列长度的时间序列数据集(例如,第一个序列是484000x128,第二个序列是563110x128,依此类推)我已将这些序列放入3D数组中。
我的问题是如何定义输入形状,因为我很困惑。我使用的是DL4J,但是概念在定义网络配置方面有所不同。
这是我的第一个试用代码:
import numpy as np
from keras.models import Sequential
from keras.layers import Embedding,LSTM,Dense,Dropout
## Loading dummy data
sequences = np.array([[[1,2,3],[1,2,3]], [[4,5,6],[4,5,6],[4,5,6]]])
y = np.array([[[0],[0]], [[1],[1],[1]]])
x_test=np.array([[2,3,2],[4,6,7],[1,2,1]])
y_test=np.array([0,1,1])
n_epochs=40
# model configration
model = Sequential()
model.add(LSTM(100, input_shape=(3,1), activation='tanh', recurrent_activation='hard_sigmoid')) # 100 num of LSTM units
model.add(LSTM(100, activation='tanh', recurrent_activation='hard_sigmoid'))
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print(model.summary())
## training with batches of size 1 (each batch is a sequence)
for epoch in range(n_epochs):
for seq, label in zip(sequences, y):
model.train(np.array([seq]), [label]) # …Run Code Online (Sandbox Code Playgroud) 如何使用 Python 以更易读的树格式打印下面的字典数据,如下所示。这是我第一次来这里,请原谅我的无知。
TempDict =
{'outlook': {'sunny': {'humidity': {'high': 'no', 'normal': 'yes'}}, 'rainy': {'wind': {'strong': 'no', 'weak': 'yes'}}, 'overcast': 'yes'}}
Run Code Online (Sandbox Code Playgroud)
所需的输出格式 -
outlook=sunny
humidity=high:no
humidity=normal:yes
outlook=rainy
wind=strong:no
wind=weak:yes
Run Code Online (Sandbox Code Playgroud)
像这样打印的类:
class DecisionNode:
def __init__(self, attribute):
self.attribute = attribute
self.children = {}
# Visualizes the tree
def display(self, level = 0):
if self.children == {}:
# reached leaf level
print(": ", self.attribute, end="")
else:
for value in self.children.keys():
prefix = "\n" + " " * level * 4
print(prefix, self.attribute, "=", value, …Run Code Online (Sandbox Code Playgroud) 我有正确的代码:
with open("text.txt") as openfile:
for line in openfile:
for part in line.split():
if "Hello" in part:
print(part)
Run Code Online (Sandbox Code Playgroud)
只能在文本中找到像 hello 这样的特定单词并打印它。问题是,我希望它停止仅打印该单词并打印包含该单词的整行。我怎样才能做到这一点?
这是我现在得到的结果:
hello
hello
hello
Run Code Online (Sandbox Code Playgroud)
然而,文本文件包括:
hello, i am a code
hello, i am a coder
hello, i am a virus
Run Code Online (Sandbox Code Playgroud) 我正在编写这个函数,首先我使用的是循环。循环需要时间,因为我尝试了列表理解。它没有用。此功能允许 10 秒。请检查一下。该函数返回公司的排序列表,其中公司的比率为 5% 或更高。
def mostActive(customers):
# Write your code here
tot = len(customers)
set_cust = set(customers)
cust_dict = {i: customers.count(i)/tot for i in set_cust}
cus_list = [i for i in list(cust_dict) if cust_dict[i] >= 0.05]
return sorted(cus_list)
Run Code Online (Sandbox Code Playgroud)
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Alpha
Omega
Beta
Run Code Online (Sandbox Code Playgroud)
预期输出:
Alpha
Beta
Omega
Run Code Online (Sandbox Code Playgroud)