ML-PSE / Machine_Learning_for_DPS

Code repository for the book 'Machine Learning in Python for Dynamic Process Systems'
https://mlforpse.com/books/
Apache License 2.0
5 stars 1 forks source link

About example code : #1

Closed SelmaCho closed 5 months ago

SelmaCho commented 8 months ago

Dear Dr. Kumar

I am a student who is studying System Identification based on your latest book.

There is a error in example code, but I cannot fix it.

I would appreciate you if you let me know how to tackle this error.

ValueError Traceback (most recent call last) Cell In[71], line 4 1 # get model's 1-step ahead and 5-step ahead predictions, and simulation responses on validation dataset 2 from sippy import functionset as fset ----> 4 TO_val_predicted_1step_centered = np.transpose(fset.validation(ARXmodel, FG_val_centered, TO_val_centered, np.linspace(0,299,300), k=1)) 5 TO_val_predicted_5step_centered = np.transpose(fset.validation(ARXmodel, FG_val_centered, TO_val_centered, np.linspace(0,299,300), k=5)) 6 TO_val_simulatedcentered, , _ = control.matlab.lsim(ARXmodel.G, FG_val_centered[:,0], np.linspace(0,299,300))

File ~\Desktop\Python\SysID\Chapter_InputOutputModels_Part1\sippy\functionset.py:192, in validation(SYS, u, y, Time, k, centering) 189 for i in range(ydim): 190 # one-step ahead predictor 191 if k == 1: --> 192 T, Y_u = cnt.forced_response((1/SYS.H[i,0])*SYS.G[i,:], Time, u) 193 T, Y_y = cnt.forced_response(1 - (1/SYS.H[i,0]), Time, y[i,:] - y_rif[i]) 194 Yval[i,:] = (Y_u + np.atleast_2d(Y_y) + y_rif[i])

ValueError: too many values to unpack (expected 2)

ML-PSE commented 8 months ago

Hi SelmaCho,

This issue seems to be arising from the Control package. What is the version of the Control package that you are using?

With regards, Ankur

SelmaCho commented 8 months ago

Dear Dr. Kumar.

I originally used control version 0.8.4; however, it shows severe error. Hence, I changed version of control to 0.9.0.

Now, it does not show error of fset.validation.

But, it shows error of "y_scaler.inverse_transform"

I've attached the code and error.

get model's 1-step ahead and 5-step ahead predictions, and simulation responses on validation dataset

from sippy import functionset as fset

TO_val_predicted_1step_centered = np.transpose(fset.validation(ARXmodel, FG_val_centered, TO_val_centered, np.linspace(0,299,300), k=1)) TO_val_predicted_5step_centered = np.transpose(fset.validation(ARXmodel, FG_val_centered, TO_val_centered, np.linspace(0,299,300), k=5)) TO_val_simulatedcentered, , _ = control.matlab.lsim(ARXmodel.G, FG_val_centered[:,0], np.linspace(0,299,300))

TO_val_predicted_1step = y_scaler.inverse_transform(TO_val_predicted_1step_centered) TO_val_predicted_5step = y_scaler.inverse_transform(TO_val_predicted_5step_centered) TO_val_simulated = y_scaler.inverse_transform(TO_val_simulated_centered)

plt.figure(figsize=(18,7.5)) plt.plot(TO_val, 'g', linewidth=0.8, label='Measurements') plt.plot(TO_val_predicted_1step, 'r', linewidth=0.8, label='1-step ahead predictions') plt.plot(TO_val_predicted_5step, 'c', linewidth=0.8, label='5-step ahead predictions') plt.plot(TO_val_simulated, 'm', linewidth=0.8, label='Simulation response') plt.title('Validation data') plt.ylabel('y(k): Measured vs predicted/simulated'), plt.xlabel('k') plt.legend(), plt.xlim(0) plt.grid(which='both', axis='y', linestyle='--') plt.show()

C:\Users\selma\anaconda3\envs\SysIDBox\Lib\site-packages\control\timeresp.py:293: UserWarning: return_x specified for a transfer function system. Internal conversion to state space used; results may meaningless. warnings.warn(

ValueError Traceback (most recent call last) Cell In[14], line 10 8 TO_val_predicted_1step = y_scaler.inverse_transform(TO_val_predicted_1step_centered) 9 TO_val_predicted_5step = y_scaler.inverse_transform(TO_val_predicted_5step_centered) ---> 10 TO_val_simulated = y_scaler.inverse_transform(TO_val_simulated_centered) 12 plt.figure(figsize=(18,7.5)) 13 plt.plot(TO_val, 'g', linewidth=0.8, label='Measurements')

File ~\anaconda3\envs\SysIDBox\Lib\site-packages\sklearn\preprocessing_data.py:1048, in StandardScaler.inverse_transform(self, X, copy) 1045 check_is_fitted(self) 1047 copy = copy if copy is not None else self.copy -> 1048 X = check_array( 1049 X, 1050 accept_sparse="csr", 1051 copy=copy, 1052 dtype=FLOAT_DTYPES, 1053 force_all_finite="allow-nan", 1054 ) 1056 if sparse.issparse(X): 1057 if self.with_mean:

File ~\anaconda3\envs\SysIDBox\Lib\site-packages\sklearn\utils\validation.py:938, in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name) 936 # If input is 1D raise error 937 if array.ndim == 1: --> 938 raise ValueError( 939 "Expected 2D array, got 1D array instead:\narray={}.\n" 940 "Reshape your data either using array.reshape(-1, 1) if " 941 "your data has a single feature or array.reshape(1, -1) " 942 "if it contains a single sample.".format(array) 943 ) 945 if dtype_numeric and hasattr(array.dtype, "kind") and array.dtype.kind in "USV": 946 raise ValueError( 947 "dtype='numeric' is not compatible with arrays of bytes/strings." 948 "Convert your data to numeric values explicitly instead." 949 )

