저는 Python을 처음 접했고 Matlab 코드를 번역하려고합니다. 사용자가 IR 교육 스펙트럼에서 데이터를 업로드하거나 입력 한 다음 배열이나 행렬에 추가하는 프로그램으로 시작하는 프로그램을 작성하려고합니다. 하지만 난이 권리를하고 있어요 것을 확실하지 않다사용자에게 파이썬에서 행렬을 입력하도록 요청하는 방법은 무엇입니까?
# Requires numpy and math.
# Import necessary modules.
import numpy as np
import math
# Get data for the training spectra as a list.
# Then turn that list into a numpy array given the user's input of how many
# rows and columns there should be.
# (An alternate way to do this would be to have users input it with commas and
# semi-colons.)
# btrain_matrix returns the array.
def btrain_matrix():
btrain = [input("Input btrain as a list of values separated by commas.")]
btrain_row_number = int(input("How many rows should there be in this matrix? \n i.e., how many training samples were there?"))
btrain_column_number = int(input("How many columns should there be in this matrix? \n i.e., how many peaks were trained?"))
btrain_array=np.array(btrain)
btrain_multidimensional_array = btrain_array.reshape(btrain_row_number,btrain_column_number)
print(btrain_multidimensional_array)
return (btrain_multidimensional_array)
btrain_matrix()
btrain_row_number = input("Please re-enter the number of rows in btrain.")
# Insert a sequence to call btrain_matrix here
내가지고있어 오류가 이것이다 (나는 점점 오류를 유지 특히 때문에!) : 나는 입력하면
Input btrain as a list of values separated by commas.1,2,3
How many rows should there be in this matrix?
i.e., how many training samples were there?1
How many columns should there be in this matrix?
i.e., how many peaks were trained?3
Traceback (most recent call last):
File "C:\Users\Cynthia\Documents\Lodder Lab\Spectral Analysis\Supa Fly.py", line 24, in <module>
btrain_matrix()
File "C:\Users\Cynthia\Documents\Lodder Lab\Spectral Analysis\Supa Fly.py", line 19, in btrain_matrix
btrain_multidimensional_array = btrain_array.reshape(btrain_row_number,btrain_column_number)
ValueError: total size of new array must be unchanged
"1 , 2,3 "및"1 ","1 ", 프로그램이 제대로 실행됩니다. 각 입력을 목록의 개별 항목으로 인식하게하려면 어떻게해야합니까?
단지 사용자 기반 발언
분할 (구분)이 경우 몇 가지 구분에 분할 할 모든 값의 목록이 제공 될 것입니다해야 할 일 - 사람에게 물어 보지 않습니다 매트릭스를 수작업으로 표준 입력란에 놓습니다. 이것은 유용한 접근법이 아닙니다. .csv – lejlot
"* 오류가 계속 * *"와 같이 일반적인 형식으로 된 행렬을 포함하는 파일의 경로를 요청하면 Google에서 도움을 줄 수있는 정보가 아닙니다. –
@lejlot 이것은 아마도 어리석은 질문 일 것입니다.하지만 어떻게 할 수 있습니까? –