An warware: concat tare da sifili tsararrun adadi

A cikin duniyar shirye-shirye da nazarin bayanai, sarrafa tsararraki masu yawa da matrices sun zama mahimmanci don ingantaccen aiki. ฦŠayan ษ—akin karatu da ya yi fice a cikin Python don aiki tare da waษ—annan tsarin bayanai shine Lambobi. NumPy yana haษ—a wani abu mai ฦ™arfi N-dimensional array tare da ayyuka iri-iri da kayan aiki don aiki akan bayanan. A yau, za mu tattauna batun akai-akai wanda masu haษ“akawa da manazarta ke ci karo da su: haษ—a tsararru mai girman sifili ta amfani da NumPy.

Kafin nutsewa cikin mafita, bari mu tattauna ainihin abin da ke nufin haษ—a ma'aunin sifili. A cikin NumPy, wani lokaci mukan yi mu'amala da tsararraki waษ—anda ba su da abubuwa sifili, wanda kuma ake magana da shi azaman fanko ko girman sifili. Manufar mu a nan ita ce mu gano yadda za mu haษ—a waษ—annan jeriyoyin masu girman sifili da sauran tsararru.

The Magani

Don magance matsalar, muna buฦ™atar bincika ko shirye-shiryen da muke haษ—awa ba komai bane ko a'a. Idan tsararru babu komai, kawai mu tsallake tare da haษ—a shi. Za mu yi amfani da Python's if sanarwa tare da numpy.size() aiki don cimma wannan.

Bari mu ga yadda wannan ke aiki a mataki-mataki tsari.

Bayanin Code-by-Taki-Taki

Da farko, bari mu shigo da ษ—akin karatu da ake buฦ™ata:

import numpy as np

Yanzu, za mu ฦ™irฦ™iri tsararraki biyu don dalilai na nunawa. Bari array_a zama tsararru mai girman sifili, kuma array_b ya zama tsararru mai abubuwa:

array_a = np.array([])
array_b = np.array([1, 2, 3, 4, 5])

Na gaba, za mu haษ“aka aikinmu don haษ—a tsararraki, muna la'akari da yanayi na musamman na tsararrun masu girman sifili:

def concatenate_arrays(array1, array2):
    if not np.size(array1):
        return array2
    elif not np.size(array2):
        return array1
    else:
        return np.concatenate((array1, array2))

A cikin aikin da ke sama, da farko za mu bincika ko ษ—ayan tsararrun shigarwar ba su da abubuwa marasa sifili (ba komai). Idan array1 babu komai, aikin zai dawo array2, kuma akasin haka. Idan babu komai a cikin tsararru, yana ci gaba da haษ—a su ta amfani da numpy.concatenate() aiki.

Yanzu, bari mu gwada aikin mu concatenate_arrays:

result_array = concatenate_arrays(array_a, array_b)
print(result_array)

Wannan zai fitar da:

[1., 2., 3., 4., 5.]

Kamar yadda kuke gani, aikinmu ya sami nasarar haษ—a jeri mai girman sifili tare da ษ—ayan, yana maido da abubuwan da ba su da sifili kawai.

Laburaren NumPy

Lambobi, wanda ke tsaye ga Python lambobi, ษ—akin karatu ne mai ฦ™arfi wanda ke ba masu amfani damar yin aiki yadda ya kamata tare da tsarin bayanai kamar su arrays, matrices, da ฦ™ari. Girman shahararsa a cikin al'ummar kimiyyar bayanai shaida ce ga iyawar sa, yana baiwa masu haษ“aka damar aiwatar da ayyukan lissafi cikin sauri akan manyan bayanan bayanai. NumPy yana ba da tushe ga sauran mahimman ษ—akunan karatu kamar pandas, TensorFlow, da scikit-learn.

Yin mu'amala da Tsarukan Maษ—aukaki Mai Girma

Ikon NumPy ya ta'allaka ne akan ikon sa na aiki tare da tsararraki masu girma dabam ba tare da wahala ba. A cikin lissafin kimiyya, sau da yawa muna yin hulษ—a da manyan jigogi na n-girma, masu wakiltar sigogi daban-daban don nuna hadaddun bayanai. NumPy tsararru adana bayanai iri ษ—aya da ayyukan tallafi kamar haษ“aka-hikima da haษ“akawa, samfuran dige-dige, da watsa shirye-shirye, duk yayin da suke ba da kyakkyawan aiki. Wannan yana sa yin aiki tare da waษ—annan tsararru mai inganci kuma mai sauฦ™i, rage duk wani shingen hanya da za su iya fuskanta yayin aiwatarwa.

A ฦ™arshe, maษ“alli don haษ—a jeri mai girman sifili ta amfani da NumPy yana ta'allaka ne wajen sarrafa tsararrun fanko da kyau. Ta hanyar magance wannan batu, aikin mu na ฦ™arshe yana goyan bayan haษ—a nau'o'in nau'i-nau'i da yawa da sifili a cikin tsari mara kyau. Tare da ฦ™arfin ฦ™arfinsa don sarrafa bayanai, NumPy ya kafa kanta a matsayin kayan aiki mai mahimmanci don nazarin bayanai, koyon injin, sarrafa hoto, da ฦ™ari.

Shafi posts:

Leave a Comment