Angelo Mendes
Angelo Mendes

Reputation: 978

How to convert a matrix as string to ndarray?

I have a csv file with this structure:

id;matrix
1;[[1.2 1.3] [1.2 1.3] [1.2 1.3]]

I'm trying read the matrix field as numpy.ndarray using pandas.read_csv to read and making df.to_numpy() to convert the matrix, but the shape array result in (1,0). I was waiting for the shape equals (3,2) as:

matrix = [[1.2 1.3] 
          [1.2 1.3]
          [1.2 1.3]]

I was try too numpy.asmatrix, but the result is like df.to_numpy()

Upvotes: 0

Views: 67

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62523

A solution with pandas

  • Providing the format of the matrix column is consistent with that shown in the example, replace the spaces with ,, then use literal_eval to turn the string into a list of lists, and then apply np.array.
import pandas as pd
from ast import literal_eval
import numpy as np

# read the data
df = pd.read_csv('file.csv', sep=';')

# replace the spaces
df['matrix'] = df['matrix'].str.replace(' ', ',')

# apply literal_eval
df['matrix'] = df['matrix'].apply(literal_eval)

# apply numpy array
df['matrix'] = df['matrix'].apply(np.array)

print(type(df.iloc[0, 1]))

>>> numpy.ndarray
  • Each row of the matrix column will be an ndarray
  • The two apply calls can be combined into:
    • df['matrix'] = df['matrix'].apply(lambda x: np.array(literal_eval(x)))
  • Or this hot mess:
    • df['matrix'] = df['matrix'].str.replace(' ', ',').apply(lambda x: np.array(literal_eval(x)))
    • I personally prefer one transformation per line for code clarity.

Upvotes: 1

Related Questions