0

I'm pretty new to Python, and I'm trying to translate a Matlab code. I'm trying to write a program that begins with a user uploading or entering their data from an IR training spectrum, and then the program appending it into an array or a matrix. But I'm not certain that I'm doing this right (especially because I keep getting errors!)

# 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

The error I'm getting is this:

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

If I input "1,2,3" and "1", "1", the program runs fine. How do I get it to recognize each of those inputs as separate items in a list?

rrauenza
  • 6,285
  • 4
  • 32
  • 57

1 Answers1

1

Your code is ok so far but btrain = [input("Input btrain as a list of values separated by commas.")] will end up being a list of a single string or a list of a tuple of your values if you're on python 2.7. The correct way to do it would be

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

split(delimiter) gives a list of all values splitted at some delimiter in this case ","

RainbowRevenge
  • 211
  • 1
  • 7
  • Thanks, that fixed it. I am getting a new error now when I command it to print(btrain_multidimensional_array) Traceback (most recent call last): File "C:\Users\Cynthia\Documents\Lodder Lab\Spectral Analysis\Supa Fly.py", line 24, in print(btrain_multidimensional_array) NameError: name 'btrain_multidimensional_array' is not defined Any idea why the object I commanded it to return doesn't exist? – Cyndi Dickerson Jun 08 '16 at 22:03
  • Well this error comes from the fact that np.reshape() does not return anything. It just reshapes an exisiting array so you don't need to assign a new variable. Your other error should go away as well. – RainbowRevenge Jun 08 '16 at 22:31