这个问题似乎以前曾被问过,但我似乎无法评论对已接受答案的进一步澄清,我无法弄清楚所提供的解决方案.
我正在尝试学习如何使用sklearn和我自己的数据.在过去的100年里,我基本上只得到了2个不同国家的GDP年度变化百分比.我现在只想学习使用单个变量.我基本上要做的是使用sklearn来预测A国的GDP%变化将给出B国GDP的百分比变化.
问题是我收到一条错误说:
ValueError:找到样本数不一致的数组:[1 107]
这是我的代码:
import sklearn.linear_model as lm
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def bytespdate2num(fmt, encoding='utf-8'):#function to convert bytes to string for the dates.
strconverter = mdates.strpdate2num(fmt)
def bytesconverter(b):
s = b.decode(encoding)
return strconverter(s)
return bytesconverter
dataCSV = open('combined_data.csv')
comb_data = []
for line in dataCSV:
comb_data.append(line)
date, chngdpchange, ausgdpchange = np.loadtxt(comb_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%d/%m/%Y')})
chntrain = chngdpchange[:-1]
chntest = chngdpchange[-1:]
austrain = ausgdpchange[:-1]
austest = …Run Code Online (Sandbox Code Playgroud) 我有一列pandas形状的数据帧(362L,),并希望将其更改为(362, 103).我怎样才能做到这一点?
目标是对具有多个输入的 Keras 模型执行交叉验证。这对于只有一个输入的正常顺序模型来说效果很好。然而,当使用函数式 api 并扩展到两个输入时,sklearnscross_val_score似乎没有按预期工作。
def create_model():
input_text = Input(shape=(1,), dtype=tf.string)
embedding = Lambda(UniversalEmbedding, output_shape=(512, ))(input_text)
dense = Dense(256, activation='relu')(embedding)
input_title = Input(shape=(1,), dtype=tf.string)
embedding_title = Lambda(UniversalEmbedding, output_shape=(512, ))(input_title)
dense_title = Dense(256, activation='relu')(embedding_title)
out = Concatenate()([dense, dense_title])
pred = Dense(2, activation='softmax')(out)
model = Model(inputs=[input_text, input_title], outputs=pred)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
Run Code Online (Sandbox Code Playgroud)
keras_classifier = KerasClassifier(build_fn=create_model, epochs=10, batch_size=10, verbose=1)
cv = StratifiedKFold(n_splits=10, random_state=0)
results = cross_val_score(keras_classifier, [X1, X2], y, cv=cv, scoring='f1_weighted')
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "func.py", …Run Code Online (Sandbox Code Playgroud)