This question already has answers here:
Adding a column in front of a numpy array
(3 answers)
Closed 2 months ago.
How all how do you add rows numbers to an array using numpy?
I wish to print an array to look like the following:
[1, 39, 41, 43],
[2, 38, 32, 18],
[3, 27, 14, 17],
[4, 22, 21, 22],
[5, 20, 28, 23]
With 1-5 being the row numbers
I can only print the array without row numbers.
np.insert(array, 0, np.arange(array.shape[0]), axis=1)
Related
im trying to print data from a .mat file which contains about 400 rows and 800 columns of data, represented by a 2-D list.
I used scipy.io to load the file, and im trying to print the 2-D list in vs code.
but the result is shortened with "..." to something like
[[55, 23, 12...52, 13, 14]
[28, 26, 12...1, 14, 25]
[28, 13, 12...1, 15, 33]
...
[5, 2, 22...32, 11, 4]
[2, 6, 14...15, 54, 25]
[2, 11, 16...3, 25, 13]]
is there a setting in VS code that allows me to print the entire 400x800 matrix?
Any suggestion is appreciated!
I have a .txt file
[7, 9, 20, 30, 50] [1-8]
[9, 14, 27, 31, 45] [2-5]
[7, 10, 22, 27, 38] [1-7]
that I am trying to read into a data frame of two columns using df = pd.read_fwf(readfile,header=None)
Instead of two columns it forms a data frame with three columns and sometimes reads each of the first list of numbers into five columns
0 1 2
0 [7, 9, 20, 30, 50] [1-8]
1 [9, 14, 27, 31, 45] [2-5]
2 [7, 10, 22, 27, 38] [1-7]
I do not understand what I am doing wrongly. Could someone please help?
You can exploit the two spaces between the lists
pd.read_csv(readfile, sep='\s\s', header=None, engine='python')
Out:
0 1
0 [7, 9, 20, 30, 50] [1-8]
1 [9, 14, 27, 31, 45] [2-5]
2 [7, 10, 22, 27, 38] [1-7]
pd.read_fwf without an explicit widths argument tries to infere the fixed widths. But the length of the first list varies. There is no fixed width to separate each line into two columns.
The widths argument is very usefull if your data has no delimiter but fixed number of letters per value. 40 years ago this was a common data format.
# data.txt
20200810ITEM02PRICE30COUNT001
20200811ITEM03PRICE31COUNT012
20200812ITEM12PRICE02COUNT107
pd.read_csv sep argument accepts multi char and regex delimiter. Often this is more flexible to separate strings to columns.
By single line you can read using pandas
import pandas as pd
df = pd.read_csv(readfile, sep='\s\s')
This question already has answers here:
Transpose list of lists
(14 answers)
Closed 2 years ago.
I want to change the x and y axis of a matrix. For example I want to store the first element of each
nested array in the first line, the second of each nested array on the second line etc...
For example:
list = [[1,2,3,4,5,6]
[7,8,9,10,11,12]
[13,14,15,16,17,18]
[19,20,21,22,23,24]]
I want to change into this:
new list = [[1,7,13,19]
[2,8,14,20]
[3,9,15,21]
[4,10,16,22]
[5,11,17,23]
[6,12,18,24]]
Note: This isnt a rotation
Use numpy.ndarray.T, which is the same as numpy.transpose
import numpy as np
data = [[1,2,3,4,5,6],
[7,8,9,10,11,12],
[13,14,15,16,17,18],
[19,20,21,22,23,24]]
# convert the list of lists to an array
data = np.array(data)
# transpose the array
data_t = data.T
# print(data_t)
array([[ 1, 7, 13, 19],
[ 2, 8, 14, 20],
[ 3, 9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23],
[ 6, 12, 18, 24]])
This question already has an answer here:
How to calculate the outer product of two matrices A and B per rows faster in python (numpy)?
(1 answer)
Closed 6 years ago.
If I have to arrays X (X has n rows and k columns) and Y (Y has n rows and q columns) how do I multiply the two in the vector form, such that I obtain array Z with following characteristics:
Z[0]=X[:,0]*Y
Z[1]=X[:,1]*Y
Z[2]=X[:,2]*Y
...
Z[K-1]=X[:,k-1]*Y
Z[K]=X[:,k]*Y
for c in range(X.shape[1]):
Z[c]=X[:,c].dot(Y)
From your description, and almost no thinking:
Z=np.einsum('nk,nq->kq',X,Y)
I could also write it with np.dot, with a transpose or two. np.dot does the matrix sum over the last dim of the 1st and 2nd to last of 2nd
Z = np.dot(X.T, Y)
=================
In [566]: n,k,q=2,3,4
In [567]: X=np.arange(n*k).reshape(n,k)
In [568]: Y=np.arange(n*q).reshape(n,q)
In [569]: Z=np.einsum('nk,nq->kq',X,Y)
In [570]: Z
Out[570]:
array([[12, 15, 18, 21],
[16, 21, 26, 31],
[20, 27, 34, 41]])
In [571]: Z1=np.empty((k,q))
In [572]: Z1=np.array([X[:,c].dot(Y) for c in range(k)])
In [573]: Z1
Out[573]:
array([[12, 15, 18, 21],
[16, 21, 26, 31],
[20, 27, 34, 41]])
In [574]: X.T.dot(Y)
Out[574]:
array([[12, 15, 18, 21],
[16, 21, 26, 31],
[20, 27, 34, 41]])
This question already has answers here:
NumPy selecting specific column index per row by using a list of indexes
(7 answers)
Closed 8 years ago.
I would like to extract specific elements from a 2d-array by index.
The index specifies the element in the column.
Example:
14, 7, 30
44, 76, 65
42, 87, 11
indices = (0, 1, 2) or (0, 1, 1)
=> result = [14,76,11] or [14, 76, 65]
I don't want to use any loop, just numpy functions and slicing and stuff.
I thought about masking but again I don't know how to generate a mask-2d-array
from the indices-array without a direct loop.
You can use row and column index vectors directly:
import numpy as np
A = np.array([[14, 7, 30],
[44, 76, 65],
[42, 87, 11]])
print A[[0, 1, 2], range(len(A))]
print A[[0, 1, 1], range(len(A))]
(Since you want exactly one item per column, the column index vector is range(len(A)).)
Output:
[14 76 11]
[14 76 65]