write a numpy program to convert a list of numeric value into a one-dimensional numpy array then create a 3x3 matrix
时间: 2023-12-03 21:43:09 浏览: 150
Sure, here's the code:
```python
import numpy as np
# create a list of numeric values
values = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# convert the list to a one-dimensional numpy array
arr = np.array(values)
# create a 3x3 matrix from the array
matrix = arr.reshape(3, 3)
print(matrix)
```
Output:
```
[[1 2 3]
[4 5 6]
[7 8 9]]
```
Explanation:
First, we import the `numpy` library. Then, we create a list of numeric values called `values`.
Next, we convert the `values` list to a one-dimensional numpy array using the `np.array()` function and assign it to the variable `arr`.
Finally, we create a 3x3 matrix from the `arr` array using the `reshape()` method and assign it to the variable `matrix`. We then print the `matrix` variable to see the output.
阅读全文