Invalid syntax erroExpected 2D array, got 1D array instead for linear regression - linear-regression

this is my sample dataset
RH
0 79.8
1 79.2
2 79.4
3 78.4
4 77.8
My code is trying to replace the missing values in the dataset using linear regression but i am getting an error.
why am i getting an error for this code of mine
linreg=LinearRegression()
datawithnull= taoRH["RH"].dropna()
datawithoutnull= datawithnull.dropna()
train_data_x= datawithoutnull.values.reshape(-1, 1)
train_data_y= datawithoutnull.values.reshape(-1, 1)
linreg.fit(train_data_x,train_data_y)
test_data= datawithnull.iloc[:]
rh_predicted[["RH"]]= pd.DataFrame(linreg.predict(test_data))
datawithnull.RH.fillna(rh_predicted.RH,inplace=True)
i am getting this as the error
ValueError: Expected 2D array, got 1D array instead:
array=[79.8 79.2 79.4 ... 75.2 74.2 74.9].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Related

Solving the Linear Regression Model using QR Decomposition (MATLAB)

Background : I want to implement a MATLAB algorithm that takes as input vectors x and y, solves the linear regression problem
associated with the data stored in x and y using a modified QR version and then to plot the graph of the linear function.
So first I wrote the modified QR algorithm :
function x=QRQ(A,b,n)
[Q1,R1]=qr(A);
c1=Q1'*b;
n=length(c1);
x=backward(R1,c1,n);
end
function x=backward(U,y,n)
x=zeros(n,1);
x(n)=y(n)/U(n,n);
for i=n -1 : -1 : 1
x(i)=(y(i)-U(i,i+1 : n)*x(i+1 : n))/U(i,i);
end
end
Then I wrote the algorithm for the linear regression :
function ysol = LinearReg(x,y)
A=[x ones(21,1)];
z=QRQ(A,y,2);
ysol=z(1)*x+z(2);
plot(x,y,'bo',x,ysol,'g-');
end
I tried to run this algorithm on the following data :
x=[0;0.25;0.5;0.75;1;1.25;1.5;1.75;2;2.25;2.5;2.75;3;3.25;3.5;3.75;4;4.25;4.5;4.75;5];
y=[4;3;7;7;1;4;4;6;7;7;2;6;6;1;1;4;9;3;5;2;7];
The full error message that I received is :
Index in position 2 exceeds array bounds (must not exceed 2).
Error in untitled>backward (line 12)
x(n)=y(n)/U(n,n);
Error in untitled>QRQ (line 8)
x=backward(R1,c1,n);
Error in untitled>LinearReg (line 20)
z=QRQ(A,y,2);
The line causing the error is x(n)=y(n)/U(n,n);
the only variable with an index in position 2 is U. Apparently U only has 2 columns, and n is a value >2, hence the error.
Using the debugger, I see that U is a 21x2 array, and n has a value of 21.
How can this MATLAB algorithm be fixed?
The U in your case is R1. Since the matrix A has rank two, R1 will only have two columns since A already only has two columns.
You then try to solve the system R1 * x = y using backward substitution with the index starting at n, but here you clearly have to start at 2.
(Keep in mind that R1 is an upper triangular matrix.)

matlab ARX parameter

I want to use ARX. X is a 1000X13 matrix (1000 sample with 13 features). I want to see the relationship of for example 1st and 2nd column of X. I don't know how to make input parameters right. What should be the size of [na nb nk] for my regression problem. Matlab documentation doesn't have much detail.
Here is my code:
data = iddata(X(:,1),[],1); %I have to make iddata object first.
Y = arx(data,[ [ones(size(X(:,1),2),size(X(:,1),2))] [ones(size(X(:,2),1),size(X(:,1),1))] [ones(size(X(:,1),2),size(X(:,1),1))] ])
Error is:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
I tried to change the dimensions of [na nb nk], but every time, I got an error like:
Y = arx(data,[ [ones(size(X(:,1),2),size(X(:,1),2))] 1 [ones(size(X(:,1),2),size(X(:,1),1))] ])
Invalid ARX orders. Note that continuous-time ARX models are not supported.
Y = arx(data,[ 1 1 1])
Error using arx (line 77)
The model orders must be compatible with the input and output dimensions of the estimation data.

Forecast w/ AR(3) model using Y0 data error in MATLAB

