Version:0.9 StartHTML:0000000105 EndHTML:0000014152 StartFragment:0000001234 EndFragment:0000014136
#!/usr/bin/env python
# shows predict of single sample with one feature!
# https://maxbox4.wordpress.com/2019/11/24/keras-visual-validation/
# purpose: predict the square with one single value, locs=50
import fileinput
import glob
import os
import numpy as np
import sys
def html():
os.system('sphinx-build -b html -d build/doctrees . build/html')
def train(X,y):
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(X,y)
return model
# sample values with target
X=[1, 2, 3, 4, 5] #shape [5,n_samples, 1,n_features]
y=[1, 4, 9, 16, 25] #shape [5,n_samples, 1,n_targets]
# ValueError: Expected 2D array, got 1D array instead:
# So you have a shape of (5,1) for input X and (5,) or (5,1) for target y.
X = np.array(X).reshape(-1, 1)
y = np.array(y) # You may omit this step if you want
print(X , y)
model = train(X,y)
print('model score: ',model.score(X,y))
# now predict a single sample with one feature:
X_new = np.array([12]).reshape(-1, 1)
print('predict single: ',model.predict(X_new))
# And then again at prediction time:
X_new = np.array([20, 40, 42, 500, 700, 90]).reshape(-1, 1)
print('predict multi: ',model.predict(X_new))
if len(sys.argv)>1:
for arg in sys.argv[1:]:
func = funcd.get(arg)
if func is None:
raise SystemExit('Dont know how to handle %s; valid args are'%(
arg, funcd.keys()))
func()
else: # all()
print('test shell with all()')