mismatch in sizes with test and train data during function print

185
November 30, 2019, at 8:40 PM

Data file I would like to process has 71 records build with two columns: one for x value and second one for y value. Main task is to select training part and testing part, print chosen functions (in my example I've taken linear and exponential(^4) one. However I've stumbled upon error I can't solve.

Full description of the error:

File="zad1.py", line 25, in module
v = np.linalg.pinv(c) @ y
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, 
with gufunc signature (n?, k),(k, m?)->(n?,m?) (size 71 is different from 53)

code

from sklearn.model_selection import train_test_split
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt
a = np.loadtxt('dane10.txt')
x = a[:,[1]]
y = a[:,[0]]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.33)
c = np.hstack([X_train, np.ones(X_train.shape)])
v = np.linalg.pinv(c) @ y
plt.plot(X_train, y_train, 'ro')
plt.plot(X_test, y_test, 'go')
plt.plot(X_train,v[0]*X_train + v[1])

c = np.hstack([
   X_train * X_train * X_train * X_train,
   X_train * X_train * X_train,
   X_train * X_train,
   X_train,
   np.ones(X_train.shape)])
v = np.linalg.pinv(c) @ y
plt.plot(v[0]*X_train^4 + v[1]*X_train^3 + v[2]*X_train^2 + v[3]*X_train +v[4])
plt.show()

Would appreciate any help :).

I've redone it a little and both functions are being printed now but the exponentail one is kinda weird...I mean something is not right here because it's not adjusting to the points od diagram but it's being printed way further.

from sklearn.model_selection import train_test_split
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt
a = np.loadtxt('dane10.txt')
x = a[:,[1]]
y = a[:,[0]]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.33)
c = np.hstack([x, np.ones(x.shape)])
v = np.linalg.pinv(c) @ y
plt.plot(X_train, y_train, 'ro')
plt.plot(X_test, y_test, 'go')
plt.plot(X_train,v[0]*X_train + v[1])
c = np.hstack([
x * x * x * x,
x * x * x,
x * x,
x,
np.ones(x.shape)])
v = np.linalg.pinv(c) @ y
plt.plot(v[0]*X_train*X_train*X_train*X_train  + v[1]*X_train*X_train*X_train 
+ 
v[2]*X_train*X_train + v[3]*X_train +v[4])
plt.show()
Answer 1

The problem apparently happens when you multiply X_train * X_train. Since it is not a square matrix, it can not be multiplied by itself. Do you just need to raise each number in X_train to 2d-4th power? In that case use numpy.multiply

READ ALSO
3D plotting in Python - Adding a Legend to Scatterplot

3D plotting in Python - Adding a Legend to Scatterplot

Essentially I'd like to add a legend for the scatterplot that shows the unique values in y_train and what color point it corresponds to on the plot

111
Get combobox value to feed into button command in python tkinter

Get combobox value to feed into button command in python tkinter

so I'm learning tkinter by building a "weightlifting GUI" for funAs it stands, the application mostly runs

103
Live graph in python with a fixed window

Live graph in python with a fixed window

I am trying to construct a live graph in python to plot random numbers in a graph with a fixed plot window The width of the plot window will be 20 samplesFor the 21st sample, the 1st sample will dissappear from the right side

113
How to subtract dictionaries from a list

How to subtract dictionaries from a list

I have a list of dictionaries (not all in the same length) like -

128