程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python basics 2022 latest] Lesson 6 numpy

編輯:Python

【Python Basics 2022 newest 】 Lesson 6 Numpy

  • summary
  • Numpy
    • Numpy install
    • Anaconda
    • Guide pack
  • ndarray
    • np.array establish
    • np.zeros establish
    • np.ones establish
  • Common functions
    • reshape
    • flatten

summary

Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .

Numpy

Numpy yes Python A very important library , It provides us with a large number of data processing functions .

Numpy install

Installation command :

pip install numpy
pip3 install numpy

Anaconda

Anaconda It is a library of Computing Science , It can provide convenience for us Python Environmental Science .

install :
Anaconda Official website

Guide pack

Import Numpy package :

# Guide pack
import numpy as np
![ Insert picture description here ](https://img-blog.csdnimg.cn/002699042b894844a1349b86b29bdc6a.gif)

ndarray

ndarray yes Numpy The most important feature . ndarray It's a N Dimensional array object .

np.array establish

np.array It can help us create a ndarray.

Format :

numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)

Parameters :

  • object: An array of class
  • dtype: data type , Optional

Example :

# Guide pack
import numpy as np
# establish ndarray
array1 = np.array([1, 2, 3]) # adopt lsit establish
array2 = np.array([1, 2, 3], dtype=float)
# Debug output
print(array1, type(array1))
print(array2, type(array2))

Output results :

# Guide pack
import numpy as np
# establish ndarray
array1 = np.array([1, 2, 3]) # adopt lsit establish
array2 = np.array([1, 2, 3], dtype=float)
# Debug output
print(array1, type(array1))
print(array2, type(array2))

np.zeros establish

np.zeros It can help us create a full 0 Array .

Format :

numpy.zeros(shape, dtype=float, order='C', *, like=None)

Parameters :

  • shape: Array shape
  • detype: The default is float, floating-point

Example :

import numpy as np
# Create whole 0 Of ndarray
array = np.zeros((3, 3), dtype=int)
print(array)

Output results :

[[0 0 0]
[0 0 0]
[0 0 0]]

np.ones establish

np.zeros It can help us create a full 1 Array .

Format :

numpy.ones(shape, dtype=float, order='C', *, like=None)

Parameters :

  • shape: Array shape
  • detype: The default is float, floating-point

Example :

import numpy as np
# Create whole 1 Of ndarray
array = np.ones((3, 3), dtype=int)
print(array)
print(type(array))

Output results :

[[1 1 1]
[1 1 1]
[1 1 1]]
<class 'numpy.ndarray'>

Common functions

reshape

adopt reshape() We can change the shape of the array .

Format :

numpy.reshape(arr, newshape, order='C')

Parameters :

  • arr: Need to change the shape of the array
  • newshape: New shape

Example :

import numpy as np
# establish ndarray
array = np.zeros(9)
print(array)
# reshape
array = array.reshape((3,3))
print(array)
print(array.shape) # Debug output array shape

Output results :

[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
(3, 3)

flatten

adopt flatten() We can flatten the multidimensional array into 1 Dimension group .

Example :

import numpy as np
# Create multidimensional arrays
array = np.zeros((3, 3))
print(array)
# flatten Convert to one-dimensional array
array = array.flatten()
print(array)

Output results :

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved