X_train, X_test, Y_train, Y_test = pp.split_train_test(x_ary=X, y_ary=Y, train_size=0.8) # Feature Scaling #X = pp.feature_scaling(fit_ary=X, transform_arys=(X)) #Y = pp.feature_scaling(fit_ary=Y, transform_arys=(Y)) # In[] Linear Regression as comparison from HappyML.regression import SimpleRegressor import HappyML.model_drawer as md reg_simple = SimpleRegressor() Y_simple = reg_simple.fit(x_train=X, y_train=Y).predict(x_test=X) md.sample_model(sample_data=(X, Y), model_data=(X, Y_simple)) print("R-Squared of Simple Regression:", reg_simple.r_score(x_test=X, y_test=Y)) # In[] Polynomial Regression #from sklearn.preprocessing import PolynomialFeatures #from HappyML.performance import rmse #import pandas as pd # #deg=5 #poly_reg = PolynomialFeatures(degree=deg) #X_poly = pd.DataFrame(poly_reg.fit_transform(X)) # #regressor = SimpleRegressor() #regressor.fit(X_poly, Y) #Y_predict = regressor.predict(x_test=X_poly)
# In[] Visualize the Training Set #import matplotlib.pyplot as plt # #plt.scatter(X_train, Y_train, color="red") #plt.plot(X_train, regressor.predict(X_train), color="blue") #plt.title("Salary vs. Experience") #plt.xlabel("Experience") #plt.ylabel("Salary") #plt.show() from HappyML import model_drawer as md sample_data = (X_train, Y_train) model_data = (X_train, regressor.predict(X_train)) md.sample_model(sample_data=sample_data, model_data=model_data, title="訓練集樣本點 vs. 預測模型", font="DFKai-sb") md.sample_model(sample_data=(X_test, Y_test), model_data=(X_test, Y_pred), title="測試集樣本點 vs. 預測模型", font="DFKai-sb") # In[] Test for Linearity of Features #from HappyML import model_drawer as md # #for i in range(X_train.shape[1]): # md.sample_model(sample_data=(X_train[:, i], Y_train), model_data=None, title="Linearity of Column {}".format(i)) from HappyML.criteria import AssumptionChecker checker = AssumptionChecker(X_train, X_test, Y_train, Y_test, Y_pred)
regressor[1][0].fit(X_w_train, Y_w_train.iloc[:, 0].to_frame()) regressor[1][1].fit(X_w_train, Y_w_train.iloc[:, 1].to_frame()) print("台灣 6~15 歲學童身高、體重評估系統\n") gender = eval(input("請輸入您的性別(1.男 2.女):")) - 1 age = eval(input("請輸入您的年齡(6-15):")) height = eval(input("請輸入您的身高(cm):")) weight = eval(input("請輸入您的體重(kg):")) h_avg = regressor[0][gender].predict(x_test=pd.DataFrame([[age]])).iloc[0, 0] w_avg = regressor[1][gender].predict(x_test=pd.DataFrame([[age]])).iloc[0, 0] model_data_h = (X_h_train, regressor[0][gender].predict(X_h_train)) md.sample_model(sample_data=(age, height), model_data=model_data_h, title="身高落點分布", xlabel="年齡", ylabel="身高", font="Microsoft JhengHei") if gender == 0: print(age, "歲男生平均身高為", "{:.2f}".format(h_avg), "公分,您的身高為", height, "公分") elif gender == 1: print(age, "歲女生平均身高為", "{:.2f}".format(h_avg), "公分,您的身高為", height, "公分") model_data_w = (X_w_train, regressor[1][gender].predict(X_w_train)) md.sample_model(sample_data=(age, weight), model_data=model_data_w, title="體重落點分布", xlabel="年齡", ylabel="體重", font="Microsoft JhengHei") if gender == 0:
import HappyML.preprocessor as pp dataset = pp.dataset("C:/Users/henry/Desktop/Python Training/Python機器學習/範例原始碼&「快樂版」函式庫/Ch05 Regression/Position_Salaries.csv") X, Y = pp.decomposition(dataset, x_columns=[1], y_columns=[2]) X_train, X_test, Y_train, Y_test = pp.split_train_test(x_ary=X, y_ary=Y, train_size=0.8) # In[] from HappyML.regression import SimpleRegressor import HappyML.model_drawer as md reg_simple = SimpleRegressor() Y_simple = reg_simple.fit(x_train=X, y_train=Y).predict(X) md.sample_model(sample_data=(X, Y), model_data=(X, Y_simple)) print("R-Squared of Simple Regression:", reg_simple.r_score(x_test=X, y_test=Y)) # In[] from sklearn.preprocessing import PolynomialFeatures deg = 12 poly_reg = PolynomialFeatures(degree=deg) X_poly = poly_reg.fit_transform(X) # In[] import pandas as pd regressor = SimpleRegressor() regressor.fit(X_poly, Y) Y_predict = regressor.predict(x_test=pd.DataFrame(X_poly))