<jupyter_start><jupyter_text>**Machine Learning Assignment Number 2**   **<NAME>**<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Plot the data<jupyter_code>data = np.loadtxt('data.txt')
positive_data= data[0:50,:2]
negative_data= data[-50:,:2]

def plot_data():
  plt.plot(positive_data[:,0], positive_data[:,1], 'bo', markerfacecolor='none', label='+ive')
  plt.plot(negative_data[:,0], negative_data[:,1], 'ro', markerfacecolor='none', label='--ive')
  
#plot initial data
plot_data()
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Initialize parameters<jupyter_code>initial_theta = np.random.uniform(-0.01,0.01,3)
print('initial theta is ', initial_theta)
learning_rate = 0.01
x1 = data[:,0]
x2 = data[:,1]
y =  data[:,2]
#Prediction function

def predicted_y(th, x_corr):
  return (-1 / th[2]) * (th[0] + th[1] * x_corr)
#updaing theta using gradient descend
def gradient_desc(th, itr_count, lr):
  for itr in np.arange(itr_count):
    hx = th[0] + x1 * th[1] + th[2] * x2
    gx = 1 / (1 + np.exp(-hx))
    y_diff = np.array(y - gx).reshape(1, len(y))
    temp = np.array([[1] * len(y), x1, x2]).T
    temp1 = np.dot(y_diff, temp).reshape(3)  
    th =  th + lr * temp1
  return th
theta = gradient_desc(initial_theta, 5000, learning_rate)
print('theta is ', theta)
y_pred = predicted_y(theta, x1)
y_pred_initial=predicted_y(initial_theta, x1)
plt.figure(figsize=(12, 6))
plot_data()
plt.plot(data[:,0], y_pred_initial, 'grey', label='Initial model')
plt.plot(data[:,0], y_pred, 'g', label='Final model')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1818734793187348, 'fraction_numerical': 0.0340632603406326, 'mean_word_length': 5.854166666666667, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '394939', 'license': 'MIT', 'n_tokens_mistral': 688, 'n_tokens_neox': 667, 'n_words': 131, 'path': 'Results/Jupyter/ML/ML_SoSe19/ML_SoSe19-master/ML_ex2.ipynb', 'repo_name': 'in2dblue/interactive-rl', 'size': '1655'}