<jupyter_start><jupyter_text>Solution Assignment 1 (actually assignment 2)  Problem 1 I will consider the following color code RED=1, and BLACK=0<jupyter_code>red_points=[(-3,-1.5),(0,3),(1,-2.5),(4,-3)]
black_points=[(-4,1.5),(-2,-3),(-1,4),(2,-1.5),(3,3)]

def my_l0(x0,atupple):
    if atupple[0] > x0:
        return 1
    elif atupple[0]== x0:
        if atupple[1] >= 0:
            return 1
        else:
            return 0
    else:
        return 0
    
def my_precrecall(x0,red_list,black_list):
    tp=tn=fp=fn=0
    for item in red_list:
        if my_l0(x0,item) == 1:
            tp+=1
        else:
            fn+=1
    for item in black_list:
        if my_l0(x0,item) == 1:
            fp+=1
        else:
            tn+=1
    return tp/(tp+fp), tp/(tp+fn)

result=my_precrecall(-3,red_points,black_points)
print(f"Precision={result[0]} and Recall {result[1]}")<jupyter_output>Precision=0.42857142857142855 and Recall 0.75<jupyter_text>> Note that if we change RED=0 and BLACK=1, these numbers will change. So from the classification perspective, it matters what is considered to be POSITIVE or NEGATIVE.  Problem 2<jupyter_code>import pandas as pd
mydata=pd.read_csv("qsar.txt",sep=";")
mydata.head()
from sklearn.model_selection import train_test_split
train_set, test_set= train_test_split(mydata,test_size=0.2,random_state=6201)
train_variables=train_set.drop("LC50",axis=1)
train_lc50=train_set["LC50"].copy()
test_variables=test_set.drop("LC50",axis=1)
test_lc50=test_set["LC50"].copy()
whole_lc50=mydata["LC50"].copy()
whole_variables=mydata.drop("LC50",axis=1)<jupyter_output><empty_output><jupyter_text>Linear regression part<jupyter_code>from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
lin_reg=LinearRegression()
lin_reg.fit(train_variables,train_lc50)
train_pred_lc50=lin_reg.predict(train_variables)
train_mse=mean_squared_error(train_lc50,train_pred_lc50)
print(f"Training MSE: {train_mse}")
test_pred_lc50=lin_reg.predict(test_variables)
test_mse=mean_squared_error(test_lc50,test_pred_lc50)
print(f"Test MSE: {test_mse}")<jupyter_output>Training MSE: 0.868830821830307
Test MSE: 1.0084995398408565<jupyter_text>k-fold cross validation> Note that there was a typo on the homework. $K=1$ doesn't make much sense for cross-validation.<jupyter_code>from sklearn.model_selection import cross_val_score
for k in range(2,11):
    scores=cross_val_score(lin_reg, whole_variables,whole_lc50,scoring="neg_mean_squared_error",cv=k)
    linreg_rmse_scores=-scores
    print(f"{k}-fold cross validation MSE {linreg_rmse_scores.mean()}")<jupyter_output>2-fold cross validation MSE 0.9637266602520833
