<jupyter_start><jupyter_text>Coding 1-D linear regression<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

# load data
X = []
Y = []

with open('data_1d.csv', 'r') as f:
    lines = f.readlines()
    
for line in lines:
    x, y = line.split(',')
    X.append(float(x))
    Y.append(float(y))

X = np.array(X)
Y = np.array(Y)

plt.scatter(X, Y)
plt.title('data 1d')
plt.show()

# apply the equations
denominator = np.mean(np.square(X)) - np.square(np.mean(X))

a = (np.mean(X * Y) - np.mean(X) * np.mean(Y)) / denominator
b = (np.mean(np.square(X)) * np.mean(Y) - np.mean(X) * np.mean(X * Y)) / denominator

Yhat = a * X + b
plt.scatter(X, Y)
plt.plot(X, Yhat, color='r')
plt.title('data predict')
plt.show()

d1 = Y - Yhat
d2 = Y - Y.mean()
r_square = 1 - d1.dot(d1) / d2.dot(d2)
print('r_square = ', r_square)<jupyter_output><empty_output><jupyter_text>Section 2, Lecture 8: Moore law<jupyter_code>import re
import numpy as np
import matplotlib.pyplot as plt

# load data
X = []
Y = []

non_decimal = re.compile(r'[^\d]+')
with open('moore.csv', 'r') as f:
    lines = f.readlines()
    
for line in lines:
    r = line.split('\t')
    x = int(non_decimal.sub('', r[2].split('[')[0]))
    y = int(non_decimal.sub('', r[1].split('[')[0]))
    X.append(float(x))
    Y.append(float(y))

X = np.array(X)
Y = np.array(Y)

plt.scatter(X, Y)
plt.title('raw data')
plt.show()

Y = np.log(Y)
# apply the equations
denominator = X.dot(X) - X.mean() * X.sum()

a = (X.dot(Y) - X.mean() * Y.sum()) / denominator
b = (Y.mean() * X.dot(X) - X.mean() * X.dot(Y)) / denominator

Yhat = a * X + b
plt.scatter(X, Y)
plt.plot(X, Yhat, color='r')
plt.title('data predict')
plt.show()

d1 = Y - Yhat
d2 = Y - Y.mean()
r_square = 1 - d1.dot(d1) / d2.dot(d2)
print('r_square = ', r_square)

# mathematic stuffs
# log(tc) = a*year + b
# tc = exp(a*year) * exp(b)
# 2*tc = 2*exp(a*year) * exp(b)
# 2*tc = exp(ln(2)) * exp(a*year) * exp(b) = exp(b) * exp(a*year + ln(2))
# exp(a*year2) * exp(b) = exp(b) * exp(a*year + ln(2))
# a*year2 = a*year + ln(2)
# year2 = year + ln(2)/a

print('time to double: ', np.log(2)/a)<jupyter_output><empty_output><jupyter_text>Section 3, Lecture 16: Coding data 2d<jupyter_code>import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

#load data
X = []
Y = []

with open('data_2d.csv', 'r') as f:
    lines = f.readlines()
for line in lines:
    x1, x2, y = line.split(',')
    X.append([float(x1), float(x2), 1])
    Y.append(float(y))
    
# turn to np array
X = np.array(X)
Y = np.array(Y)

# plot data
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# ax.scatter(X[:, 0], X[:, 1], Y)
# plt.show()

# finding w with np solve
w = np.linalg.solve(X.T.dot(X), X.T.dot(Y))

# calc r_square
yhat = X.dot(w)
d1 = Y - yhat
d2 = Y - Y.mean()

r_square = 1 - d1.dot(d1) / d2.dot(d2)
print('r_square = ', r_square)<jupyter_output>r_square =  0.9980040612475777<jupyter_text>Section 3, Lecture 18: Predict Systolic Blood Pressure<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd


# blood, year, weight
df = pd.read_excel('mlr02.xls')
df['ones'] = 1

Y = df[['X1']]
X = df[['X2', 'X3', 'ones']]
X2_only = df[['X2', 'ones']]
X3_only = df[['X3', 'ones']]

# plot 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(np.array(X2_only)[:,0], np.array(X3_only)[:,0], np.array(Y))
plt.show()

def find_r2_square(X, Y):
    X = np.array(X)
    Y = np.array(Y)
    
    w = np.linalg.solve(X.T.dot(X), X.T.dot(Y))
    
    yhat = X.dot(w)
    d1 = (Y - yhat).squeeze()
    d2 = (Y - Y.mean()).squeeze()
    r_square = 1 - d1.dot(d1) / d2.dot(d2)
    return r_square

print('only use X2, r_square = ', find_r2_square(X2_only, Y))
print('only use X3, r_square = ', find_r2_square(X3_only, Y))
print('use both, r_square = ', find_r2_square(X, Y))<jupyter_output>*** No CODEPAGE record, no encoding_override: will use 'ascii'
{'fraction_non_alphanumeric': 0.20371765887967846, 'fraction_numerical': 0.028887214267771917, 'mean_word_length': 3.7124260355029586, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '196745', 'license': 'MIT', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1809, 'n_words': 396, 'path': '02.LinearRegression/loingo_linear_regression_class.ipynb', 'repo_name': 'tuanthi/Machine-Learning-Course', 'size': '3981'}