A cikin wannan labarin, za mu bincika batun sabunta ƙimar tsararrun NumPy da samar da cikakkiyar mafita ga wannan matsalar. NumPy ɗakin karatu ne na Python wanda ake amfani da shi sosai don sarrafa tsararru da ayyukan lissafi. Yana da inganci sosai kuma yana ba da ayyuka iri-iri. Fahimtar tsarin sabunta tsarin NumPy yana da mahimmanci ga kowane mai haɓakawa da ke aiki tare da bayanan lambobi a Python.
Magani ga Matsala: Ana ɗaukaka Ƙimar NumPy Array
Hanya mafi sauƙi don sabunta ƙimar tsararrun NumPy ita ce yin amfani da ainihin ƙididdiga da dabarun aiki. Wannan yana ba masu haɓaka damar samun dama ga takamaiman abubuwa, layuka, ko ginshiƙan tsararru kuma su gyara ƙimar su bisa ga dabarar da ake buƙata.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) arr[0, 0] = 10 # Update the value at (0, 0) index arr[2] = [7, 88, 9] # Update the entire row 2 with new values print(arr)
Wannan lambar za ta fitar da tsararrun da aka sabunta masu zuwa:
““
[[10 2]
[4]
[ shafi na 7]
““
Bayanin mataki-mataki na Code
1. Shigo da NumPy: Mataki na farko shine shigo da ɗakin karatu na NumPy azaman np. Wannan yana ba mu damar amfani da ayyukansa da azuzuwan sa cikin kodin.
import numpy as np
2. Ƙirƙiri Array: Na gaba, muna ƙirƙirar samfurin 3 × 3 NumPy ta amfani da aikin `np.array ()`. Wannan shine tsararrun da za mu gyara a cikin matakai masu zuwa.
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
3. Sabunta Ƙimar Array: Muna sabunta ƙima a fihirisa (0, 0) na tsararrun mu ta amfani da maƙasudin ƙima da ɗawainiya. A wannan yanayin, muna canza darajar daga 1 zuwa 10.
arr[0, 0] = 10
Hakanan zamu iya sabunta jeri duka ta hanyar sanya sabon jerin ƙima zuwa wannan layin. Anan, muna sabunta layi na uku (jeri na 2) tare da sabbin ƙima.
arr[2] = [7, 88, 9]
4. Nuna Sabunta Tsari: A ƙarshe, muna buga tsararrun da aka sabunta don ganin canje-canjen da aka yi amfani da su.
print(arr)
Yanzu kuna da cikakkiyar fahimtar yadda ake sabunta ƙimar tsararrun NumPy ta amfani da dabarun ƙididdigewa da ɗawainiya.
Ayyuka da hanyoyin NumPy da ake yawan amfani da su akai-akai
Lokacin aiki tare da tsararrun NumPy, ana amfani da ayyuka da dama da yawa don sarrafa tsararru da ayyukan lissafi. Waɗannan sun haɗa da:
- np.zeros(): Ƙirƙiri sabon tsararru mai cike da sifili.
- np.su(): Ƙirƙiri sabon tsararru cike da waɗanda.
- np.reshape(): Canja siffar tsararru ba tare da canza bayanan sa ba.
- np.concatenate(): Haɗa tsararraki biyu ko fiye tare da ƙayyadadden axis.
- np.dot(): Yi lissafin samfurin ɗigo na tsararraki biyu.
- np.sum(): Lissafin jimillar abubuwan tsararru tare da axis da aka bayar.
Fahimtar Lissafin Array a cikin NumPy
Fihirisar tsararru a cikin NumPy siffa ce mai ƙarfi wacce ke ba masu haɓaka damar samun dama da canza takamaiman abubuwa ko sassan tsararru a hankali. Wadannan su ne wasu fasahohin tantancewa na gama gari:
- Ƙididdigar asali: Samun damar abubuwa ta amfani da fihirisar jere da shafi, misali, `arr[0, 0]`.
- Yin kwalliya: Samun damar abubuwa masu jere a cikin jeri tare da axis, misali, `arr[0:2, :]`.
- Boolean Indexing: Samun damar abubuwa dangane da yanayin boolean, misali, `arr[arr> 2]`.
- Fihirisar Zato: Samun damar abubuwa ta amfani da tsararrun fihirisa, misali, `arr[[0, 1], [1, 2]]`.
Fahimta da ƙware waɗannan dabarun firikwensin na iya haɓaka haɓakar ku sosai yayin aiki tare da tsararrun NumPy.