Create a square matrix in python - python-3.x

I want to get an number from user an create a square matrix of number*number but I can't do it for now. Could you please help me about it. It should look like this:
https://i.stack.imgur.com/ZxtCl.png

import numpy as np
input = 4
matrix = np.array(range(0, input**2 - 1)).reshape((input, input))

Related

How to create a variable number of dimensions for mgrid

I would like to create a meshgrid of variable dimensions by specifying the dimensions with a variable i.e. specifying dim=2, rather than manually changing the expression as in the example below to set a 2D mesh grid.
How would I implement a wrapper function for this?
The problem stems from not being familiar with the syntax that mgrid uses (index_tricks).
import numpy as np
mgrid = np.mgrid[
-5:5:5j,
-5:5:5j,
]
Observed documentation for mgrid, but there seems to be no info on setting the number of dimensions with a variable.
You can create a tuple containing the slice manually, and repeat it some number of times:
import numpy as np
num_dims = 2
mgrid = np.mgrid[(slice(-5, 5, 5j),) * num_dims]

How to get the interceipt from model summary in Python linearmodels?

I am running a panel reggression using Python linearmodels, something like:
import pandas as pd
from linearmodels.panel import PanelOLS
data = pd.read_csv('data.csv', sep=',')
data = data.set_index(['panel_id', 'date'])
controls = ['A','B','C']
controls['const'] = 1
model = PanelOLS(data.Y, controls, entity_effects= True)
result = model.fit(use_lsdv=True)
I really need to pull out the coefficient on the constant, but looks like this would not work
intercept = result.summary.const
Could not really find the answer in
linearmodels' documentation on github
More generally, does anyone know how to pull out the estimate coefficients from the linearmodels summary? Thank you!
result.params['const']
would give the intercept, in general result.params gives the series of regression coefficients in linearmodels

Find function with numpy

I have a numpy array and I want to find all the indexes that verifies a certain condition. Example, I want to plot the Heaviside function;
import numpy as np
x=np.linspace(-5,5,11)
k_neg=x.find(x<0)
k_pos=x.find(x>=0)
y=np.zeros(len(x))
y(k_neg)=-1
y(k_pos)=1
I don't find such a function (like it exists on Matlab).
Note : my actual problem IS NOT to plot Heavyside, of corse ;)
As said by Paul Panzer;
Sounds like you are looking for np.where
Which solved my problem.
I would do it in one line with numpy:
import numpy as np
x = np.linspace(-5,5,11)
y = ((x>=0)*2)-1

Python equivalent of Pulse Integration (pulsint) MATLAB function?

I am surfing online to find Python equivalent of function pulsint of MATLAB , and till now I haven't found anything significantly close to it.
Any heads up in this regards will be really helpful!
You can easily create your own pulsint function.
The pulsint formula:
You need the numpy library to keep the things simple
import numpy as np
import matplotlib as mpl
# Non coherent integration
def pulsint(x):
return np.sqrt(np.sum(np.power(np.absolute(x),2),0))
npulse = 10;
# Random data (100x10 vector)
x = np.matlib.repmat(np.sin(2*np.pi*np.arange(0,100)/100),npulse,1)+0.1*np.random.randn(npulse,100)
# Plot the result
mpl.pyplot.plot(pulsint(x))
mpl.pyplot.ylabel('Magnitude')

Parse txt-file, from string to int/float in Python3.x

Currently, I need to parse the string saved in a rot.txt fileļ¼š
3 3
-0.0963063 0.994044 -0.0510079
-0.573321 -0.0135081 0.81922
0.813651 0.10814 0.571207
The first row denotes the dim of a rotation matrix, and the rest is a rotation matrix.
I tried the following lines:
import numpy as np
rotation = np.genfromtxt(f_name, delimiter=" ")
Obviously, the first row should be skipped. How can I fix this? THX in advance.

Resources