{"id":175,"date":"2023-04-04T10:03:46","date_gmt":"2023-04-04T08:03:46","guid":{"rendered":"https:\/\/www.medialearn.de\/?page_id=175"},"modified":"2023-04-04T10:03:46","modified_gmt":"2023-04-04T08:03:46","slug":"ml5","status":"publish","type":"page","link":"https:\/\/www.medialearn.de\/index.php\/ml5\/","title":{"rendered":"ML5"},"content":{"rendered":"<p>#!\/usr\/bin\/env python3<br \/>\n# -*- coding: utf-8 -*-<br \/>\n&#8222;&#8220;&#8220;<br \/>\nCreated on Wed Mar 3 10:58:27 2021<br \/>\nKeras Modell Zeitreihe ml 5 Versicherung<br \/>\n@author: guttmann<br \/>\n&#8222;&#8220;&#8220;<br \/>\nimport pandas as pd<br \/>\ndf = pd.read_csv(&#8222;insurance.csv&#8220;)<br \/>\nprint(df.head)<br \/>\nX=df[[&#8218;age&#8216;,&#8217;bmi&#8216;,&#8217;children&#8216;]].values<br \/>\ny=df[[&#8218;charges&#8216;]].values<br \/>\n#&#8230;&#8230;&#8230;&#8230;Normierung<br \/>\nfrom sklearn.preprocessing import StandardScaler<br \/>\nscaler_X=StandardScaler()<br \/>\nX=scaler_X.fit_transform(X)<br \/>\nscaler_y=StandardScaler()<br \/>\ny=scaler_y.fit_transform(y)<br \/>\n#&#8230;&#8230;&#8230;&#8230;&#8230;.Modell<br \/>\nfrom tensorflow.keras.models import Sequential<br \/>\nfrom tensorflow.keras.layers import Dense<br \/>\nmodel = Sequential()<br \/>\nmodel.add(Dense(units=1, input_shape=(3,)))<br \/>\nmodel.summary()<br \/>\nmodel.compile(loss=&#8217;mse&#8216;,metrics=[&#8218;accuracy&#8216;],optimizer=&#8217;sgd&#8216;)<br \/>\nhistory=model.fit(X,y, epochs=5,batch_size=32)<br \/>\n#&#8230;&#8230;&#8230;&#8230;..Modell speichern<br \/>\nmodel.save(&#8222;ML5.h5&#8220;)<br \/>\n#&#8230;&#8230;&#8230;&#8230;..Evaluation<br \/>\nfrom sklearn.metrics import r2_score<br \/>\ny_pred=model.predict(X)<br \/>\nr2=r2_score(y,y_pred)<br \/>\nprint(&#8218;Evaluation : &#8218;,&#8217;r2={:.3f}&#8216;.format(r2))<br \/>\nprint(&#8218;Gewichte :&#8216;,model.get_layer(index=0).get_weights())<br \/>\n#&#8230;&#8230;&#8230;&#8230;&#8230;.Prediction<br \/>\nX_pred=[[40.,20.1,1.]] #40 jahre, bmi 20.2, 1 kind<br \/>\nX_pred=scaler_X.transform(X_pred)<br \/>\ny_pred=model.predict(X_pred)<br \/>\ny_pred=scaler_y.inverse_transform(y_pred)<br \/>\nprint (&#8218;loss : &#8218;,history.history[&#8218;loss&#8216;])<br \/>\nprint (&#8218;accuracy : &#8218;,history.history[&#8218;accuracy&#8216;])<br \/>\nprint (&#8218;keys : &#8218;,history.history.keys())<br \/>\nprint (&#8218;Vorhersage : &#8218;,y_pred)<\/p>\n<p>#&#8230;&#8230;&#8230;&#8230;..Funktion plotten<br \/>\nimport matplotlib.pyplot as plt<br \/>\nplt.plot(history.history[&#8218;loss&#8216;])<br \/>\nplt.plot(history.history[&#8218;accuracy&#8216;]),plt.legend([&#8218;train&#8216;, &#8218;test&#8216;], loc=&#8217;upper right&#8216;)<br \/>\nplt.title(&#8218;model accuracy&#8216;)<br \/>\nplt.ylabel(&#8218;accuracy&#8216;),plt.xlabel(&#8218;epoch&#8216;)<br \/>\nplt.show()<br \/>\n# summarize history for loss<br \/>\nplt.plot(history.history[&#8218;loss&#8216;])<br \/>\nplt.title(&#8218;model loss&#8216;),plt.legend([&#8218;train&#8216;, &#8218;test&#8216;], loc=&#8217;upper right&#8216;)<br \/>\nplt.ylabel(&#8218;loss&#8216;),plt.xlabel(&#8218;epoch&#8216;)<br \/>\nplt.show()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#!\/usr\/bin\/env python3 # -*- coding: utf-8 -*- &#8222;&#8220;&#8220; Created on Wed Mar 3 10:58:27 2021 Keras Modell Zeitreihe ml 5 Versicherung @author: guttmann &#8222;&#8220;&#8220; import pandas as pd df = pd.read_csv(&#8222;insurance.csv&#8220;) print(df.head) X=df[[&#8218;age&#8216;,&#8217;bmi&#8216;,&#8217;children&#8216;]].values y=df[[&#8218;charges&#8216;]].values #&#8230;&#8230;&#8230;&#8230;Normierung from sklearn.preprocessing import StandardScaler scaler_X=StandardScaler() X=scaler_X.fit_transform(X) scaler_y=StandardScaler() y=scaler_y.fit_transform(y) #&#8230;&#8230;&#8230;&#8230;&#8230;.Modell from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential()&hellip; <a class=\"more-link\" href=\"https:\/\/www.medialearn.de\/index.php\/ml5\/\"><span class=\"screen-reader-text\">ML5<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-175","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/pages\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":1,"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/pages\/175\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/pages\/175\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/www.medialearn.de\/index.php\/wp-json\/wp\/v2\/media?parent=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}