Force regression line through origin using sns.jointplot - regression

I am relatively new to python. I have a x-variable and a y-variable plotted against each other using the sns.jointplot function. I know when y is zero, x must also be zero. Is there a way to force the regression line through the origin to satisfy this arguement?
Thanks

Absolutely! You have a constrained linear regression on your hands. You want to find the best linear model in describing your data, right? There are different ways to approach this, but I recommend you try the function scipy.optimize.lsq_linear in SciPy. If you're already working in Seaborn this shouldn't be a problem.
This function allows you to input your linear system and a constraint, and then solves the least-squares problem to satisfy the constraint and minimize residuals.

Related

Libreoffice Calc doing a linear regression with missing data values

In Libreoffice calc, if you try to do a linear regression where some of the data is missing it just returns an Error Err:502. Is there a way for me to ask Calc to just drop/ignore the datapoints with missing values, without me having to explicitly delete the datapoints with incomplete values (since I want to keep this information)?
While doing the linear regression that doesn't seem to be an option. It would be easy to make an extra line with a boolean to say whether to use that datapoint in the linear regression or not, but then I don't know how to tell the linear regression to only use data based on the boolean. Is there for example a way to hide some of the columns from the linear regression based on a boolean?
In the above image, I would want to omit column D from the linear regression be I don't want to delete that column because I don't want to loose the data it contains.
Any help welcome.
On another sheet, possibly hidden, you could use formulas to copy over the data to include. Then do the regression on that sheet and output the regression results back to the main sheet.
These formulas could no doubt be modified based on the boolean row if you want to do that rather than simply specifying directly in the formula which columns to use.

values in torch.nn.conv2d and torch.nn.Linear

I am confiused how to get the out_channels in torch.nn.Conv2d and the in_features, out_features in torch.nn.Linear.
For example I have a non-color 28*28 image input.
the in_channels = 1,kernel_size=5,padding=2 how can I figure the out_channels.
After the convolutional, I want to make a linear layer.
How do I figure the values of in_features, out_features ?
The choice of out_channels is up to you, it's the number of filters you want your convolutional layer to compute. The higher this number is, the heavier the layer will be, but on the other hand the more features it will be able to learn (theoretically).
After going through the convolution (assuming out_channels = C), your data will have shape (C, 28, 28). In other words, one sample contains 28*28*C numbers / dimensions. It is this number that you need to input as in_features for the following linear layer. Then again, out_features is up to you.
I strongly suggest that you read and learn about neural networks (and their typical convolutional and linear layers) before attempting to use them to make magic happen. Without the required knowledge about them, you will at best be able to produce results that you don't really understand, and at worst issues that you don't know how to fix. It takes time to learn, but it really is worth it.

Best techinique to approximate a 32-bit function using machine learning?

I was wondering which is the best machine learning technique to approximate a function that takes a 32-bit number and returns another 32-bit number, from a set of observations.
Thanks!
Multilayer perceptron neural networks would be worth taking a look at. Though you'll need to process the inputs to a floating point number between 0 and 1, and then map the outputs back to the original range.
There are several possible solutions to your problem:
1.) Fitting a linear hypothesis with least-squares method
In that case, you are approximating a hypothesis y = ax + b with the least squares method. This one is really easy to implement, but sometimes, a linear model is not good enough to fit your data. But - I would give this one a try first.
Good thing is that there is a closed form, so you can directly calculate parameters a and b from your data.
See Least Squares
2.) Fitting a non-linear model
Once seen that your linear model does not describe your function very well, you can try to fit higher polynomial models to your data.
Your hypothesis then might look like
y = ax² + bx + c
y = ax³ + bx² + cx + d
etc.
You can also use least squares method to fit your data, and techniques from the gradient descent types (simmulated annealing, ...). See also this thread: Fitting polynomials to data
Or, as in the other answer, try fitting a Neural Network - the good thing is that it will automatically learn the hypothesis, but it is not so easy to explain what the relation between input and output is. But in the end, a neural network is also a linear combination of nonlinear functions (like sigmoid or tanh functions).

how to get max. value for a non linear data

I am a new Matlab user..so quite unfamilier with most of its power...Actually I need to get the maximum value in a non linear moment curvature curve...I define the theoretical max. and min. curvature values in the program and then divide it in small discrete increments...but the problem is...the max. value sometimes occur in between two increments...so the program misses that one...and it stops before finding the max. value...Please help me...how can I overcome this problem
You will need to approximate the curve, using an interpolation/fitting scheme that depends on the problem and the curve shape, and the known functional form. A spline might be appropriate, or perhaps not.
Once you have a viable approximation that connects the dots so to speak, you minimize/maximize that function. This is an easily solved problem at that point.
There is a method to solve non linear functions (find minima/maxima)
It uses least squares non linear method and I think is called lsqnonlin(). Find it in optimization toolbox. Also solve() might work. Another option is to use simulated annealing but I don't remember the name of the function.
Sorry I don't supply code. I am answering from iphone

algorithm to solve related equations

I am working on a project to create a generic equation solver... envision this to take the form of 25-30 equations that will be saved in a table- variable names along with the operators.
I would then call this table for solving any equation with a missing variable and it would move operators/ other pieces to the other side of the missing variable
e.g. 2x+ 3y=z and if x were missing variable. I would call equation with values for y and z and it would convert to solve for x=(z-3y)/2
equations could be linear, polynomial, binary(yes/no result)...
i am not sure if i can get any light-weight library available or whether this needs to built from scratch... any pointers or guidance will be appreciated
See Maxima.
I rather like it for my symbolic computation needs.
If such a general black-box algorithm could be made accurate, robust and stable, pigs could fly. Solutions can be nonexistent, multiple, parametrized, etc.
Even for linear equations it gets tricky to do it right.
Your best bet is some form of Newton algorithm, but generally you tailor it to your problem at hand.
EDIT: I didn't see you wanted something symbolic, rather than numerical. It's another bag of worms.