3-fold cross validation MSE 0.944404283336901
4-fold cross validation MSE 0.9436847810422524
5-fold cross validation MSE 0.9264965008104132
6-fold cross validation MSE 0.9281844540837957
7-fold cross validation MSE 0.9301037153056384
8-fold cross validation MSE 0.9244517705532415
9-fold cross validation MSE 0.9237379391027998
10-fold cross validation MSE 0.9214773680960923<jupyter_text>Ridge regression part<jupyter_code>from sklearn import linear_model
ridge_reg = linear_model.Ridge(alpha=.5)
ridge_reg.fit(train_variables,train_lc50)
train_pred_ridge_lc50=ridge_reg.predict(train_variables)
train_mse=mean_squared_error(train_lc50,train_pred_ridge_lc50)
print(f"Ridge Regression alpha=0.5 Training MSE: {train_mse}")
test_pred_ridge_lc50=ridge_reg.predict(test_variables)
test_mse=mean_squared_error(test_lc50,test_pred_ridge_lc50)
print(f"Ridge Regression alpha=0.5 Test MSE: {test_mse}")
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
ridge_model = linear_model.Ridge()
distributions = dict(alpha=uniform(loc=0, scale=5))
clf = RandomizedSearchCV(ridge_model, distributions, random_state=6201)
search = clf.fit(train_variables, train_lc50)
search.best_params_
best_ridge=linear_model.Ridge(alpha=search.best_params_['alpha'])
best_ridge.fit(train_variables,train_lc50)
best_ridge_test_pred_lc50=best_ridge.predict(test_variables)
best_ridge_test_mse=mean_squared_error(test_lc50,best_ridge_test_pred_lc50)
print(f"Ridge Regression best alpha={search.best_params_['alpha']} Test MSE= {best_ridge_test_mse}")<jupyter_output>Ridge Regression best alpha=2.3535513255328793 Test MSE= 1.0054760941608767<jupyter_text>LASSO regression<jupyter_code>from sklearn.linear_model import Lasso
lasso_reg = Lasso(alpha=.5)
lasso_reg.fit(train_variables,train_lc50)
train_pred_lasso_lc50=lasso_reg.predict(train_variables)
train_mse=mean_squared_error(train_lc50,train_pred_lasso_lc50)
print(f"Lasso Regression alpha=0.5 Training MSE: {train_mse}")
test_pred_lasso_lc50=lasso_reg.predict(test_variables)
test_mse=mean_squared_error(test_lc50,test_pred_lasso_lc50)
print(f"Lasso Regression alpha=0.5 Test MSE: {test_mse}")<jupyter_output>Lasso Regression alpha=0.5 Training MSE: 1.3523666536838914
Lasso Regression alpha=0.5 Test MSE: 1.2495332361002516<jupyter_text>Looking for an optimal $\lambda$ via Cross-validation<jupyter_code>lasso_model = Lasso()
distributions = dict(alpha=uniform(loc=0, scale=5))
clf = RandomizedSearchCV(lasso_model, distributions, random_state=6201)
lasso_search = clf.fit(train_variables, train_lc50)
lasso_search.best_params_
best_lasso=Lasso(alpha=lasso_search.best_params_['alpha'])
best_lasso.fit(train_variables,train_lc50)
best_lasso_test_pred_lc50=best_lasso.predict(test_variables)
best_lasso_test_mse=mean_squared_error(test_lc50,best_lasso_test_pred_lc50)
print(f"Lasso Regression best alpha={lasso_search.best_params_['alpha']} Test MSE= {best_lasso_test_mse}")<jupyter_output>Lasso Regression best alpha=0.12089738915014081 Test MSE= 1.0710111859717448<jupyter_text>> Note that when you change the range on the distribution of parameters $\lambda$ (or $\alpha$) will change. Thus, the instability of this parameter needs to be carefully addressed in your reporting.<jupyter_code>best_lasso.coef_
colnames=mydata.columns.values[:-1]
model_text=""
for i in range(len(colnames)):
    model_text+=repr(best_lasso.coef_[i])+"*"+colnames[i]+"\n\t"
print(f"LC50 = {model_text}")<jupyter_output>LC50 = 0.0*CIC0
	0.3839978978246306*SM1_Dz
	-0.0*GATS1i
	0.07044173684624118*NdsCH
	0.06973470044493849*NdssC
	0.5935042841315463*MLOGP<jupyter_text>Problem 4. Let's call the new data. We need to clean up a bit this since the first two rows are not to be processed. Also, we need to get rid of the first element of the rows.<jupyter_code>import numpy as np
wh_raw_data = open('weight_data.txt', 'r')
Lines = wh_raw_data.readlines()
 
count = 0
weight=[]
height=[]
for line in Lines:
    if count<=2 :
        count+=1
        continue
    else:
        line1=line.split("\n")[0]
        line2=line1.split("\t")
        weight.append(line2[2])
        height.append(line2[1])
        count+=1
wh_raw_data.close()
np_weight=np.array(weight).astype(float)
np_height=np.array(height).astype(float)
np_ones=np.ones(len(np_weight))
xt_matrix=np.array([np_ones,np_weight])
xt_matrix.shape
xt_matrix
x_matrix=xt_matrix.transpose()
x_matrix.shape
from numpy.linalg import inv

first_part=inv(np.matmul(xt_matrix,x_matrix))
first_part<jupyter_output><empty_output><jupyter_text>The first part represents $(X^t X)^{-1}$.<jupyter_code>second_part=np.matmul(first_part,xt_matrix)
second_part.shape<jupyter_output><empty_output><jupyter_text>The second part represents $(X^t X)^{-1} X^t$.<jupyter_code>y=np_height
theta_matrix=np.matmul(second_part,y)
theta_matrix<jupyter_output><empty_output><jupyter_text>The solution $\hat{\theta}= (X^t X)^{-1} X^t Y$. See below for the values obtained by regular regression. The first argument is $\theta_0$ and the second $\theta_1$.<jupyter_code>from sklearn.linear_model import LinearRegression
lin_reg=LinearRegression()
lin_reg.fit(np_weight.reshape(-1,1),np_height.reshape(-1,1))
lin_reg.intercept_, lin_reg.coef_<jupyter_output><empty_output><jupyter_text>Problem 5<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np



fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Make data.
X = np.arange(-0.15, 1.15, 0.05)
Y = np.arange(-0.15, 1.15, 0.05)
X, Y = np.meshgrid(X, Y)
Z = 4*X*Y-2*X-2*Y+1

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11522113727742676, 'fraction_numerical': 0.07340608845491096, 'mean_word_length': 6.745551601423488, 'pattern_counts': {'":': 1, '<': 47, '<?xml version=': 0, '>': 51, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '541256', 'license': 'Apache-2.0', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3305, 'n_words': 598, 'path': 'Test1/Assignment2.ipynb', 'repo_name': 'horaciogacevedo/Test_binder', 'size': '8705'}