""" Created on Mon Aug 2 18:25:36 2021 @author: henry """ # In[] import HappyML.preprocessor as pp dataset = pp.dataset("C:/Users/henry/Desktop/Python Training/Python機器學習/範例原始碼&「快樂版」函式庫/Ch05 Regression/50_Startups.csv") X, Y = pp.decomposition(dataset, [0, 1, 2, 3], [4]) # X = pp.onehot_encoder(X, columns=[3]) # X = pp.remove_columns(X, [3]) X = pp.onehot_encoder(X, columns=[3], remove_trap=True) X_train, X_test, Y_train, Y_test = pp.split_train_test(X, Y, train_size=0.8) X_train, X_test= pp.feature_scaling(fit_ary=X_train, transform_arys=(X_train, X_test)) Y_train, Y_test= pp.feature_scaling(fit_ary=Y_train, transform_arys=(Y_train, Y_test)) # In[] from HappyML.regression import SimpleRegressor simple_reg = SimpleRegressor() Y_pred_simple = simple_reg.fit(X_train, Y_train).predict(X_test) # R-Squared always increase in multiple linear regression --> Use Adjusted R-Squared instead print("Goodness of Model (R-Squared Score):", simple_reg.r_score(X_test, Y_test))
Created on Mon Jul 15 12:21:45 2019 @author: 俊男 """ # In[] Pre-processing import HappyML.preprocessor as pp # Dataset Loading dataset = pp.dataset("50_Startups.csv") # Independent/Dependent Variables Decomposition X, Y = pp.decomposition(dataset, [0, 1, 2, 3], [4]) # Apply One Hot Encoder to Column[3] & Remove Dummy Variable Trap X = pp.onehot_encoder(X, columns=[3]) X = pp.remove_columns(X, [3]) #X = pp.onehot_encoder(X, columns=[3], remove_trap=True) # Split Training vs. Testing Set X_train, X_test, Y_train, Y_test = pp.split_train_test(X, Y, train_size=0.8) # Feature Scaling (optional) #X_train, X_test = pp.feature_scaling(fit_ary=X_train, transform_arys=(X_train, X_test)) #Y_train, Y_test = pp.feature_scaling(fit_ary=Y_train, transform_arys=(Y_train, Y_test)) # In[] Create Linear Regressor from HappyML.regression import SimpleRegressor simple_reg = SimpleRegressor() Y_pred_simple = simple_reg.fit(X_train, Y_train).predict(X_test)
Created on Mon Aug 26 21:20:09 2019 @author: 俊男 """ # In[] Preprocessing import HappyML.preprocessor as pp # Load Dataset dataset = pp.dataset(file="Mall_Customers.csv") # Decomposition X = pp.decomposition(dataset, x_columns=[1, 2, 3, 4]) # One-Hot Encoding X = pp.onehot_encoder(ary=X, columns=[0], remove_trap=True) # Feature Scaling (for PCA Feature Selection) X = pp.feature_scaling(fit_ary=X, transform_arys=X) # Feature Selection (PCA) from HappyML.preprocessor import PCASelector selector = PCASelector() X = selector.fit(x_ary=X, verbose=True, plot=True).transform(x_ary=X) # In[] K-Means Clustering with Fixed Clusters = 4 (Without HappyML) # from sklearn.cluster import KMeans # import time # # K-Means Clustering with K=4
""" Created on Mon Aug 23 20:11:45 2021 @author: henry """ # In[] import HappyML.preprocessor as pp dataset = pp.dataset(file="Mushrooms.csv") X, Y = pp.decomposition(dataset, x_columns=[i for i in range(1, 23)], y_columns=[0]) X = pp.onehot_encoder(X, columns=[i for i in range(22)], remove_trap=True) Y, Y_mapping = pp.label_encoder(Y, mapping=True) from HappyML.preprocessor import KBestSelector selector = KBestSelector(best_k="auto") X = selector.fit(x_ary=X, y_ary=Y, verbose=True, sort=True).transform(x_ary=X) X_train, X_test, Y_train, Y_test = pp.split_train_test(x_ary=X, y_ary=Y) # In[] from HappyML.classification import DecisionTree classifier = DecisionTree() Y_pred = classifier.fit(X_train, Y_train).predict(X_test)
# In[] Data Pre-processing from sklearn.datasets import load_iris import HappyML.preprocessor as pp # Load Data dataset = load_iris() # X, Y import pandas as pd X = pd.DataFrame(dataset.data, columns=dataset.feature_names) Y = pd.DataFrame(dataset.target, columns=["Iris_Type"]) Y_name = dataset.target_names.tolist() # One_hot incoder for Y Y = pp.onehot_encoder(ary=Y, columns=[0]) # Feature Selection from HappyML.preprocessor import KBestSelector selector = KBestSelector(best_k="auto") X = selector.fit(x_ary=X, y_ary=Y, auto=False, verbose=True, sort=True).transform(x_ary=X) # Split Training / Testing Set X_train, X_test, Y_train, Y_test = pp.split_train_test(x_ary=X, y_ary=Y) # Feature Scaling X_train, X_test = pp.feature_scaling(fit_ary=X_train, transform_arys=(X_train, X_test)) # In[] Neural Networks without HappyML's Class
Created on Tue Sep 7 09:57:19 2021 @author: henry """ import HappyML.preprocessor as pp from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense import pandas as pd from HappyML.performance import ClassificationPerformance dataset = pp.dataset("Churn_Modelling.csv") X, Y = pp.decomposition(dataset, [i for i in range(3, 13)], [13]) X = pp.onehot_encoder(X, [1, 2], True) selector = pp.KBestSelector() X = selector.fit(X, Y, True, True).transform(X) X_train, X_test, Y_train, Y_test = pp.split_train_test(X, Y) X_train, X_test = pp.feature_scaling(X_train, (X_train, X_test)) classifier = Sequential() arithmetic_mean = [0, 0] arithmetic_mean[0] = int((X_train.shape[1] + 1) / 2) arithmetic_mean[1] = int((arithmetic_mean[0] + 1) / 2) experience_law = [0, 0]
""" Created on Sun Aug 29 05:49:05 2021 @author: henry """ import HappyML.preprocessor as pp from HappyML.classification import DecisionTree from HappyML.performance import KFoldClassificationPerformance import HappyML.model_drawer as md from IPython.display import Image, display dataset = pp.dataset("HR-Employee-Attrition.csv") X, Y = pp.decomposition(dataset, [i for i in range(35) if i != 1], [1]) X = pp.onehot_encoder(X, [1, 3, 6, 10, 14, 16, 20, 21], True) Y, Y_mapping = pp.label_encoder(Y, True) selector = pp.KBestSelector() X = selector.fit(X, Y, True, True).transform(X) X_train, X_test, Y_train, Y_test = pp.split_train_test(X, Y) classifier = DecisionTree() Y_pred = classifier.fit(X_train, Y_train).predict(X_test) K = 10 kfp = KFoldClassificationPerformance(X, Y, classifier.classifier, K) print("----- Decision Tree Classification -----") print("{} Folds Mean Accuracy: {}".format(K, kfp.accuracy()))