diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..b6fe378 Binary files /dev/null and b/.DS_Store differ diff --git a/hw1/T1_P2.py b/hw1/T1_P2.py deleted file mode 100644 index eb65caf..0000000 --- a/hw1/T1_P2.py +++ /dev/null @@ -1,97 +0,0 @@ -##################### -# CS 181, Spring 2021 -# Homework 1, Problem 2 -# Start Code -################## - -import math -import matplotlib.cm as cm - -from math import exp -import numpy as np -import pandas as pd -import matplotlib.pyplot as plt -import matplotlib.colors as c - -# Read from file and extract X and y -df = pd.read_csv('data/p2.csv') - -X_df = df[['x1', 'x2']] -y_df = df['y'] - -X = X_df.values -y = y_df.values - -print("y is:") -print(y) - -def predict_kernel(alpha=0.1): - """Returns predictions using kernel-based predictor with the specified alpha.""" - # TODO: your code here - return y_df - -def predict_knn(k=1): - """Returns predictions using KNN predictor with the specified k.""" - # TODO: your code here - return y_df - -def plot_kernel_preds(alpha): - title = 'Kernel Predictions with alpha = ' + str(alpha) - plt.figure() - plt.title(title) - plt.xlabel('x1') - plt.ylabel('x2') - plt.xlim((0, 1)) - plt.ylim((0, 1)) - - plt.xticks(np.arange(0, 1, 0.1)) - plt.yticks(np.arange(0, 1, 0.1)) - y_pred = predict_kernel(alpha) - print(y_pred) - print('L2: ' + str(sum((y - y_pred) ** 2))) - norm = c.Normalize(vmin=0.,vmax=1.) - plt.scatter(df['x1'], df['x2'], c=y_pred, cmap='gray', vmin=0, vmax = 1, edgecolors='b') - for x_1, x_2, y_ in zip(df['x1'].values, df['x2'].values, y_pred): - plt.annotate(str(round(y_, 2)), - (x_1, x_2), - textcoords='offset points', - xytext=(0,5), - ha='center') - - # Saving the image to a file, and showing it as well - plt.savefig('alpha' + str(alpha) + '.png') - plt.show() - -def plot_knn_preds(k): - title = 'KNN Predictions with k = ' + str(k) - plt.figure() - plt.title(title) - plt.xlabel('x1') - plt.ylabel('x2') - plt.xlim((0, 1)) - plt.ylim((0, 1)) - - plt.xticks(np.arange(0, 1, 0.1)) - plt.yticks(np.arange(0, 1, 0.1)) - y_pred = predict_knn(k) - print(y_pred) - print('L2: ' + str(sum((y - y_pred) ** 2))) - norm = c.Normalize(vmin=0.,vmax=1.) - plt.scatter(df['x1'], df['x2'], c=y_pred, cmap='gray', vmin=0, vmax = 1, edgecolors='b') - for x_1, x_2, y_ in zip(df['x1'].values, df['x2'].values, y_pred): - plt.annotate(str(round(y_, 2)), - (x_1, x_2), - textcoords='offset points', - xytext=(0,5), - ha='center') - # Saving the image to a file, and showing it as well - plt.savefig('k' + str(k) + '.png') - plt.show() - -for alpha in (0.1, 3, 100): - # TODO: Print the loss for each chart. - plot_kernel_preds(alpha) - -for k in (1, 5, len(X)-1): - # TODO: Print the loss for each chart. - plot_knn_preds(k)