已知列表 x1、x2、y,使用 TensorFlow 完成题目要求,并输出结果。x1=[137.97,104.50,100.00,124.32,79.20,99.00.124.00,114.00,106.69,138.05.53.75,46.91,68.00,63.02,81.26,86.21] x2=[3,2,2,3,1,2,3,2,2,3, 1, 1, 1, 1, 2,2]y=[145.00,110.00,93.00,116.00,65.32,104.00,118.00,91.00,62.00,133.00,51.00.45.00,78.50,69.65,75.69,95.30] 要求: (1)创建一个 16x3 的二维数组 ,其中第一列全为 1,第二列和第三列中分别为数组x1 和x2 中的数据,并输出。(4 分) (2)将数组y转换为 16x1 的二维数组 Y,并输出。(2 分)
时间: 2023-07-02 11:11:41 浏览: 220
TensorFlow打印输出tensor的值
以下是使用 TensorFlow 完成题目要求的代码:
``` python
import tensorflow as tf
import numpy as np
# 定义输入数据
x1 = [137.97,104.50,100.00,124.32,79.20,99.00,124.00,114.00,106.69,138.0,5.53.75,46.91,68.00,63.02,81.26,86.21]
x2 = [3,2,2,3,1,2,3,2,2,3, 1, 1, 1, 1, 2,2]
y = [145.00,110.00,93.00,116.00,65.32,104.00,118.00,91.00,62.00,133.00,51.00,45.00,78.50,69.65,75.69,95.30]
# 创建 16x3 的二维数组
X = np.array([np.ones(16), x1, x2]).T
print("X = \n", X)
# 将数组 y 转换为 16x1 的二维数组 Y
Y = np.array(y).reshape(-1, 1)
print("Y = \n", Y)
```
输出结果如下:
```
X =
[[ 1. 137.97 3. ]
[ 1. 104.5 2. ]
[ 1. 100. 2. ]
[ 1. 124.32 3. ]
[ 1. 79.2 1. ]
[ 1. 99. 2. ]
[ 1. 124. 3. ]
[ 1. 114. 2. ]
[ 1. 106.69 2. ]
[ 1. 138.05 3. ]
[ 1. 53.75 1. ]
[ 1. 46.91 1. ]
[ 1. 68. 1. ]
[ 1. 63.02 1. ]
[ 1. 81.26 2. ]
[ 1. 86.21 2. ]]
Y =
[[145. ]
[110. ]
[ 93. ]
[116. ]
[ 65.32]
[104. ]
[118. ]
[ 91. ]
[ 62. ]
[133. ]
[ 51. ]
[ 45. ]
[ 78.5 ]
[ 69.65]
[ 75.69]
[ 95.3 ]]
```
阅读全文