2016-06-08 4 views
0

저는 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 ", 프로그램이 제대로 실행됩니다. 각 입력을 목록의 개별 항목으로 인식하게하려면 어떻게해야합니까?

+1

단지 사용자 기반 발언

btrain = input("Input btrain as a list of values separated by commas.").split(",") 

분할 (구분)이 경우 몇 가지 구분에 분할 할 모든 값의 목록이 제공 될 것입니다해야 할 일 - 사람에게 물어 보지 않습니다 매트릭스를 수작업으로 표준 입력란에 놓습니다. 이것은 유용한 접근법이 아닙니다. .csv – lejlot

+1

"* 오류가 계속 * *"와 같이 일반적인 형식으로 된 행렬을 포함하는 파일의 경로를 요청하면 Google에서 도움을 줄 수있는 정보가 아닙니다. –

+0

@lejlot 이것은 아마도 어리석은 질문 일 것입니다.하지만 어떻게 할 수 있습니까? –

답변

1

코드가 정상적으로 작성되었지만 btrain = [input("Input btrain as a list of values separated by commas.")]은 Python 2.7을 사용하는 경우 단일 문자열 목록 또는 값 튜플 목록이됩니다. 올바른 방법은 ","

+0

고마워요. 지금 내가 명령 할 때 새로운 오류가 발생했습니다. print (btrain_multidimensional_array) 추적 (가장 최근 통화 마지막) : 파일 "C : \ Users \ Cynthia \ Documents \ Lodder Lab \ Spectral Analysis \ Supa Fly.py" , 줄 24, 인쇄 (btrain_multidimensional_array) NameError : 'btrain_multidimensional_array'name이 정의되지 않았습니다. 내가 반환하도록 명령 한 객체가 왜 존재하지 않는지 알고 싶습니다. –

+0

이 오류는 np.reshape()가 아무 것도 반환하지 않는다는 사실에서 비롯된 것입니다. 새로운 변수를 할당 할 필요가 없도록 기존 어레이를 재구성합니다. 다른 오류도 사라져야합니다. – RainbowRevenge

관련 문제