A cikin 'yan shekarun nan, amfani da Python a fagage daban-daban ya fadada sosai, musamman a fannin sarrafa bayanai da sarrafa kwamfuta. Ɗaya daga cikin ɗakunan karatu da aka fi amfani da su don waɗannan ayyuka shine NumPy. NumPy babban ɗakin karatu ne mai ƙarfi kuma mai jujjuyawar da aka yi amfani da shi sosai don aiki tare da manyan, tsararraki masu yawa da matrices, a tsakanin sauran ayyukan lissafi. Aiki ɗaya na gama gari a cikin aiki tare da waɗannan tsarin bayanan shine buƙatun rugujewa ko rage girman jigo na ƙarshe. A cikin wannan labarin, za mu bincika wannan batu daki-daki, farawa da gabatarwa ga matsalar, sannan kuma mafita, da bayanin mataki-mataki na lambar. A ƙarshe, za mu shiga cikin wasu batutuwa masu alaƙa da ɗakunan karatu waɗanda za su iya ba da sha'awa.
Bukatar rushe girma na ƙarshe na tsararru na iya tasowa a yanayi daban-daban, kamar lokacin da kuka ƙididdige sakamako daga tsararru mai yawa kuma kuna son samun mafi sauƙi, rage wakilcin bayanan. Wannan aiki da gaske ya ƙunshi canza tsarin tsararru na ainihi zuwa ɗaya mai ƙarancin girma ta hanyar kawar da, ko rugujewa, girman ƙarshe tare da axis.
Magani: Amfani da np.squeeze
Ɗaya daga cikin hanyoyin da za a magance wannan matsala ita ce amfani da numpy.matsi aiki. Wannan aikin yana cire shigarwar mai girma ɗaya daga sifar tsararrun shigarwa.
import numpy as np arr = np.random.rand(2, 3, 1) print("Original array shape:", arr.shape) collapsed_arr = np.squeeze(arr, axis=-1) print("Collapsed array shape:", collapsed_arr.shape)
Bayanin mataki-mataki
Bari yanzu mu rushe lambar kuma mu fahimci yadda yake aiki.
1. Da farko, muna shigo da ɗakin karatu na NumPy azaman np:
import numpy as np
2. Na gaba, mun ƙirƙiri tsararru mai girma 3 bazuwar tare da siffa (2, 3, 1):
arr = np.random.rand(2, 3, 1) print("Original array shape:", arr.shape)
3. Yanzu, muna amfani da np. matsa aiki don rugujewar ƙira ta ƙarshe ta tsararrun ta hanyar tantancewa axis siga kamar -1:
collapsed_arr = np.squeeze(arr, axis=-1) print("Collapsed array shape:", collapsed_arr.shape)
4. A sakamakon haka, mun sami sabon tsararru mai siffar (2, 3), yana nuna cewa an sami nasarar rushe girman ƙarshe.
Madadin Magani: Sake siffata
Wata hanyar rugujewar girma na ƙarshe shine ta amfani da numpy.sake siffa aiki tare da sigogi masu dacewa don cimma sakamakon da ake so.
collapsed_arr_reshape = arr.reshape(2, 3) print("Collapsed array shape using reshape:", collapsed_arr_reshape.shape)
A wannan yanayin, mun sake fasalin asalin tsararru a sarari don samun siffa ta (2, 3), tana rushe girma na ƙarshe yadda ya kamata.
Dakunan karatu masu alaƙa da Ayyuka
Baya ga NumPy, akwai wasu ɗakunan karatu da yawa a cikin yanayin yanayin Python waɗanda ke ba da kayan aikin aiki tare da tsararru da matrices. Ɗayan irin wannan ɗakin karatu shine SciPy, wanda ke ginawa akan NumPy kuma yana ba da ƙarin ayyuka don lissafin kimiyya. A fagen koyon injin, ɗakin karatu TensorFlow Hakanan yana aiki tare da tenors (watau tsararrun nau'ikan girma dabam) kuma yana ba da tsarin sa na ayyukan sarrafa matrix. Bugu da kari, da Panda ana iya amfani da ɗakin karatu don sarrafa Fassarar bayanai, tsarin bayanai mafi girma wanda za'a iya tunanin shi azaman tebur mai ɗauke da tsararru. Bugu da ƙari, da nupy.newaxis Aiki yana ba ku damar ƙara sabon axis zuwa tsararru, wanda zai iya zama da amfani lokacin da kuke buƙatar faɗaɗa girman tsararrun don dacewa da siffar da ake buƙata don aiki.
A ƙarshe, ikon sarrafawa da aiki tare da tsararru yadda ya kamata shine fasaha mai mahimmanci a duniyar shirye-shirye da kimiyyar bayanai. NumPy babban ɗakin karatu ne mai ƙarfi wanda ke ba da ayyuka da yawa, kuma fahimtar dabaru kamar rugujewar girma na ƙarshe zai kasance da fa'ida a cikin yanayi iri-iri yayin mu'amala da manyan bayanai masu rikitarwa.