Python | numpy.fill_diagonal () methode

Met behulp van de numpy.fill_diagonal() methode kun je de diagonalen van een numpy array vullen met de waarde die als parameter wordt doorgegeven aan numpy.fill_diagonal ( ) .

Syntaxis: numpy.fill_diagonal (array, waarde)
Return: Retourneer de gevulde waarde in de diagonaal van een array.

Voorbeeld # 1:
In dit voorbeeld kunnen we zien dat we met numpy.fill_diagonal ( ) kunnen krijgen de diagonalen gevuld met de waarden die als parameter zijn doorgegeven.

# NumPy import

import numpy als np


# met de numpy.fill_diagonal () methode

array = np.array ([[ 1 , 2 ], [ 2 , 1 ]])

np .fill_diagonal (array, 5 )

print (array)

Uitvoer:

[[5 2]
[2 5]]

Voorbeeld #2:

< /p>

Uitvoer:

[ [1 0 0]
[0 1 0]
[0 0 1]]

We hopen dat dit artikel u heeft geholpen om het probleem op te lossen. Bekijk naast Python | numpy.fill_diagonal () methode ook andere Python functions-gerelateerde onderwerpen.

Wilt u uitblinken in Python? Bekijk onze recensie van de  beste online Python-cursussen 2023. Als je geïnteresseerd bent in Data Science, bekijk dan ook hoe je programmeert in R.

Trouwens, dit materiaal is ook beschikbaar in andere talen:



Carlo Krasiko

San Francisco | 2023-03-25

Simpel gezegd en duidelijk. Bedankt voor het delen. Python | numpy.fill_diagonal () methode en andere zaken met Python functions was altijd mijn zwakke punt 😁. Zal het gebruiken in mijn bachelorscriptie

Boris Williams

Prague | 2023-03-25

Bedankt voor de uitleg! Ik zat vast met Python | numpy.fill_diagonal () methode voor enkele uren, eindelijk heb ik het voor elkaar 🤗. Ik hoop alleen dat dat niet meer naar voren komt

Dmitry Richtgofen

Milan | 2023-03-25

Ik was me aan het voorbereiden op mijn coderingsinterview, bedankt voor de verduidelijking - Python | numpy.fill_diagonal () methode in Python is niet de eenvoudigste. Zal het gebruiken in mijn bachelorscriptie

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# NumPy import

import numpy als np

< br /> # met de numpy.fill_diagonal () methode

array = np.zeros (( 3 , 3 ), int )

np.fill_diagonal (array, 1 )

print (array)