Input
2 3 **Here 2,3 are size of mateix m,n**
2 3 4
5 6 7
output
[
[2 3 4]
[5 6 7]
]
Note:-use Numpy package
If the input matrix is in the file you can use the following program:
import numpy as np
x = np.loadtxt('file.txt', dtype=int, delimiter=' ') # you don't need to give the matrix size at the beginning of the file
print(x)
rows = int(input('give number of rows: '))
cols = int(input('give number of columns: '))
x = np.zeros((rows,cols), dtype=int)
for i in range(rows):
for j in range(cols):
x[i,j] = int(input(f"give matrix element for row {i} and column {j}: "))
print(x)
If you want the user to input the matrix elements use the second part of the program.
Reading m x n input as a numpy matrix :
import numpy as np
m,n = map(int, input().split())
arr = np.zeros((m,n))
for i in range(m):
arr[i] = np.array(list(map(int, input().split())))
Output :
array([[1., 2., 3.],
[4., 5., 6.]])
Related
My goal: Get a 25x2 array like the following (a sorted list with its coordinates)
0 0
0 1
0 2
0 3
0 4
1 0
1 1
..
..
4 3
4 4
My (failed) solution:
import numpy as np
n=5
lis=np.zeros((n*n,2))
for i in range(n):
for j in range(n):
print(i,j) # This prints just what I want to have in the array
lis[j,0]=i
lis[j,1]=j
Output:
array([[4., 0.],
[4., 1.],
[4., 2.],
[4., 3.],
[4., 4.],
[0., 0.],
[0., 0.],
....
The row number is just j. It has to increment by 1 every iteration, even when j goes back to 0 on the next iteration of the i loop.
for i in range(n):
for j in range(n):
lis[i*n + j, 0] = i
lis[i*n + j, 1] = j
Another way to do it is to loop in the range up to n*n, and then calculate the indices using division and remainder.
for i in range(n*n):
lis[i, 0] = i // n
lis[i, 1] = i % n
When you access lis by lis[j,..], j ranges from 0 to 4, thus you never update rows after.
You have some options to achive your goal:
Following your logic you can do:
for i, e in enumerate(range(n*n)):
lis[i] = e//n, e%n
Using cartesian product:
import itertools
for i, e in enumerate(itertools.product(range(n), range(n))):
lis[i] = e
I have a ton of data file in the same format as described below and I'm trying to make a colormesh plot from this:
0 0 1 2
-3 1 7 7
-2 1 2 3
-1 1 7 3
[0 1 2] of the first row are values for the y axis of the plot, and [-3 -2 -1] of the first column are values for the x values of the same plot. The first 0 is only for spacing
these are the numbers that I really want inside the pcolormesh:
1 7 7
1 2 3
1 7 3
I'm trying to read these values and store into a 2D matrix as:
Matrix = [[1. 7. 7.]
[1. 2. 3.]
[1. 7. 3.]]
Here is a figure ilustrating it further:
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
# ------------- Input Data Files ------------- #
data = np.loadtxt('my_colormesh_data.dat') # Load Data File
# ------ Transform Data into 2D Matrix ------- #
Matrix = []
n_row = 4 # Number of rows counting 0 from file #
n_column = 4 # Number of columns couting 0 from file #
x = data[range(1,n_row),0] # Read x axis values from data file and store in a variable #
y = data[0, range(1,n_column)] # Read y axis values from data file and store in a variable #
print(data)
print('\n', x) # print values of x (for checking)
print('\n', y) # print values of y (for checking)
for i in range (2, n_row):
for j in range(2, n_column):
print(i, j, data[i,j]) # print values of i, j and data (for checking)
Matrix[i,j] = data[i,j]
print(Matrix)
and results in this error:
Matrix[i,j] = data[i,j]
TypeError: list indices must be integers or slices, not tuple
Could you clarify what i'm doing wrong?
Thanks in advance!
You are getting the error because Matrix is a list and you are trying to index it using a tuple, i,j. And that is not a valid operation. You can index a list oly with integers or slices
Secondly your data variable is already a 2D array. You don't have to any further conversions.
In order to skip the first row and first column you can simply use index slicing.
>>> input_data = """0 0 1 2
... -3 1 7 7
... -2 1 2 3
... -1 1 7 3 """
>>>
>>> data = np.loadtxt(StringIO(input_data))
>>> data
array([[ 0., 0., 1., 2.],
[-3., 1., 7., 7.],
[-2., 1., 2., 3.],
[-1., 1., 7., 3.]])
>>> data[1:,1:]
array([[1., 7., 7.],
[1., 2., 3.],
[1., 7., 3.]])
My input:
a b c d e f
g h i j k l
My output should have three sets of solutions like this:
Sq( a**2 + d**2 ) + Sq ( g**2 + j**2 )
Sq( b**2 + e**2 ) + Sq ( h**2 + k**2 )
Sq( c**2 + f**2 ) + Sq ( i**2 + l**2 )
My actual text file has so many rows and columns with no header. This is what I have so far:
import os
import math
import numpy as np
for file in os.listdir("directory"):
if file.endswith(".txt"):
fin=open(file, 'r')
total = 0
for line in fin:
str = [float(x) for x in line.split()]
for i in range(len(str[0:5])):
str[i]=float(str[i])
sum=np.sum((math.pow(str[i],2)+math.pow(str[i+3],2))**0.5
total += sum
fin.close()
With a file:
1 2 3 4 5 6
11 12 13 14 15 16
Correcting indention and range:
with open('stack53269737.txt') as f:
total = 0
for line in f:
str = [float(x) for x in line.split()]
for i in range(3):
str[i]=float(str[i])
sum=np.sum((math.pow(str[i],2)+math.pow(str[i+3],2))**0.5)
total += sum
In [111]: total
Out[111]: 73.84586902040324
with further cleanup
with open('stack53269737.txt') as f:
total = 0
for line in f:
alist = [float(x) for x in line.split()]
for i in range(3):
total += (alist[i]**2+alist[i+3]**2)**0.5
We don't need to convert to float twice; we don't need math for simple squares.
A numpy approach:
load it with a numpy csv reader:
In [126]: data = np.genfromtxt('stack53269737.txt')
In [127]: data
Out[127]:
array([[ 1., 2., 3., 4., 5., 6.],
[11., 12., 13., 14., 15., 16.]])
reshape the array to express your row splitting:
In [128]: data1 = data.reshape(2,2,3)
In [129]: data1
Out[129]:
array([[[ 1., 2., 3.],
[ 4., 5., 6.]],
[[11., 12., 13.],
[14., 15., 16.]]])
Now we can just square all values, sum on the correct axis, take squareroot and sum again:
In [130]: np.sum(np.sum(data1**2, axis=1)**.5)
Out[130]: 73.84586902040324
If you wish to do it without numpy, you could try the following:
import math
with open("data.txt", "r") as infile:
# Split the lines and then split numbers from each line.
lines = list(map(str.split, infile.read().split('\n')))
# Use zip to create tuples of values that take part in each operation.
lines = list(zip(*lines))
# Get length of each line.
lineLength = len(lines)
# Find the total.
total = sum([math.sqrt(int(lines[i][j])**2 + int(lines[i+3][j])**2) for i in range(lineLength-3) for j in range(2)])
print(total)
Given a file with the following data:
1 2 3 4 5 6
7 8 9 10 11 12
The result is:
57.02450048972068
Suppose we have a matrix of dimension N x M and we want to reduce its dimension preserving the values in each by summing the firs neighbors.
Suppose the matrix A is a 4x4 matrix:
A =
3 4 5 6
2 3 4 5
2 2 0 1
5 2 2 3
we want to reduce it to a 2x2 matrix as following:
A1 =
12 20
11 6
In particular my matrix represent the number of incident cases in an x-y plane. My matrix is A=103x159, if I plot it I get:
what I want to do is to aggregate those data to a bigger area, such as
Assuming you're using a numpy.matrix:
import numpy as np
A = np.matrix([
[3,4,5,6],
[2,3,4,5],
[2,2,0,1],
[5,2,2,3]
])
N, M = A.shape
assert N % 2 == 0
assert M % 2 == 0
A1 = np.empty((N//2, M//2))
for i in range(N//2):
for j in range(M//2):
A1[i,j] = A[2*i:2*i+2, 2*j:2*j+2].sum()
Though these loops can probably be optimized away by proper numpy functions.
I see that there is a solution using numpy.maxtrix, maybe you can test my solution too and return your feedbacks.
It works with a*b matrix if a and b are even. Otherwise, it may fail if a or b are odd.
Here is my solution:
v = [
[3,4,5,6],
[2,3,4,5],
[2,2,0,1],
[5,2,2,3]
]
def shape(v):
return len(v), len(v[0])
def chunks(v, step):
"""
Chunk list step per step and sum
Example: step = 2
[3,4,5,6] => [7,11]
[2,3,4,5] => [5,9]
[2,2,0,1] => [4,1]
[5,2,2,3] => [7,5]
"""
for i in v:
for k in range(0, len(i),step):
yield sum(j for j in i[k:k+step])
def sum_chunks(k, step):
"""
Sum near values with step
Example: step = 2
[
[7,11], [
[5,9], => [12, 11],
[4,1], [20, 6]
[7,5] ]
]
"""
a, c = [k[i::step] for i in range(step)], []
print(a)
for m in a:
# sum near values
c.append([sum(m[j:j+2]) for j in range(0, len(m), 2)])
return c
rows, columns = shape(v)
chunk_list = list(chunks(v, columns // 2))
final_sum = sum_chunks(chunk_list, rows // 2)
print(final_sum)
Output:
[[12, 11], [20, 6]]
New to python here (but have experience in R, SQL).
I tried googling this, however was unable to generate new ideas.
My main purpose is to generate a matrix using my csv data, but I'd like to transpose my 2nd column into a row for the matrix. I'd then like to populate that matrix with the data in my 3rd column, but wasn't able to get anywhere.
After a couple of days, I have come up with this code :
import csv
def readcsv(csvfile_name):
with open(csvfile_name) as csvfile:
file=csv.reader(csvfile, delimiter=",")
#remove rubbish data in first few rows
skiprows = int(input('Number of rows to skip? '))
for i in range(skiprows):
_ = next(file)
#change strings into integers/floats
for z in file:
z[:2]=map(int, z[:2])
z[2:]=map(float, z[2:])
print(z[:2])
return
This just cleans up my data, however what I'd like to do is to transpose the data into a matrix. The data I have is like this (imagine x,y,z,d and other letters are floats):
1 1 x
1 2 y
1 3 z
1 4 d
. . .
. . .
However, I'd like to turn this data into a matrix like this: i.e. I'd like to populate that matrix with data in the 3rd column (letters here just to make it easier to read for you guys) and convert the 2nd column into a row for the matrix. So in essence, the first and second columns of that CSV file are co-ordinates for the matrix.
1 2 3 4 . .
1 x y z d
1 a b c u
1 e f e y
.
.
I tried learning numpy, however it appears like it requires my data to already be in a matrix form.
If you want to use numpy you've got two options depending on how your data is stored.
If it is GUARANTEED that your keys increase consistently, e.g:
THIS NOT THIS
------ --------
1 1 a 1 1 a
1 2 b 1 3 b
1 3 c 2 1 c
1 4 d 3 1 d
2 1 e 1 2 e
2 2 f 1 4 f
2 3 g 8 8 g
2 4 h 2 2 h
Then simply take all the values in the far right column and chuck them into a flat numpy array and reshape according to the maximum values in the left and middle column.
import numpy as np
m = np.array(right_column)
# For the sake of example:
#: array([1., 2., 3., 4., 5., 6., 7., 8.])
m = m.reshape(max(left_column), max(middle_column))
#: array([[1., 2., 3., 4.],
#: [5., 6., 7., 8.]])
If it is not guaranteed, you could either sort it so that it is (probably easiest), OR create a zero array of the correct shape and cycle through each element.
# Example data
left_column = [1, 2, 1, 2, 1, 2, 1, 2]
middle_column = [1, 1, 3, 3, 2, 2, 4, 4]
right_column = [1., 5., 3., 7., 2., 6., 4., 8.]
import numpy as np
m = np.zeros((max(left_column), max(middle_column)), dtype=np.float)
for x, y, z in zip(left_column, middle_column, right_column):
x -= 1 # Because the indicies are 1-based
y -= 1 # Need to be 0-based
m[x, y] = z
print(m)
#: array([[ 1., 2., 3., 4.],
#: [ 5., 6., 7., 8.]])