So I'm using an arima(3,0,0) model to forecast values in Matlab. I have a vector of initial values for it to use to forecast off of but I keep getting an error.
Here is my code:
model1=arima(3,0,0);
[EstMdl1,~,logL1]=estimate(model1,qtrdatachangelog2000(:,1));
Ymdl1pred=zeros(length(qtrdatachangelogafter2000),1);
for i=1:length(qtrdatachangelogafter2000)-2;
[Ymdl1pred(i)]=forecast(EstMdl1,1,'Y0',qtrdatachangelogafter2000(i+2,1));
end;
The error I get:
Error using internal.econ.LagIndexableTimeSeries.checkPresampleData (line 653)
Number of rows in presample array 'Y0' must be at least 3.
Error in arima/forecast (line 498)
Y0 = internal.econ.LagIndexableTimeSeries.checkPresampleData(zeros(maxPQ,numPaths),
'Y0', Y0, OBJ.P);
I'm assuming this is because the AR(3) has 3 parameters and thus needs at least 3 rows of data before it can start which is why in my for loop I used i+2 but it continues to give the error. Please help.
Using i+2 you are not adding 3 rows, you are using only 1 row. You need to specify the start and end index: i:i+2. You can use this code:
[Ymdl1pred(i)]=forecast(EstMdl1,1,'Y0',qtrdatachangelogafter2000(i:i+2,1));

Index out of bounds because of numel MATLAB

I am trying to create bit scales images. I am getting an error with this code. What is wrong?
clc
clear all
a=imread('image.tif');%read file
[row col]=size(a);%row of image and column of image
b=zeros(row,col,8);%3D 0 matrix
for k=1:8%position of bit
for i=1:row%for every row
for j=1:col%for every column
bits = de2bi(a(i,j));
b(i,j,k)=bits(k);
end%endFor
end%endFor
end%endFor
for k=1:8
subplot(3,3,k);
imshow(b(:,:,k));
title(strcat(num2str(k),'. bit'));
end%endFor
ERROR:
??? Attempted to access bits(2); index out of bounds because numel(bits)=1.
Error in ==> soru1 at 13
b(i,j,k)=bits(k);
At this line of code:
bits = de2bi(a(i,j));
You are calling de2bi on the value of the pixel at "i,j". Presuming that the image you are opening is of type uint8, the value of a(i,j) can be anywhere between 0 and 255. If these values are 0 or 1, the output of de2bi as you call it is just "0" or "1" - that is, it only has one element in it, and you cannot access the second, non-existent element.
To correct this, you need to force the size of the output of de2bi to be the size you require, which can be done using a second input, like this:
bits = de2bi(a(i,j),8)
In fact the loop isn't required since de2bi, like most MATLAB functions, can handle vectors or matrices as input, not just single numbers:
a=imread('image.tif');
b=de2bi(a);
b = reshape(de2bi,[size(a),8]);

Error in matrix dimension mismatch

I am trying to model a vector data containing 200 sample points denoting a measurement.I want to see "goodness of fit" and after reading I found that this can be done by predicting the next set of values(I am not that confident though if this is the correct way).I am stuck at this since the following code gives an error and I am just unable to solve it.Can somebody please help in removing the error
Error using *
Inner matrix dimensions must agree.
Error in data_predict (line 27)
ypred(j) = ar_coeff' * y{i}(j-1:-1:j-p);
Also,can somebody tell me how to do the same thing i.e get the coefficients using nonlinear AR modelling,moving average and ARMA since using the command nlarx() did not return any model coefficients?
CODE
if ~iscell(y); y = {y}; end
model = ar(y, 2, 'yw');
%prediction
yresiduals=[];
nsegments=length(y);
ar_coeffs = model.a;
ar_coeff=[ar_coeffs(2) ar_coeffs(3)]
for i=1:nsegments
pred = zeros(length(y{i}),1);
for j=p+1:length(y{i})
ypred(j) = ar_coeff(:)' * y{i}(j-1:-1:j-p);
end
yresiduals = [yresiduals; y{i}(p+1:end) - ypred(p+1:end)];
end
In matlab, * is the matrix product between two matrices. That means that the number of columns in the first matrix must equal the number of rows in the second matrix. You might have intended to use .* element by element multiplication. EDIT: For element by element multiplication, the matrices must be the same size. Check the size of your matrices. If they don't fit either of these conditions, something needs to change.