arr0 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]) arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]) arr3 = np.array(input("请输入连续24个月的配件销售数据,元素之间用空格隔开:").split(), dtype=float) data_array = np.vstack((arr1, arr3)) data_matrix = data_array.T data = pd.DataFrame(data_matrix, columns=['month', 'sales']) sales = data['sales'].values.astype(np.float32) sales_mean = sales.mean() sales_std = sales.std() sales = abs(sales - sales_mean) / sales_std train_data = sales[:-1] test_data = sales[-12:] def create_model(): model = tf.keras.Sequential() model.add(layers.Input(shape=(11, 1))) model.add(layers.Conv1D(filters=32, kernel_size=2, padding='causal', activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Conv1D(filters=64, kernel_size=2, padding='causal', activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Conv1D(filters=128, kernel_size=2, padding='causal', activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Conv1D(filters=256, kernel_size=2, padding='causal', activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Conv1D(filters=512, kernel_size=2, padding='causal', activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(1, activation='linear')) return model model = create_model() BATCH_SIZE = 16 BUFFER_SIZE = 100 train_dataset = tf.data.Dataset.from_tensor_slices(train_data) train_dataset = train_dataset.window(11, shift=1, drop_remainder=True) train_dataset = train_dataset.flat_map(lambda window: window.batch(11)) train_dataset = train_dataset.map(lambda window: (window[:-1], window[-1:])) train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).prefetch(1) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mse') history = model.fit(train_dataset, epochs=100, verbose=0) test_input = test_data[:-1] test_input = np.reshape(test_input, (1, 11, 1)) predicted_sales = model.predict(test_input)[0][0] * sales_std + sales_mean test_prediction = model.predict(test_input) y_test=test_data[1:12] y_pred=test_prediction y_pred = test_prediction.ravel() print("预测下一个月的销量为:", predicted_sales),如何将以下代码稍作修改插入到上面的最后,def comput_acc(real,predict,level): num_error=0 for i in range(len(real)): if abs(real[i]-predict[i])/real[i]>level: num_error+=1 return 1-num_error/len(real) a=np.array(test_data[label]) real_y=a real_predict=test_predict print("置信水平:{},预测准确率:{}".format(0.2,round(comput_acc(real_y,real_predict,0.2)* 100,2)),"%")
时间: 2023-10-09 20:10:08 浏览: 141
js代码-已知如下数组: var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]; 编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组
你可以在最后添加如下代码实现 `comput_acc()` 函数的功能:
```
label = 0 # 定义标签
a = np.array(test_data[label]) # 获取测试数据的标签列
real_y = a[1:] # 实际销售数据
real_predict = y_pred[:-1] # 预测销售数据
print("置信水平:{},预测准确率:{}".format(0.2, round(comput_acc(real_y, real_predict, 0.2) * 100, 2)), "%")
```
这段代码的作用是,首先定义标签为 0,即选取测试数据中的第一列数据(即销售数据)。然后,使用 `np.array()` 函数将该列数据转换为 numpy 数组 `a`。接着,将 `a` 中的第二个元素到最后一个元素赋值给 `real_y`,这里是因为真实销售数据的第一个元素已经作为测试数据输入了模型,所以预测值中不包含该元素。然后,将 `y_pred` 中的第一个元素到倒数第二个元素赋值给 `real_predict`,这是因为预测值中的最后一个元素已经与真实值的最后一个元素相对应。最后,调用 `comput_acc()` 函数计算预测准确率,并将结果打印输出。
阅读全文