ValueError: Expected 2D array, got 1D array instead: array=[ 0. -1.77067118 -4.08237657 -6.13208918 -7.71222312 -8.89063397 -9.76248414 -10.39410209 -10.83889306 -11.14575695 -11.3554652 -11.49805559 -11.59439948 -11.65900482 -8.34001859 -3.97926755 -0.10630247 2.88149106 5.11078768 6.76082819 7.95658885 8.79883396 9.3799909 9.77719738 10.0473057 10.22982629 10.35222823 10.43379169 10.48791351 10.52371655 10.54732959 10.56285449 10.57303356 10.57969301 10.58404195 10.58687727 10.58872281 10.58992238 10.5907011 10.59120607 10.5915332 7.22971105 2.84053395 -1.05123697 -4.05143415 -6.28888622 -7.94427416 -9.1435334 -9.98806334 -10.57070993 -10.96888607 -11.23962466 -11.4225544 -11.54522168 -11.62695703 -11.68119011 -11.7170651 -11.74072462 -11.75627956 -11.76647801 -11.77314995 -11.77750695 -11.78034746 -11.78219635 -11.78339806 -8.42214428 -4.03333613 -0.14180443 2.85823778 5.09558957 6.75091266 7.95013 8.79463288 9.37726201 6.413393 2.2948103 -1.41412403 -4.29171408 -6.44746961 -8.04864952 -9.2120499 -10.03293071 -10.60002906 -10.98801107 -11.25208049 -11.43065502 -11.55048294 -11.63037014 -11.68340197 -11.71849716 -11.74165102 -11.75687837 -11.7668648 -11.77339963 -11.77766803 -11.78045132 -11.78226329 -11.78344119 -11.78420594 -11.78470191 -11.78502325 -8.42319736 -4.03401785 -0.14224538 2.85795278 5.0954055 6.75079385 7.95005335 8.79458346 9.37723016 9.77540637 10.04614501 10.22907478 10.35174208 10.43347744 10.48771052 10.52358551 10.54724505 10.56279998 10.57299843 10.57967037 10.58402737 10.58686788 10.58871678 10.58991849 10.5906986 10.59120446 10.59153217 10.59174427 10.59188143 10.59197006 10.5920273 10.59206424 10.59208807 10.59210343 10.59211332 10.5921197 10.5921238 10.59212645 10.59212815 10.59212924 10.59212994 10.59213039 10.59213068 10.59213087 10.59213099 10.59213107 10.59213112 10.59213115 10.59213117 10.59213118 10.59213119 10.5921312 10.5921312 10.5921312 10.5921312 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 10.59213121 7.23009732 2.8407833 -1.05107611 -4.05133042 -6.28881937 -7.94423109 -9.14350567 -9.98804548 -10.57069844 -10.96887868 -11.23961991 -11.42255135 -11.54521972 -11.62695577 -11.68118929 -11.71706458 -11.74072429 -11.75627934 -11.76647787 -11.77314986 -11.77750689 -11.78034742 -11.78219633 -11.78339805 -11.78417816 -11.78468403 -11.78501174 -11.78522384 -11.785361 -11.78544964 -11.78550687 -11.78554381 -11.78556764 -8.42354911 -4.03424498 -0.14239196 2.85785825 5.09534455 6.75075458 7.95002806 8.79456718 9.37721968 9.77539962 10.04614067 10.22907199 10.35174028 10.43347629 10.48770978 10.52358504 10.54724474 10.56279979 10.57299831 10.57967029 10.58402732 7.22483396 2.83736884 -1.05328884 -4.05276304 -6.28974611 -7.94483013 -9.14389261 -9.98829526 -10.57085958 -10.96898258 -11.23968687 -11.42259449 -11.5452475 -11.62697365 -11.6812008 -11.71707198 -11.74072905 -11.7562824 -11.76647984 -11.77315112 -11.7775077 -11.78034794 -11.78219666 -11.78339826 -11.7841783 -11.78468412 -11.7850118 -11.78522388 -11.78536103 -11.78544965 -11.78550688 -11.78554382 -11.78556764 -11.785583 -11.7855929 -11.78559927 -11.78560338 -11.78560602 -11.78560772 -11.78560881 -11.78560951 -11.78560997 -11.78561026 -11.78561044 -11.78561056 -8.42357676 -4.03426278 -0.14240341 2.85785088 5.09533982 6.75075153 7.9500261 ]. 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. Click to add a cell.

Thank you for your consideration.

Regards, Selma.

SelmaCho commented 8 months ago

P.S. Also, where can I the data for example code in chapter 2?

"InputOutputData.txt " is not shown in your github repository. Please upload this file.

ML-PSE commented 8 months ago

Hi SelmaCho,

This issue can be handled by modifying the statement

TO_val_simulated = y_scaler.inverse_transform(TO_val_simulated_centered) as TO_val_simulated = y_scaler.inverse_transform(TO_val_simulated_centered[:, None])

It seems version differences impact the output shapes.

Regarding the Chapter 2 data: I originally did not intend to upload Chapter 2 data; the code in the book was only for a quick introduction to how a SysID script looks like.

With regards, Ankur

SelmaCho commented 8 months ago

Dear Dr. Ankur,

Thank you for your correction.

Regards. Selma