Katiso ea matrix ke ts'ebetso ea mantlha ho algebra ea mola.
Ka kakaretso re e sebelisa lits'ebetsong tse ngata joalo ka ts'ebetso ea litšoantšo, ho ithuta ka mochini le tse ling tse ngata. NumPy ke sephutheloana se tsebahalang sa Python bakeng sa komporo ea mahlale.
Leha ho le joalo, posong ena, re tla sheba mekhoa e fapaneng ea ho atisa matrix ho Python ntle le ho sebelisa NumPy.
Re tla e sebelisa lihlaha sehlaha, mosebetsi oa 'mapa o hahelletsoeng ka hare, le kutloisiso ea lenane.
Ho phaella moo, re tla sheba melemo le mathata a leano le leng le le leng, hammoho le nako ea ho sebelisa e 'ngoe le e' ngoe ea tsona. Haeba u le mocha ho linear algebra 'me u batla ho ithuta haholoanyane ka katiso ea matrix; tswela pele ho bala.
Re Sebelisa Ho Atisa Matrix Hokae?
Katiso ea matrix e sebelisoa ho Grafiken khomphutha ho fetola litšoantšo tsa 2D le 3D. Mohlala, o ka potoloha, oa lekanya le ho fetolela lintho tse skrineng. Li-matrix li sebelisoa ha ho etsoa litšoantšo ho emela litšoantšo e le lihlopha tsa pixels. Ntle le moo, li-matrix li ka sebelisoa ho etsa ts'ebetso joalo ka ho sefa litšoantšo.
Re boetse re sebelisa matrix ho ho ithuta mochine. Li ka re thusa ho emela lintlha le liparamente tsa mohlala. Re ka etsa mesebetsi e mengata, joalo ka lihlahisoa tsa matheba a khomphutha le lihlahisoa tsa matrix-vector.
Ehlile, ts'ebetso ena e boetse e na le molemo o moholo lits'ebetsong tsa mahlale. Re ka e sebelisa ho fisiks le boenjiniere ho hlalosa bongata ba 'mele. Kahoo, re ka sebetsa ka li-vectors le tensor.
Hobaneng re ka se Khetha ho Sebelisa NumPy?
Ha NumPy e le a Laebrari ea Python, ha se kamehla e leng khetho e nepahetseng bakeng sa ho atisa matrix. Re kanna ra se khethe ho sebelisa NumPy ka mabaka a kang boholo le ho itšetleha, ho ithuta, le litsamaiso tsa lefa.
Ho sebelisa mesebetsi e hahelletsoeng ka har'a Python kapa ho nts'etsapele khoutu ea moetlo ho ka sebetsa hantle maemong a mang. Ho bohlokoa ho hlokomela, leha ho le joalo, hore NumPy ke laeborari e matla. Ntle le moo, o ka e sebelisa hape bakeng sa ho atisa matrix.
Joale, ha re shebeng hore na re ka fihlela keketseho ea matrix joang ntle le NumPy.
Nested loops mokhoa
Mokhoa oa ho loops oa sehlaha o sebelisa li-loops tse hahelletsoeng ho etsa katiso ea matrix ho Python. Ts'ebetso e pheta-pheta holim'a elemente ka 'ngoe ea matrix. 'Me, e li atisa ho sebelisa letoto la li-loops tse hahiloeng. Mosebetsi o khutlisa sephetho, se bolokiloeng ka matrix a macha.
Mokhoa ona o bonolo ho o utloisisa. Leha ho le joalo, e kanna ea se sebetse hantle joalo ka mekhoa e meng, haholo bakeng sa matrices a maholo. Leha ho le joalo, ke khetho e ntle ho uena haeba u le mocha ho linear algebra.
def matrix_multiplication(A, B):
# Determine the matrices' dimensions.
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
# Beha sephetho sa matrix ho zeroes.
result = [[0 for row in range(cols_B)] for col in
range(rows_A)]
# Iterate through rows of A
for s in range(rows_A):
# Iterate through columns of B
for j in range(cols_B):
# Iterate through rows of B
for k in range(cols_A):
result[s][j] += A[s][k] * B[k][j]
return result
A re be le mohlala oa kamoo re ka etsang sena kateng. U ka eketsa mela ena ea khoutu e ka tlase ho leka mohlala ona.
# Sample matrices
A = [[1, 4, 3], [4, 9, 6]]
B = [[7, 8], [9, 10], [11, 12]]
# Perform matrix multiplication
result = matrix_multiplication(A, B)
# Print the result
print(result)
# Output: [[76, 84], [175, 194]]
melemo e:
- Ho bonolo ho utloisisa.
- E ntle bakeng sa ba qalang kapa ba batlang kutloisiso e tebileng ea katiso ea matrix.
go tlhoka mesola:
- Ha e sebetse joalo ka mekhoa e meng, haholo bakeng sa matrices a maholo.
- Ha e balehe joalo ka mekhoa e meng.
'mapa () mokhoa oa ts'ebetso
'Mapa () mokhoa oa ts'ebetso o fana ka mokhoa o mong oa ho atisa matrix ho Python. Ka mokhoa ona, re sebelisa mosebetsi oa 'mapa o hahelletsoeng ka hare. Kahoo, re sebelisa sesebelisoa se sebetsang sa lenaneo se sebelisang mosebetsi o fanoeng ho ntho e 'ngoe le e' ngoe e ka khonehang (lenane, tuple, joalo-joalo). Hape, Mosebetsi oa 'mapa () o amohela liparamente tse peli, mosebetsi le e ka hlakang. 'Me, e khutlisetsa iterator e sebelisang ts'ebetso ho ntho e ngoe le e ngoe e ka phetoang.
Ka mokhoa ona, re feta ho setho se seng le se seng sa matrix mme re etsa katiso re sebelisa 'mapa oa sehlaha().
Ts'ebetso ea zip () e sebelisetsoa ho pheta-pheta karolo e 'ngoe le e 'ngoe ea matrices ka ho bapa.
Qetellong, kakaretso () ts'ebetso e sebelisoa ho kenyelletsa sephetho.
def matrix_multiplication(A, B):
# To get the dimensions of the matrices
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
# We use map() function for multiplication.
result = [[sum(a * b for a, b in zip(row_a, col_b)) for
col_b in zip(*B)] for row_a in A]
return result
Joale, hape, re ka leka khoutu ea rona ka mohlala.
# Example matrices
A = [[3, 2, 3], [4, 5, 6]]
B = [[7, 8], [9, 10], [11, 12]]
# Use map() function to perform matrix multiplication
result = list(map(lambda x: list(map(lambda y: sum(i*j
for i,j in zip(x,y)), zip(*B))), A))
# Print the result
print(result)
# Output: [[72, 80], [139, 154]]
le menyetla
- E sebetsa hantle ho feta mokhoa oa ho lopolla
- E sebelisa lenaneo le sebetsang ho etsa hore khoutu e be bonolo haholoanyane.
go tlhoka mesola
- Batho ba bang ba sa tloaelaneng le mananeo a sebetsang a ka 'na a fumana a sa balehe haholo.
- Ha e utloisisehe ho feta mokhoa oa ho loops oa sehlaha.
Lethathamo la mokhoa oa kutloisiso
Kutloisiso ea lethathamo e u nolofalletsa ho hlahisa lenane le lecha moleng o le mong oa khoutu. Kahoo, sena ke ka ho sebelisa polelo ho setho se seng le se seng sa lenane le teng.
Ka mokhoa ona, katiso e etsoa ka ho pheta-pheta khafetsa ka setho se seng le se seng sa matrix. Re sebelisa kutloisiso ea lethathamo la layered.
# Sample matrices
A = [[1, 12, 3], [14, 5, 6]]
B = [[7, 8], [9, 10], [12, 12]]
# Matrix multiplication using list comprehension
result = [[sum(A[i][k] * B[k][j] for k in range(len(A[0])))
for j in range(len(B[0]))] for i in range(len(A))]
# Print the result
print(result)
[[151, 164], [215, 234]]
melemo e
- Ha ho bapisoa le 'mapa() mokhoa oa ts'ebetso, o mokhuts'oane ebile o baloa haholoanyane.
go tlhoka mesola
- E kanna ea se sebetse hantle ho feta ts'ebetso ea 'mapa(), haholo bakeng sa matrices a maholo.
- Ho thata ho feta mokhoa oa ho lopolla sehlaha.
fihlela qeto e
Ka poso ena, re shebile mekhoa e meng ea ho sebelisa NumPy ha u atisa matrices ho Python. Re entse katiso ea matrix ka liloop tse kentsoeng, 'mapa o hahelletsoeng(), le ho utloisisa lenane.
Leano le molemo ka ho fetisisa le tla itšetleha ka litlhoko tse khethehileng tsa morero oa hau.
E 'ngoe le e' ngoe ea maano e na le melemo le boiketlo ba eona. Ho etsa bonnete ba hore ts'ebetso e sebetsa hantle, ke mohopolo o motle ho kenya likhese tsa liteko tse nang le litekanyo tse fapaneng tsa matrix le boleng.
U lokela hape ho kenyelletsa liteko tse ling tsa ts'ebetso ho bapisa hore na mekhoa ena e sebetsa hantle hakae.
Leave a Reply