├── images ├── 1.png ├── CNN.PNG ├── RNN.PNG ├── k-means.PNG ├── autoencoders.PNG ├── rainforcement.PNG └── gradient-descent.png ├── __pycache__ ├── plot.cpython-312.pyc └── model.cpython-312.pyc ├── requirements.txt ├── env ├── pyvenv.cfg └── share │ └── man │ └── man1 │ └── ttx.1 ├── .gitignore ├── 3_Decision_Trees └── main.py ├── 8_Rainforcement_Learning └── main.py ├── 6_K_means └── main.py ├── main.py ├── 11_Autoencoders └── main.py ├── 9_CNN_DeepLearning └── main.py ├── 2_Logistic_Regression └── main.py ├── 1_Linear_Regression ├── model.py ├── plot.py └── main.py └── README.md /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/1.png -------------------------------------------------------------------------------- /images/CNN.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/CNN.PNG -------------------------------------------------------------------------------- /images/RNN.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/RNN.PNG -------------------------------------------------------------------------------- /images/k-means.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/k-means.PNG -------------------------------------------------------------------------------- /images/autoencoders.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/autoencoders.PNG -------------------------------------------------------------------------------- /images/rainforcement.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/rainforcement.PNG -------------------------------------------------------------------------------- /images/gradient-descent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/images/gradient-descent.png -------------------------------------------------------------------------------- /__pycache__/plot.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/__pycache__/plot.cpython-312.pyc -------------------------------------------------------------------------------- /__pycache__/model.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yosuke-kuroki/AI-Courses-For-Beginners/HEAD/__pycache__/model.cpython-312.pyc -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | keras~=3.8.0 2 | matplotlib~=3.10.0 3 | numpy~=2.0.0 4 | pandas~=2.2.0 5 | tensorflow~=2.18.0 6 | plotly 7 | seaborn 8 | scikit-learn 9 | matplotlib -------------------------------------------------------------------------------- /env/pyvenv.cfg: -------------------------------------------------------------------------------- 1 | home = C:\Users\kk\AppData\Local\Programs\Python\Python312 2 | include-system-site-packages = false 3 | version = 3.12.10 4 | executable = C:\Users\kk\AppData\Local\Programs\Python\Python312\python.exe 5 | command = C:\Users\kk\AppData\Local\Programs\Python\Python312\python.exe -m venv D:\0LSR\AI courses\env 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | Include 10 | Lib 11 | Scripts 12 | node_modules 13 | dist 14 | dist-ssr 15 | *.local 16 | 17 | # Editor directories and files 18 | .vscode/* 19 | !.vscode/extensions.json 20 | .idea 21 | .DS_Store 22 | *.suo 23 | *.ntvs* 24 | *.njsproj 25 | *.sln 26 | *.sw? -------------------------------------------------------------------------------- /3_Decision_Trees/main.py: -------------------------------------------------------------------------------- 1 | from sklearn.datasets import load_iris 2 | from sklearn.tree import DecisionTreeClassifier 3 | iris = load_iris() 4 | X = iris.data[:, 2:] # petal length and width 5 | y = iris.target 6 | tree_clf = DecisionTreeClassifier(max_depth=2) 7 | tree_clf.fit(X, y) 8 | 9 | from sklearn.tree import export_graphviz 10 | export_graphviz( 11 | tree_clf, 12 | out_file=iris.image_path("iris_tree.dot"), 13 | feature_names=iris.feature_names[2:], 14 | class_names=iris.target_names, 15 | rounded=True, 16 | filled=True 17 | ) 18 | 19 | 20 | print(X, y) -------------------------------------------------------------------------------- /8_Rainforcement_Learning/main.py: -------------------------------------------------------------------------------- 1 | import gym 2 | import numpy as np 3 | import warnings 4 | 5 | # Suppress specific deprecation warnings 6 | warnings.filterwarnings("ignore", category=DeprecationWarning) 7 | 8 | # Load the environment with render mode specified 9 | env = gym.make('CartPole-v1', render_mode="human") 10 | 11 | # Initialize the environment to get the initial state 12 | state = env.reset() 13 | 14 | # Print the state space and action space 15 | print("State space:", env.observation_space) 16 | print("Action space:", env.action_space) 17 | 18 | # Run a few steps in the environment with random actions 19 | for _ in range(10): 20 | env.render() # Render the environment for visualization 21 | action = env.action_space.sample() # Take a random action 22 | 23 | # Take a step in the environment 24 | step_result = env.step(action) 25 | 26 | # Check the number of values returned and unpack accordingly 27 | if len(step_result) == 4: 28 | next_state, reward, done, info = step_result 29 | terminated = False 30 | else: 31 | next_state, reward, done, truncated, info = step_result 32 | terminated = done or truncated 33 | 34 | print(f"Action: {action}, Reward: {reward}, Next State: {next_state}, Done: {done}, Info: {info}") 35 | 36 | if terminated: 37 | state = env.reset() # Reset the environment if the episode is finished 38 | 39 | env.close() # Close the environment when done -------------------------------------------------------------------------------- /6_K_means/main.py: -------------------------------------------------------------------------------- 1 | # import library 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from sklearn.datasets import make_blobs 5 | 6 | X,y = make_blobs(n_samples = 500,n_features = 2,centers = 3,random_state = 23) 7 | 8 | fig = plt.figure(0) 9 | plt.grid(True) 10 | plt.scatter(X[:,0],X[:,1]) 11 | plt.show() 12 | 13 | k = 3 14 | clusters = {} 15 | np.random.seed(23) 16 | for i in range(k): 17 | clusters[i] = { 18 | 'center': 2*(2*np.random.random((X.shape[1],))-1), 19 | 'points': [] 20 | } 21 | 22 | plt.scatter(X[:,0],X[:,1]) 23 | plt.grid(True) 24 | for i in clusters: 25 | center = clusters[i]['center'] 26 | plt.scatter(center[0],center[1],marker = '*',c = 'red') 27 | plt.show() 28 | 29 | def distance(p1,p2): 30 | return np.sqrt(np.sum((p1-p2)**2)) 31 | 32 | def assign_clusters(X, clusters): 33 | for idx in range(X.shape[0]): 34 | dist = [] 35 | 36 | curr_x = X[idx] 37 | 38 | for i in range(k): 39 | dis = distance(curr_x,clusters[i]['center']) 40 | dist.append(dis) 41 | curr_cluster = np.argmin(dist) 42 | clusters[curr_cluster]['points'].append(curr_x) 43 | return clusters 44 | 45 | def update_clusters(X, clusters): 46 | for i in range(k): 47 | points = np.array(clusters[i]['points']) 48 | if points.shape[0] > 0: 49 | new_center = points.mean(axis =0) 50 | clusters[i]['center'] = new_center 51 | 52 | clusters[i]['points'] = [] 53 | return clusters 54 | 55 | def pred_cluster(X, clusters): 56 | pred = [] 57 | for i in range(X.shape[0]): 58 | dist = [] 59 | for j in range(k): 60 | dist.append(distance(X[i],clusters[j]['center'])) 61 | pred.append(np.argmin(dist)) 62 | return pred 63 | 64 | clusters = assign_clusters(X,clusters) 65 | clusters = update_clusters(X,clusters) 66 | pred = pred_cluster(X,clusters) 67 | 68 | plt.scatter(X[:,0],X[:,1],c = pred) 69 | for i in clusters: 70 | center = clusters[i]['center'] 71 | plt.scatter(center[0],center[1],marker = '^',c = 'red') 72 | plt.show() -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import tensorflow as tf 4 | from tensorflow import keras 5 | from tensorflow.keras import layers, losses 6 | from tensorflow.keras.models import Model 7 | from keras.datasets import mnist 8 | 9 | (x_train, _), (x_test, _) = mnist.load_data() 10 | 11 | x_train = x_train.astype('float32') / 255. 12 | x_test = x_test.astype('float32') / 255. 13 | 14 | x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) 15 | x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) 16 | 17 | 18 | class SimpleAutoencoder(Model): 19 | def __init__(self, latent_dimensions): 20 | super(SimpleAutoencoder, self).__init__() 21 | self.encoder = tf.keras.Sequential([ 22 | layers.Input(shape=(28, 28, 1)), 23 | layers.Flatten(), 24 | layers.Dense(latent_dimensions, activation='relu'), 25 | ]) 26 | 27 | self.decoder = tf.keras.Sequential([ 28 | layers.Dense(28 * 28, activation='sigmoid'), 29 | layers.Reshape((28, 28, 1)) 30 | ]) 31 | 32 | def call(self, input_data): 33 | encoded = self.encoder(input_data) 34 | decoded = self.decoder(encoded) 35 | return decoded 36 | 37 | latent_dimensions = 64 38 | autoencoder = SimpleAutoencoder(latent_dimensions) 39 | autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError()) 40 | 41 | autoencoder.fit(x_train, x_train, 42 | epochs=10, 43 | batch_size=256, 44 | shuffle=True, 45 | validation_data=(x_test, x_test)) 46 | 47 | encoded_imgs = autoencoder.encoder(x_test).numpy() 48 | decoded_imgs = autoencoder.decoder(encoded_imgs).numpy() 49 | 50 | n = 6 51 | plt.figure(figsize=(12, 6)) 52 | for i in range(n): 53 | ax = plt.subplot(2, n, i + 1) 54 | plt.imshow(x_test[i].reshape(28, 28), cmap='gray') 55 | plt.title("Original") 56 | plt.axis('off') 57 | 58 | ax = plt.subplot(2, n, i + 1 + n) 59 | plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray') 60 | plt.title("Reconstructed") 61 | plt.axis('off') 62 | 63 | plt.show() -------------------------------------------------------------------------------- /11_Autoencoders/main.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import tensorflow as tf 4 | from tensorflow import keras 5 | from tensorflow.keras import layers, losses 6 | from tensorflow.keras.models import Model 7 | from keras.datasets import mnist 8 | 9 | (x_train, _), (x_test, _) = mnist.load_data() 10 | 11 | x_train = x_train.astype('float32') / 255. 12 | x_test = x_test.astype('float32') / 255. 13 | 14 | x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) 15 | x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) 16 | 17 | 18 | class SimpleAutoencoder(Model): 19 | def __init__(self, latent_dimensions): 20 | super(SimpleAutoencoder, self).__init__() 21 | self.encoder = tf.keras.Sequential([ 22 | layers.Input(shape=(28, 28, 1)), 23 | layers.Flatten(), 24 | layers.Dense(latent_dimensions, activation='relu'), 25 | ]) 26 | 27 | self.decoder = tf.keras.Sequential([ 28 | layers.Dense(28 * 28, activation='sigmoid'), 29 | layers.Reshape((28, 28, 1)) 30 | ]) 31 | 32 | def call(self, input_data): 33 | encoded = self.encoder(input_data) 34 | decoded = self.decoder(encoded) 35 | return decoded 36 | 37 | latent_dimensions = 64 38 | autoencoder = SimpleAutoencoder(latent_dimensions) 39 | autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError()) 40 | 41 | autoencoder.fit(x_train, x_train, 42 | epochs=10, 43 | batch_size=256, 44 | shuffle=True, 45 | validation_data=(x_test, x_test)) 46 | 47 | encoded_imgs = autoencoder.encoder(x_test).numpy() 48 | decoded_imgs = autoencoder.decoder(encoded_imgs).numpy() 49 | 50 | n = 6 51 | plt.figure(figsize=(12, 6)) 52 | for i in range(n): 53 | ax = plt.subplot(2, n, i + 1) 54 | plt.imshow(x_test[i].reshape(28, 28), cmap='gray') 55 | plt.title("Original") 56 | plt.axis('off') 57 | 58 | ax = plt.subplot(2, n, i + 1 + n) 59 | plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray') 60 | plt.title("Reconstructed") 61 | plt.axis('off') 62 | 63 | plt.show() -------------------------------------------------------------------------------- /9_CNN_DeepLearning/main.py: -------------------------------------------------------------------------------- 1 | # import the necessary libraries 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pyplot as plt 5 | from itertools import product 6 | 7 | # set the param 8 | plt.rc('figure', autolayout=True) 9 | plt.rc('image', cmap='magma') 10 | 11 | # define the kernel 12 | kernel = tf.constant([[-1, -1, -1], 13 | [-1, 8, -1], 14 | [-1, -1, -1], 15 | ]) 16 | 17 | # load the image 18 | image = tf.io.read_file('Ganesh.jpg') 19 | image = tf.io.decode_jpeg(image, channels=1) 20 | image = tf.image.resize(image, size=[300, 300]) 21 | 22 | # plot the image 23 | img = tf.squeeze(image).numpy() 24 | plt.figure(figsize=(5, 5)) 25 | plt.imshow(img, cmap='gray') 26 | plt.axis('off') 27 | plt.title('Original Gray Scale image') 28 | plt.show() 29 | 30 | 31 | # Reformat 32 | image = tf.image.convert_image_dtype(image, dtype=tf.float32) 33 | image = tf.expand_dims(image, axis=0) 34 | kernel = tf.reshape(kernel, [*kernel.shape, 1, 1]) 35 | kernel = tf.cast(kernel, dtype=tf.float32) 36 | 37 | # convolution layer 38 | conv_fn = tf.nn.conv2d 39 | 40 | image_filter = conv_fn( 41 | input=image, 42 | filters=kernel, 43 | strides=1, # or (1, 1) 44 | padding='SAME', 45 | ) 46 | 47 | plt.figure(figsize=(15, 5)) 48 | 49 | # Plot the convolved image 50 | plt.subplot(1, 3, 1) 51 | 52 | plt.imshow( 53 | tf.squeeze(image_filter) 54 | ) 55 | plt.axis('off') 56 | plt.title('Convolution') 57 | 58 | # activation layer 59 | relu_fn = tf.nn.relu 60 | # Image detection 61 | image_detect = relu_fn(image_filter) 62 | 63 | plt.subplot(1, 3, 2) 64 | plt.imshow( 65 | # Reformat for plotting 66 | tf.squeeze(image_detect) 67 | ) 68 | 69 | plt.axis('off') 70 | plt.title('Activation') 71 | 72 | # Pooling layer 73 | pool = tf.nn.pool 74 | image_condense = pool(input=image_detect, 75 | window_shape=(2, 2), 76 | pooling_type='MAX', 77 | strides=(2, 2), 78 | padding='SAME', 79 | ) 80 | 81 | plt.subplot(1, 3, 3) 82 | plt.imshow(tf.squeeze(image_condense)) 83 | plt.axis('off') 84 | plt.title('Pooling') 85 | plt.show() -------------------------------------------------------------------------------- /2_Logistic_Regression/main.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | import matplotlib.pyplot as plt 4 | import seaborn as sns 5 | from sklearn.datasets import load_diabetes 6 | from sklearn.model_selection import train_test_split 7 | from sklearn.preprocessing import StandardScaler 8 | from sklearn.linear_model import LogisticRegression 9 | from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, auc 10 | 11 | # Load the diabetes dataset 12 | diabetes = load_diabetes() 13 | print(diabetes) 14 | X, y = diabetes.data, diabetes.target 15 | 16 | # Convert the target variable to binary (1 for diabetes, 0 for no diabetes) 17 | y_binary = (y > np.median(y)).astype(int) 18 | 19 | # Split the data into training and testing sets 20 | X_train, X_test, y_train, y_test = train_test_split( 21 | X, y_binary, test_size=0.2, random_state=42) 22 | 23 | # Standardize features 24 | scaler = StandardScaler() 25 | X_train = scaler.fit_transform(X_train) 26 | X_test = scaler.transform(X_test) 27 | 28 | # Train the Logistic Regression model 29 | model = LogisticRegression() 30 | model.fit(X_train, y_train) 31 | 32 | # Evaluate the model 33 | y_pred = model.predict(X_test) 34 | accuracy = accuracy_score(y_test, y_pred) 35 | print("Accuracy: {:.2f}%".format(accuracy * 100)) 36 | 37 | # evaluate the model 38 | print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred)) 39 | print("\nClassification Report:\n", classification_report(y_test, y_pred)) 40 | 41 | # Visualize the decision boundary with accuracy information 42 | plt.figure(figsize=(8, 6)) 43 | sns.scatterplot(x=X_test[:, 2], y=X_test[:, 8], hue=y_test, palette={ 44 | 0: 'blue', 1: 'red'}, marker='o') 45 | plt.xlabel("BMI") 46 | plt.ylabel("Age") 47 | plt.title("Logistic Regression Decision Boundary\nAccuracy: {:.2f}%".format( 48 | accuracy * 100)) 49 | plt.legend(title="Diabetes", loc="upper right") 50 | plt.show() 51 | 52 | # Plot ROC Curve 53 | y_prob = model.predict_proba(X_test)[:, 1] 54 | fpr, tpr, thresholds = roc_curve(y_test, y_prob) 55 | roc_auc = auc(fpr, tpr) 56 | 57 | plt.figure(figsize=(8, 6)) 58 | plt.plot(fpr, tpr, color='darkorange', lw=2, 59 | label=f'ROC Curve (AUC = {roc_auc:.2f})') 60 | plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--', label='Random') 61 | plt.xlabel('False Positive Rate') 62 | plt.ylabel('True Positive Rate') 63 | plt.title('Receiver Operating Characteristic (ROC) Curve\nAccuracy: {:.2f}%'.format( 64 | accuracy * 100)) 65 | plt.legend(loc="lower right") 66 | plt.show() -------------------------------------------------------------------------------- /1_Linear_Regression/model.py: -------------------------------------------------------------------------------- 1 | #@title Code - Define ML functions 2 | import keras 3 | import pandas as pd 4 | from plot import make_plots, model_info 5 | 6 | def build_model(my_learning_rate, num_features): 7 | """Create and compile a simple linear regression model.""" 8 | # Describe the topography of the model. 9 | # The topography of a simple linear regression model 10 | # is a single node in a single layer. 11 | inputs = keras.Input(shape=(num_features,)) 12 | outputs = keras.layers.Dense(units=1)(inputs) 13 | model = keras.Model(inputs=inputs, outputs=outputs) 14 | 15 | # Compile the model topography into code that Keras can efficiently 16 | # execute. Configure training to minimize the model's mean squared error. 17 | model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=my_learning_rate), 18 | loss="mean_squared_error", 19 | metrics=[keras.metrics.RootMeanSquaredError()]) 20 | 21 | return model 22 | 23 | 24 | def train_model(model, features, label, epochs, batch_size): 25 | """Train the model by feeding it data.""" 26 | 27 | # Feed the model the feature and the label. 28 | # The model will train for the specified number of epochs. 29 | history = model.fit(x=features, 30 | y=label, 31 | batch_size=batch_size, 32 | epochs=epochs) 33 | 34 | # Gather the trained model's weight and bias. 35 | trained_weight = model.get_weights()[0] 36 | trained_bias = model.get_weights()[1] 37 | 38 | # The list of epochs is stored separately from the rest of history. 39 | epochs = history.epoch 40 | 41 | # Isolate the error for each epoch. 42 | hist = pd.DataFrame(history.history) 43 | 44 | # To track the progression of training, we're going to take a snapshot 45 | # of the model's root mean squared error at each epoch. 46 | rmse = hist["root_mean_squared_error"] 47 | 48 | return trained_weight, trained_bias, epochs, rmse 49 | 50 | 51 | def run_experiment(df, feature_names, label_name, learning_rate, epochs, batch_size): 52 | 53 | print('INFO: starting training experiment with features={} and label={}\n'.format(feature_names, label_name)) 54 | 55 | num_features = len(feature_names) 56 | 57 | features = df.loc[:, feature_names].values 58 | label = df[label_name].values 59 | 60 | model = build_model(learning_rate, num_features) 61 | model_output = train_model(model, features, label, epochs, batch_size) 62 | 63 | print('\nSUCCESS: training experiment complete\n') 64 | print('{}'.format(model_info(feature_names, label_name, model_output))) 65 | make_plots(df, feature_names, label_name, model_output) 66 | 67 | return model 68 | 69 | print("SUCCESS: defining linear regression functions complete.") -------------------------------------------------------------------------------- /1_Linear_Regression/plot.py: -------------------------------------------------------------------------------- 1 | import plotly.exceptions as px 2 | import plotly.subplots as make_subplots 3 | #@title Define plotting functions 4 | 5 | def make_plots(df, feature_names, label_name, model_output, sample_size=200): 6 | 7 | random_sample = df.sample(n=sample_size).copy() 8 | random_sample.reset_index() 9 | weights, bias, epochs, rmse = model_output 10 | 11 | is_2d_plot = len(feature_names) == 1 12 | model_plot_type = "scatter" if is_2d_plot else "surface" 13 | fig = make_subplots(rows=1, cols=2, 14 | subplot_titles=("Loss Curve", "Model Plot"), 15 | specs=[[{"type": "scatter"}, {"type": model_plot_type}]]) 16 | 17 | plot_data(random_sample, feature_names, label_name, fig) 18 | plot_model(random_sample, feature_names, weights, bias, fig) 19 | plot_loss_curve(epochs, rmse, fig) 20 | 21 | fig.show() 22 | return 23 | 24 | def plot_loss_curve(epochs, rmse, fig): 25 | curve = px.line(x=epochs, y=rmse) 26 | curve.update_traces(line_color='#ff0000', line_width=3) 27 | 28 | fig.append_trace(curve.data[0], row=1, col=1) 29 | fig.update_xaxes(title_text="Epoch", row=1, col=1) 30 | fig.update_yaxes(title_text="Root Mean Squared Error", row=1, col=1, range=[rmse.min()*0.8, rmse.max()]) 31 | 32 | return 33 | 34 | def plot_data(df, features, label, fig): 35 | if len(features) == 1: 36 | scatter = px.scatter(df, x=features[0], y=label) 37 | else: 38 | scatter = px.scatter_3d(df, x=features[0], y=features[1], z=label) 39 | 40 | fig.append_trace(scatter.data[0], row=1, col=2) 41 | if len(features) == 1: 42 | fig.update_xaxes(title_text=features[0], row=1, col=2) 43 | fig.update_yaxes(title_text=label, row=1, col=2) 44 | else: 45 | fig.update_layout(scene1=dict(xaxis_title=features[0], yaxis_title=features[1], zaxis_title=label)) 46 | 47 | return 48 | 49 | def plot_model(df, features, weights, bias, fig): 50 | df['FARE_PREDICTED'] = bias[0] 51 | 52 | for index, feature in enumerate(features): 53 | df['FARE_PREDICTED'] = df['FARE_PREDICTED'] + weights[index][0] * df[feature] 54 | 55 | if len(features) == 1: 56 | model = px.line(df, x=features[0], y='FARE_PREDICTED') 57 | model.update_traces(line_color='#ff0000', line_width=3) 58 | else: 59 | z_name, y_name = "FARE_PREDICTED", features[1] 60 | z = [df[z_name].min(), (df[z_name].max() - df[z_name].min()) / 2, df[z_name].max()] 61 | y = [df[y_name].min(), (df[y_name].max() - df[y_name].min()) / 2, df[y_name].max()] 62 | x = [] 63 | for i in range(len(y)): 64 | x.append((z[i] - weights[1][0] * y[i] - bias[0]) / weights[0][0]) 65 | 66 | plane=pd.DataFrame({'x':x, 'y':y, 'z':[z] * 3}) 67 | 68 | light_yellow = [[0, '#89CFF0'], [1, '#FFDB58']] 69 | model = go.Figure(data=go.Surface(x=plane['x'], y=plane['y'], z=plane['z'], 70 | colorscale=light_yellow)) 71 | 72 | fig.add_trace(model.data[0], row=1, col=2) 73 | 74 | return 75 | 76 | def model_info(feature_names, label_name, model_output): 77 | weights = model_output[0] 78 | bias = model_output[1] 79 | 80 | nl = "\n" 81 | header = "-" * 80 82 | banner = header + nl + "|" + "MODEL INFO".center(78) + "|" + nl + header 83 | 84 | info = "" 85 | equation = label_name + " = " 86 | 87 | for index, feature in enumerate(feature_names): 88 | info = info + "Weight for feature[{}]: {:.3f}\n".format(feature, weights[index][0]) 89 | equation = equation + "{:.3f} * {} + ".format(weights[index][0], feature) 90 | 91 | info = info + "Bias: {:.3f}\n".format(bias[0]) 92 | equation = equation + "{:.3f}\n".format(bias[0]) 93 | 94 | return banner + nl + info + nl + equation 95 | 96 | print("SUCCESS: defining plotting functions complete.") -------------------------------------------------------------------------------- /env/share/man/man1/ttx.1: -------------------------------------------------------------------------------- 1 | .Dd May 18, 2004 2 | .\" ttx is not specific to any OS, but contrary to what groff_mdoc(7) 3 | .\" seems to imply, entirely omitting the .Os macro causes 'BSD' to 4 | .\" be used, so I give a zero-width space as its argument. 5 | .Os \& 6 | .\" The "FontTools Manual" argument apparently has no effect in 7 | .\" groff 1.18.1. I think it is a bug in the -mdoc groff package. 8 | .Dt TTX 1 "FontTools Manual" 9 | .Sh NAME 10 | .Nm ttx 11 | .Nd tool for manipulating TrueType and OpenType fonts 12 | .Sh SYNOPSIS 13 | .Nm 14 | .Bk 15 | .Op Ar option ... 16 | .Ek 17 | .Bk 18 | .Ar file ... 19 | .Ek 20 | .Sh DESCRIPTION 21 | .Nm 22 | is a tool for manipulating TrueType and OpenType fonts. It can convert 23 | TrueType and OpenType fonts to and from an 24 | .Tn XML Ns -based format called 25 | .Tn TTX . 26 | .Tn TTX 27 | files have a 28 | .Ql .ttx 29 | extension. 30 | .Pp 31 | For each 32 | .Ar file 33 | argument it is given, 34 | .Nm 35 | detects whether it is a 36 | .Ql .ttf , 37 | .Ql .otf 38 | or 39 | .Ql .ttx 40 | file and acts accordingly: if it is a 41 | .Ql .ttf 42 | or 43 | .Ql .otf 44 | file, it generates a 45 | .Ql .ttx 46 | file; if it is a 47 | .Ql .ttx 48 | file, it generates a 49 | .Ql .ttf 50 | or 51 | .Ql .otf 52 | file. 53 | .Pp 54 | By default, every output file is created in the same directory as the 55 | corresponding input file and with the same name except for the 56 | extension, which is substituted appropriately. 57 | .Nm 58 | never overwrites existing files; if necessary, it appends a suffix to 59 | the output file name before the extension, as in 60 | .Pa Arial#1.ttf . 61 | .Ss "General options" 62 | .Bl -tag -width ".Fl t Ar table" 63 | .It Fl h 64 | Display usage information. 65 | .It Fl d Ar dir 66 | Write the output files to directory 67 | .Ar dir 68 | instead of writing every output file to the same directory as the 69 | corresponding input file. 70 | .It Fl o Ar file 71 | Write the output to 72 | .Ar file 73 | instead of writing it to the same directory as the 74 | corresponding input file. 75 | .It Fl v 76 | Be verbose. Write more messages to the standard output describing what 77 | is being done. 78 | .It Fl a 79 | Allow virtual glyphs ID's on compile or decompile. 80 | .El 81 | .Ss "Dump options" 82 | The following options control the process of dumping font files 83 | (TrueType or OpenType) to 84 | .Tn TTX 85 | files. 86 | .Bl -tag -width ".Fl t Ar table" 87 | .It Fl l 88 | List table information. Instead of dumping the font to a 89 | .Tn TTX 90 | file, display minimal information about each table. 91 | .It Fl t Ar table 92 | Dump table 93 | .Ar table . 94 | This option may be given multiple times to dump several tables at 95 | once. When not specified, all tables are dumped. 96 | .It Fl x Ar table 97 | Exclude table 98 | .Ar table 99 | from the list of tables to dump. This option may be given multiple 100 | times to exclude several tables from the dump. The 101 | .Fl t 102 | and 103 | .Fl x 104 | options are mutually exclusive. 105 | .It Fl s 106 | Split tables. Dump each table to a separate 107 | .Tn TTX 108 | file and write (under the name that would have been used for the output 109 | file if the 110 | .Fl s 111 | option had not been given) one small 112 | .Tn TTX 113 | file containing references to the individual table dump files. This 114 | file can be used as input to 115 | .Nm 116 | as long as the referenced files can be found in the same directory. 117 | .It Fl i 118 | .\" XXX: I suppose OpenType programs (exist and) are also affected. 119 | Don't disassemble TrueType instructions. When this option is specified, 120 | all TrueType programs (glyph programs, the font program and the 121 | pre-program) are written to the 122 | .Tn TTX 123 | file as hexadecimal data instead of 124 | assembly. This saves some time and results in smaller 125 | .Tn TTX 126 | files. 127 | .It Fl y Ar n 128 | When decompiling a TrueType Collection (TTC) file, 129 | decompile font number 130 | .Ar n , 131 | starting from 0. 132 | .El 133 | .Ss "Compilation options" 134 | The following options control the process of compiling 135 | .Tn TTX 136 | files into font files (TrueType or OpenType): 137 | .Bl -tag -width ".Fl t Ar table" 138 | .It Fl m Ar fontfile 139 | Merge the input 140 | .Tn TTX 141 | file 142 | .Ar file 143 | with 144 | .Ar fontfile . 145 | No more than one 146 | .Ar file 147 | argument can be specified when this option is used. 148 | .It Fl b 149 | Don't recalculate glyph bounding boxes. Use the values in the 150 | .Tn TTX 151 | file as is. 152 | .El 153 | .Sh "THE TTX FILE FORMAT" 154 | You can find some information about the 155 | .Tn TTX 156 | file format in 157 | .Pa documentation.html . 158 | In particular, you will find in that file the list of tables understood by 159 | .Nm 160 | and the relations between TrueType GlyphIDs and the glyph names used in 161 | .Tn TTX 162 | files. 163 | .Sh EXAMPLES 164 | In the following examples, all files are read from and written to the 165 | current directory. Additionally, the name given for the output file 166 | assumes in every case that it did not exist before 167 | .Nm 168 | was invoked. 169 | .Pp 170 | Dump the TrueType font contained in 171 | .Pa FreeSans.ttf 172 | to 173 | .Pa FreeSans.ttx : 174 | .Pp 175 | .Dl ttx FreeSans.ttf 176 | .Pp 177 | Compile 178 | .Pa MyFont.ttx 179 | into a TrueType or OpenType font file: 180 | .Pp 181 | .Dl ttx MyFont.ttx 182 | .Pp 183 | List the tables in 184 | .Pa FreeSans.ttf 185 | along with some information: 186 | .Pp 187 | .Dl ttx -l FreeSans.ttf 188 | .Pp 189 | Dump the 190 | .Sq cmap 191 | table from 192 | .Pa FreeSans.ttf 193 | to 194 | .Pa FreeSans.ttx : 195 | .Pp 196 | .Dl ttx -t cmap FreeSans.ttf 197 | .Sh NOTES 198 | On MS\-Windows and MacOS, 199 | .Nm 200 | is available as a graphical application to which files can be dropped. 201 | .Sh SEE ALSO 202 | .Pa documentation.html 203 | .Pp 204 | .Xr fontforge 1 , 205 | .Xr ftinfo 1 , 206 | .Xr gfontview 1 , 207 | .Xr xmbdfed 1 , 208 | .Xr Font::TTF 3pm 209 | .Sh AUTHORS 210 | .Nm 211 | was written by 212 | .An -nosplit 213 | .An "Just van Rossum" Aq just@letterror.com . 214 | .Pp 215 | This manual page was written by 216 | .An "Florent Rougon" Aq f.rougon@free.fr 217 | for the Debian GNU/Linux system based on the existing FontTools 218 | documentation. It may be freely used, modified and distributed without 219 | restrictions. 220 | .\" For Emacs: 221 | .\" Local Variables: 222 | .\" fill-column: 72 223 | .\" sentence-end: "[.?!][]\"')}]*\\($\\| $\\| \\| \\)[ \n]*" 224 | .\" sentence-end-double-space: t 225 | .\" End: -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI courses for beginners 2 | 3 | ## 1. Linear Regression 4 | 5 | - [Linear regression](https://developers.google.com/machine-learning/crash-course/linear-regression) 6 | is a statistical technique used to find the relationship between variables. In an ML context, linear regression finds the relationship between features and a label. 7 | 8 | - [Loss](https://developers.google.com/machine-learning/crash-course/linear-regression/loss) 9 | 10 | - [Gradient Descent](https://developers.google.com/machine-learning/crash-course/linear-regression/gradient-descent) 11 | 12 | - [Hyperparameters](https://developers.google.com/machine-learning/crash-course/linear-regression/hyperparameters) 13 | 14 | ### Exercise 15 | 16 | https://developers.google.com/machine-learning/crash-course/linear-regression/programming-exercise 17 | 18 | https://colab.research.google.com/github/google/eng-edu/blob/main/ml/cc/exercises/linear_regression_taxi.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=linear_regression#scrollTo=6sjix7lXI7xT 19 | 20 | 21 | 22 | ## 2. Logistic Regression 23 | 24 | - This module introduces a new type of regression model called [logistic regression](https://developers.google.com/machine-learning/crash-course/logistic-regression) that is designed to predict the probability of a given outcome. 25 | 26 | 27 | 28 | ### Exercise 29 | 30 | https://www.geeksforgeeks.org/ml-logistic-regression-using-python/ 31 | 32 | ## 3. Classification 33 | 34 | - [Classification](https://developers.google.com/machine-learning/crash-course/classification) is the task of predicting which of a set of classes (categories) an example belongs to. In this module, you'll learn how to convert a logistic regression model that predicts a probability into a binary classification model that predicts one of two classes. You'll also learn how to choose and calculate appropriate metrics to evaluate the quality of a classification model's predictions. Finally, you'll get a brief introduction to multi-class classification problems, which are discussed in more depth later in the course. 35 | 36 | ## 4. Decision Trees 37 | 38 | - [Decision Trees](https://developers.google.com/machine-learning/crash-course/classification) are versatile Machine Learning algorithms that can per‐ 39 | form both classification and regression tasks, and even multioutput tasks 40 | 41 | ### Exercise 42 | 43 | Hands_On_Machine_Learning_with_Scikit_Learn_and_TensorFlow (191P) 44 | 45 | https://www.clc.hcmus.edu.vn/wp-content/uploads/2017/11/Hands_On_Machine_Learning_with_Scikit_Learn_and_TensorFlow.pdf 46 | https://www.kaggle.com/code/prashant111/decision-tree-classifier-tutorial 47 | https://www.w3schools.com/python/python_ml_decision_tree.asp 48 | 49 | ## 5. SVM(Support Vector Machine Algorithm) 50 | 51 | - [Support Vector Machine(SVM)](https://www.geeksforgeeks.org/support-vector-machine-algorithm/) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or cat vs. dog. 52 | 53 | ### Exercise 54 | 55 | https://www.geeksforgeeks.org/support-vector-machine-algorithm/ 56 | 57 | ## 6. K means Clustering 58 | 59 | - [K-Means Clustering](https://www.geeksforgeeks.org/k-means-clustering-introduction/) is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. 60 | 61 | 62 | 63 | ### Exercise 64 | 65 | https://www.geeksforgeeks.org/k-means-clustering-introduction/ 66 | 67 | ## 7. PCA(Principal Componenet Analysis) 68 | 69 | - [PCA](https://drlee.io/secrets-of-pca-a-comprehensive-guide-to-principal-component-analysis-with-python-and-colab-6f7f3142e721) is not just a mathematical technique; it’s a gateway to a more profound understanding of data, revealing patterns and relationships that might otherwise go unnoticed. 70 | 71 | ### Exercise 72 | 73 | https://drlee.io/secrets-of-pca-a-comprehensive-guide-to-principal-component-analysis-with-python-and-colab-6f7f3142e721 74 | 75 | ## 8. Rainforcement Learning 76 | 77 | [Reinforcement Learning (RL)](https://www.geeksforgeeks.org/machine-learning/what-is-reinforcement-learning/) is a branch of machine learning that focuses on how agents can learn to make decisions through trial and error to maximize cumulative rewards. 78 | 79 | 80 | 81 | ### Exercise 82 | 83 | https://www.geeksforgeeks.org/machine-learning/what-is-reinforcement-learning/ 84 | 85 | ## 9. CNN in Deep Learning 86 | 87 | [A convolutional neural network (CNN)](https://www.simplilearn.com/tutorials/deep-learning-tutorial/convolutional-neural-network) is a feed-forward neural network that is generally used to analyze visual images by processing data with grid-like topology. It’s also known as a ConvNet. A convolutional neural network is used to detect and classify objects in an image. 88 | 89 | 90 | 91 | ### Exercise 92 | 93 | https://www.simplilearn.com/tutorials/deep-learning-tutorial/convolutional-neural-network 94 | 95 | https://www.geeksforgeeks.org/machine-learning/introduction-convolution-neural-network/ 96 | 97 | ## 10. RNN in Deep Learning 98 | 99 | [Recurrent Neural Networks (RNN)](https://www.geeksforgeeks.org/introduction-to-recurrent-neural-network/) differ from regular neural networks in how they process information. While standard neural networks pass information in one direction i.e from input to output, RNNs feed information back into the network at each step. 100 | 101 | Imagine reading a sentence and you try to predict the next word, you don’t rely only on the current word but also remember the words that came before. RNNs work similarly by “remembering” past information and passing the output from one step as input to the next i.e it considers all the earlier words to choose the most likely next word. This memory of previous steps helps the network understand context and make better predictions. 102 | 103 | 104 | 105 | ### Exercise 106 | 107 | https://www.geeksforgeeks.org/introduction-to-recurrent-neural-network/ 108 | 109 | ## 11. Autoencoders in Machine Learning 110 | 111 | [Autoencoders](https://www.geeksforgeeks.org/machine-learning/auto-encoders/#) are a special type of neural networks that learn to compress data into a compact form and then reconstruct it to closely match the original input. 112 | 113 | They consist of an: 114 | 115 | - Encoder that captures important features by reducing dimensionality. 116 | 117 | - Decoder that rebuilds the data from this compressed representation. 118 | 119 | 120 | 121 | ### Exercise 122 | 123 | https://www.geeksforgeeks.org/machine-learning/auto-encoders/# 124 | -------------------------------------------------------------------------------- /1_Linear_Regression/main.py: -------------------------------------------------------------------------------- 1 | import io 2 | 3 | import pandas as pd 4 | import numpy as np 5 | 6 | import keras 7 | 8 | import plotly.express as px 9 | import plotly.subplots as make_subplots 10 | import plotly.graph_objects as go 11 | 12 | import seaborn as sns 13 | 14 | # from model import run_experiment 15 | 16 | data_link = "https://download.mlcc.google.com/mledu-datasets/chicago_taxi_train.csv" 17 | 18 | chicago_taxi_dataset = pd.read_csv(data_link) 19 | 20 | training_df = chicago_taxi_dataset[['TRIP_MILES', 'TRIP_SECONDS', 'FARE', 'COMPANY', 'PAYMENT_TYPE', 'TIP_RATE']] 21 | 22 | print('Read dataset completed successfully.') 23 | print('Total number of rows: {0}\n\n'.format(len(training_df.index))) 24 | training_df.head(200) 25 | 26 | print('Total number of rows: {0}\n\n'.format(len(training_df.index))) 27 | training_df.describe(include='all') 28 | 29 | max_fare = training_df['FARE'].max() 30 | print("What is the maximum fare? \t\t\t\tAnswer: ${fare:.2f}".format(fare = max_fare)) 31 | 32 | mean_distance = training_df['TRIP_MILES'].mean() 33 | print("What is the mean distance across all trips? \t\tAnswer: {mean:.4f} miles".format(mean = mean_distance)) 34 | 35 | # How many cab companies are in the dataset? 36 | num_unique_companies = training_df['COMPANY'].nunique() 37 | print("How many cab companies are in the dataset? \t\tAnswer: {number}".format(number = num_unique_companies)) 38 | 39 | # What is the most frequent payment type? 40 | most_freq_payment_type = training_df['PAYMENT_TYPE'].value_counts().idxmax() 41 | print("What is the most frequent payment type? \t\tAnswer: {type}".format(type = most_freq_payment_type)) 42 | 43 | # Are any features missing data? 44 | missing_values = training_df.isnull().sum().sum() 45 | print("Are any features missing data? \t\t\t\tAnswer:", "No" if missing_values == 0 else "Yes") 46 | 47 | training_df.corr(numeric_only = True) 48 | 49 | sns.pairplot(training_df, x_vars=["FARE", "TRIP_MILES", "TRIP_SECONDS"], y_vars=["FARE", "TRIP_MILES", "TRIP_SECONDS"]) 50 | 51 | def make_plots(df, feature_names, label_name, model_output, sample_size=200): 52 | 53 | random_sample = df.sample(n=sample_size).copy() 54 | random_sample.reset_index() 55 | weights, bias, epochs, rmse = model_output 56 | 57 | is_2d_plot = len(feature_names) == 1 58 | model_plot_type = "scatter" if is_2d_plot else "surface" 59 | fig = make_subplots(rows=1, cols=2, 60 | subplot_titles=("Loss Curve", "Model Plot"), 61 | specs=[[{"type": "scatter"}, {"type": model_plot_type}]]) 62 | 63 | plot_data(random_sample, feature_names, label_name, fig) 64 | plot_model(random_sample, feature_names, weights, bias, fig) 65 | plot_loss_curve(epochs, rmse, fig) 66 | 67 | fig.show() 68 | return 69 | 70 | def plot_loss_curve(epochs, rmse, fig): 71 | curve = px.line(x=epochs, y=rmse) 72 | curve.update_traces(line_color='#ff0000', line_width=3) 73 | 74 | fig.append_trace(curve.data[0], row=1, col=1) 75 | fig.update_xaxes(title_text="Epoch", row=1, col=1) 76 | fig.update_yaxes(title_text="Root Mean Squared Error", row=1, col=1, range=[rmse.min()*0.8, rmse.max()]) 77 | 78 | return 79 | 80 | def plot_data(df, features, label, fig): 81 | if len(features) == 1: 82 | scatter = px.scatter(df, x=features[0], y=label) 83 | else: 84 | scatter = px.scatter_3d(df, x=features[0], y=features[1], z=label) 85 | 86 | fig.append_trace(scatter.data[0], row=1, col=2) 87 | if len(features) == 1: 88 | fig.update_xaxes(title_text=features[0], row=1, col=2) 89 | fig.update_yaxes(title_text=label, row=1, col=2) 90 | else: 91 | fig.update_layout(scene1=dict(xaxis_title=features[0], yaxis_title=features[1], zaxis_title=label)) 92 | 93 | return 94 | 95 | def plot_model(df, features, weights, bias, fig): 96 | df['FARE_PREDICTED'] = bias[0] 97 | 98 | for index, feature in enumerate(features): 99 | df['FARE_PREDICTED'] = df['FARE_PREDICTED'] + weights[index][0] * df[feature] 100 | 101 | if len(features) == 1: 102 | model = px.line(df, x=features[0], y='FARE_PREDICTED') 103 | model.update_traces(line_color='#ff0000', line_width=3) 104 | else: 105 | z_name, y_name = "FARE_PREDICTED", features[1] 106 | z = [df[z_name].min(), (df[z_name].max() - df[z_name].min()) / 2, df[z_name].max()] 107 | y = [df[y_name].min(), (df[y_name].max() - df[y_name].min()) / 2, df[y_name].max()] 108 | x = [] 109 | for i in range(len(y)): 110 | x.append((z[i] - weights[1][0] * y[i] - bias[0]) / weights[0][0]) 111 | 112 | plane=pd.DataFrame({'x':x, 'y':y, 'z':[z] * 3}) 113 | 114 | light_yellow = [[0, '#89CFF0'], [1, '#FFDB58']] 115 | model = go.Figure(data=go.Surface(x=plane['x'], y=plane['y'], z=plane['z'], 116 | colorscale=light_yellow)) 117 | 118 | fig.add_trace(model.data[0], row=1, col=2) 119 | 120 | return 121 | 122 | def model_info(feature_names, label_name, model_output): 123 | weights = model_output[0] 124 | bias = model_output[1] 125 | 126 | nl = "\n" 127 | header = "-" * 80 128 | banner = header + nl + "|" + "MODEL INFO".center(78) + "|" + nl + header 129 | 130 | info = "" 131 | equation = label_name + " = " 132 | 133 | for index, feature in enumerate(feature_names): 134 | info = info + "Weight for feature[{}]: {:.3f}\n".format(feature, weights[index][0]) 135 | equation = equation + "{:.3f} * {} + ".format(weights[index][0], feature) 136 | 137 | info = info + "Bias: {:.3f}\n".format(bias[0]) 138 | equation = equation + "{:.3f}\n".format(bias[0]) 139 | 140 | return banner + nl + info + nl + equation 141 | 142 | print("SUCCESS: defining plotting functions complete.") 143 | 144 | def build_model(my_learning_rate, num_features): 145 | """Create and compile a simple linear regression model.""" 146 | # Describe the topography of the model. 147 | # The topography of a simple linear regression model 148 | # is a single node in a single layer. 149 | inputs = keras.Input(shape=(num_features,)) 150 | outputs = keras.layers.Dense(units=1)(inputs) 151 | model = keras.Model(inputs=inputs, outputs=outputs) 152 | 153 | # Compile the model topography into code that Keras can efficiently 154 | # execute. Configure training to minimize the model's mean squared error. 155 | model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=my_learning_rate), 156 | loss="mean_squared_error", 157 | metrics=[keras.metrics.RootMeanSquaredError()]) 158 | 159 | return model 160 | 161 | 162 | def train_model(model, features, label, epochs, batch_size): 163 | """Train the model by feeding it data.""" 164 | 165 | # Feed the model the feature and the label. 166 | # The model will train for the specified number of epochs. 167 | history = model.fit(x=features, 168 | y=label, 169 | batch_size=batch_size, 170 | epochs=epochs) 171 | 172 | # Gather the trained model's weight and bias. 173 | trained_weight = model.get_weights()[0] 174 | trained_bias = model.get_weights()[1] 175 | 176 | # The list of epochs is stored separately from the rest of history. 177 | epochs = history.epoch 178 | 179 | # Isolate the error for each epoch. 180 | hist = pd.DataFrame(history.history) 181 | 182 | # To track the progression of training, we're going to take a snapshot 183 | # of the model's root mean squared error at each epoch. 184 | rmse = hist["root_mean_squared_error"] 185 | 186 | return trained_weight, trained_bias, epochs, rmse 187 | 188 | 189 | def run_experiment(df, feature_names, label_name, learning_rate, epochs, batch_size): 190 | 191 | print('INFO: starting training experiment with features={} and label={}\n'.format(feature_names, label_name)) 192 | 193 | num_features = len(feature_names) 194 | 195 | features = df.loc[:, feature_names].values 196 | label = df[label_name].values 197 | 198 | model = build_model(learning_rate, num_features) 199 | model_output = train_model(model, features, label, epochs, batch_size) 200 | 201 | print('\nSUCCESS: training experiment complete\n') 202 | print('{}'.format(model_info(feature_names, label_name, model_output))) 203 | # make_plots(df, feature_names, label_name, model_output) 204 | 205 | return model 206 | 207 | print("SUCCESS: defining linear regression functions complete.") 208 | 209 | # Experiment 210 | learning_rate = 0.001 211 | epochs = 20 212 | batch_size = 50 213 | 214 | features = ['TRIP_MILES'] 215 | label = 'FARE' 216 | 217 | model_1 = run_experiment(training_df, features, label, learning_rate, epochs, batch_size) 218 | 219 | 220 | learning_rate = 0.001 221 | epochs = 20 222 | batch_size = 50 223 | 224 | training_df.loc[:, 'TRIP_MINUTES'] = training_df['TRIP_SECONDS']/60 225 | 226 | features = ['TRIP_MILES', 'TRIP_MINUTES'] 227 | label = 'FARE' 228 | 229 | model_2 = run_experiment(training_df, features, label, learning_rate, epochs, batch_size) 230 | 231 | 232 | 233 | #@title Code - Define functions to make predictions 234 | def format_currency(x): 235 | return "${:.2f}".format(x) 236 | 237 | def build_batch(df, batch_size): 238 | batch = df.sample(n=batch_size).copy() 239 | batch.set_index(np.arange(batch_size), inplace=True) 240 | return batch 241 | 242 | def predict_fare(model, df, features, label, batch_size=50): 243 | batch = build_batch(df, batch_size) 244 | predicted_values = model.predict_on_batch(x=batch.loc[:, features].values) 245 | 246 | data = {"PREDICTED_FARE": [], "OBSERVED_FARE": [], "L1_LOSS": [], 247 | features[0]: [], features[1]: []} 248 | for i in range(batch_size): 249 | predicted = predicted_values[i][0] 250 | observed = batch.at[i, label] 251 | data["PREDICTED_FARE"].append(format_currency(predicted)) 252 | data["OBSERVED_FARE"].append(format_currency(observed)) 253 | data["L1_LOSS"].append(format_currency(abs(observed - predicted))) 254 | data[features[0]].append(batch.at[i, features[0]]) 255 | data[features[1]].append("{:.2f}".format(batch.at[i, features[1]])) 256 | 257 | output_df = pd.DataFrame(data) 258 | return output_df 259 | 260 | def show_predictions(output): 261 | header = "-" * 80 262 | banner = header + "\n" + "|" + "PREDICTIONS".center(78) + "|" + "\n" + header 263 | print(banner) 264 | print(output) 265 | return 266 | 267 | 268 | output = predict_fare(model_2, training_df, features, label) 269 | show_predictions(output) --------------------------------------------------------------------------------