Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS C:\Users\a-xiaobodou> python -m pip install --upgrade pip
C:\msys64\mingw64\bin\python.exe: No module named pip
PS C:\Users\a-xiaobodou> python -m pip install numpy
C:\msys64\mingw64\bin\python.exe: No module named pip
PS C:\Users\a-xiaobodou> python3 -m pip install numpy
C:\msys64\mingw64\bin\python3.exe: No module named pip
PS C:\Users\a-xiaobodou> pip install numpy
Requirement already satisfied: numpy in c:\users\a-xiaobodou\appdata\local\programs\python\python310\lib\site-packages (1.22.3)
PS C:\Users\a-xiaobodou> python -i
Python 3.9.11 (main, Mar 18 2022, 16:54:01) [GCC 11.2.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>> eye(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'eye' is not defined
>>>
不明白,为什么在命令提示符出现ModuleNotFoundError:No mudule namy 'numpy'
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>>
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> my_array=np.array([1,2,3,4,5])
>>> print my_array
File "<stdin>", line 1
print my_array
^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
>>> print my_array
File "<stdin>", line 1
print my_array
^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
>>> print(my_array)
[1 2 3 4 5]
>>> print(my_array.shape)
(5,)
>>> print(my_array[0])
1
>>> my_array[0]=-1
>>> print(my_array[0])
-1
>>> print(my_array)
[-1 2 3 4 5]
>>> my_new_array=np.zeros((5))
>>> print(my_random_array)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'my_random_array' is not defined. Did you mean: 'my_new_array'?
>>> print(my_new_array)
[0. 0. 0. 0. 0.]
>>> my_random_array=np.radom.radom((5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\a-xiaobodou\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\__init__.py", line 315, in __getattr__
raise AttributeError("module {!r} has no attribute "
AttributeError: module 'numpy' has no attribute 'radom'. Did you mean: 'random'?
>>> my_random_array=np.random.radom((5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'numpy.random' has no attribute 'radom'. Did you mean: 'random'?
>>> my_random_array=np.random.random((5))
>>> print(my_random_array)
[0.25351566 0.7168677 0.13383434 0.56201388 0.80009886]
>>> my_2d_array=np.zeros((2,3)) print(my_2d_array)
File "<stdin>", line 1
my_2d_array=np.zeros((2,3)) print(my_2d_array)
^^^^^
SyntaxError: invalid syntax
>>> my_2d_array=np.zeros((2,3))
>>> print(my_ad_array)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'my_ad_array' is not defined. Did you mean: 'my_2d_array'?
>>> print(my_2d_array)
[[0. 0. 0.]
[0. 0. 0.]]
>>> my_ad_array_new=np.ones((2,4))
>>> print(my_2d_array_new)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'my_2d_array_new' is not defined. Did you mean: 'my_ad_array_new'?
>>> print(my_ad_array_new)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
>>> my_array=np.array([4,5],[6,1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Field elements must be 2- or 3-tuples, got '6'
>>> my_array=np.array([[4,5],[6,1]])
>>> print(my_array[0][1])
5
>>> print(my_array)
[[4 5]
[6 1]]
>>> print(my_array.shape)
(2, 2)
>>> my_array_column_2=my_array[:,1]
>>> print(my_array_colum_2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'my_array_colum_2' is not defined. Did you mean: 'my_array_column_2'?
>>> print(my_array_column_2)
[5 1]
>>>
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a=np.array([[1.0,2.0],[3.0,4.0]])
>>> b=np.array([[5.0,6.0],[7.0,8.0]])
>>> sum=a+b
>>> difference=a-b
>>> product=a*b
>>> quotient=a/b
>>> print('Sum=\n',sum)
Sum=
[[ 6. 8.]
[10. 12.]]
>>> print('Difference =\n',difference)
Difference =
[[-4. -4.]
[-4. -4.]]
>>> print('Product =\n',product)
Product =
[[ 5. 12.]
[21. 32.]]
>>> print('Quotient =\n', quotient)
Quotient =
[[0.2 0.33333333]
[0.42857143 0.5 ]]
>>>
>>> print(a)
[[1. 2.]
[3. 4.]]
>>> print(b)
[[5. 6.]
[7. 8.]]
>>>
>>> matrix_product=a.dot(b)
>>> print("Matrix Product = ",matrix_product)
Matrix Product = [[19. 22.]
[43. 50.]]
>>>
属性 | 说明 |
ndarray.ndim | 秩,即轴的数量 或维度的数量 |
ndarray.shape | 数组的维度,对于矩阵,为n行m列 |
ndarray.size | 数组元素的总个数,相当于.shape中n×m的值 |
ndarray.dtype | ndarray对象的元素类型 |
ndarray.itemsize | ndarray对象中每个元素的大小,以字节为单位 |
ndarray.flags | ndarray对象的内存信息 |
ndarray.real | ndarray元素的实部 |
ndarray.imag | ndarray元素的虚部 |
ndarray.data | 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性 |