I am trying to load the data from a file. There are 3 rows and columns and I want to convert into an array A
with shape (3,3)
. How do I go about doing it?
JavaScript
x
3
1
import numpy as np
2
np.loadtxt('A.csv')[:, 0]
3
The data looks like
The array A
should be
JavaScript
1
5
1
A=np.array([[0.751900795,0.720029442,0.519947357],
2
[0.757660601,0.682370477,0.693973342],
3
[0.799340382,0.641430593,0.73287523]])
4
5
Advertisement
Answer
Following my comment, convert the file to a panda dataframe first to get rid of the empty rows in your dataset. Then convert the resulting dataframe to numpy.
data.csv
JavaScript
1
7
1
0.751900795,0.720029442,0.519947357
2
3
0.757660601,0.682370477,0.693973342
4
5
0.799340382,0.641430593,0.73287523
6
7
code
JavaScript
1
7
1
import pandas as pd
2
3
df = pd.read_csv('data.csv', header=None)
4
s = df. to_numpy()
5
print(s)
6
7
output
JavaScript
1
5
1
[[0.75190079 0.72002944 0.51994736]
2
[0.7576606 0.68237048 0.69397334]
3
[0.79934038 0.64143059 0.73287523]]
4
5