0% found this document useful (0 votes)
36 views5 pages

Wa0002.

This document contains code for analyzing a linear regression model. It includes: 1. Plotting sample data and fitting a linear regression model. 2. Using the model to make predictions on new data and plotting the results. 3. Converting the data to higher order polynomials and refitting the models. 4. Loading iris flower data, splitting into train and test, and evaluating classification models including logistic regression and reporting accuracy.

Uploaded by

sir Bryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Wa0002.

This document contains code for analyzing a linear regression model. It includes: 1. Plotting sample data and fitting a linear regression model. 2. Using the model to make predictions on new data and plotting the results. 3. Converting the data to higher order polynomials and refitting the models. 4. Loading iris flower data, splitting into train and test, and evaluating classification models including logistic regression and reporting accuracy.

Uploaded by

sir Bryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

# -*- coding: utf-8 -*-

"""Untitled5.ipynb

Automatically generated by Colaboratory.

Original file is located at


https://colab.research.google.com/drive/1P3KHjkaBoBiwK2K023s5f6Z45CrUDoWy

# PAVAN KUMAR REDDYMASU


#11525000
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split, cross_val_score
#from statistics import meanimport pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split, cross_val_score
from statistics import mean

X=np.arange(9).reshape(9,1)
Y=np.array([2.5,4.2,2,1,4.4,0.9,1.7,5.1,12])

"""#1.1 plot the data"""

plt.title("Matplotlib plot")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(X,Y)
plt.show()

"""#1.2 Train linear regressor y=ax+b"""

model = LinearRegression().fit(X, Y)
r_sq = model.score(X, Y)
print('coefficient of determination:', r_sq)

"""#1.3 Plot the coefficient and plot the model curve from 0 to 10 at
x=np.arrange(0,10,0.01)

"""

plt.scatter(X,Y,c='steelblue', edgecolor='white', s=70)


plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('scatter plot of x vs y')
plt.show()

# Print the Intercept:


print('intercept:', model.intercept_)
# Print the Slope:
print('slope:', model.coef_)
# Predict a Response and print it:
y_pred = model.predict(X)
print('Predicted response:', y_pred, sep='\n')
"""#1.4 Use the model to predict x_new and plot x_new and its corresponding
predictions. keep the training data scatter as background in the same plot.

"""

x_new=np.arange(0,8,0.01).reshape(800,1)

y_new = model.predict(x_new)
print (y_new)

"""#1.5 Use the normal equation to solve the model paramters. Print the solution.

# Convert target variable array from 1d to 2d.


"""

x_new = np.array([np.ones(len(X)), X.flatten()]).T

x_new

"""# Using Normal Equation.

"""

theta_best_values = np.linalg.inv(x_new.T.dot(x_new)).dot(x_new.T).dot(Y)

# Display best values obtained.


print ("Display best values obtained")
print(theta_best_values)
# Plot the output.
plt.scatter(X,Y,s=30,marker='o')
plt.plot(X,y_pred,c='red')
plt.plot()
plt.xlabel("Feature")
plt.ylabel("Target_Variable")
plt.title('Linear Regression')
plt.show()

"""#1.6 Generate new data matrix with higher polynomial orders.

"""

from sklearn.preprocessing import PolynomialFeatures

#poly order2
poly = PolynomialFeatures(degree = 2)
X_poly = poly.fit_transform(X)
poly.fit(X_poly, Y)
lin2 = LinearRegression(fit_intercept=True)
lin2.fit(X_poly, Y)

w_0 = lin2.intercept_
w_1 = lin2.coef_[1]
w_2 = lin2.coef_[2]

print (lin2.intercept_)
print (lin2.coef_)

x_new=np.arange(0,8,0.01).reshape(800,1)
x_new_poly=poly.fit_transform(x_new)
prediction = lin2.predict(x_new_poly)

plt.scatter(x_new, prediction)
plt.show()

#poly order8
poly = PolynomialFeatures(degree = 8)

X_poly = poly.fit_transform(X)
lin = LinearRegression()
poly.fit(X_poly, Y)
lin3 = LinearRegression(fit_intercept=True)

lin3.fit(X_poly, Y)
w_0 = lin3.intercept_
w_1 = lin3.coef_[1]
w_2 = lin3.coef_[2]

print(lin3.intercept_)
print(lin3.coef_)

x_new=np.arange(0,8,0.01).reshape(800,1)
x_new_poly=poly.fit_transform(x_new)
prediction = lin3.predict(x_new_poly)

plt.scatter(x_new, prediction)
plt.show()

"""#2.1 Load iris data

"""

from sklearn.datasets import make_moons as mm


from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
iris= load_iris()
type(iris)
print (iris.data)
iris.data.shape
print (iris.feature_names)
from sklearn.datasets import make_moons as mm

X, y = mm(n_samples=200, noise=0.1, random_state=0)


plt.scatter(X[:,0], X[:,1], c=y)
plt.show()

"""#2.2 Split the data and report test accuracy

"""

# Split the data


X_train, X_test, Y_train, Y_test = train_test_split(iris['data'], iris['target'],
random_state=0)

# Prepare the classifier


from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
# Fit the model
logreg.fit(X_train, Y_train)

logreg = LogisticRegression(penalty='l2', C=1.0)


logreg.fit(X_train, Y_train)
y_pred = logreg.predict(X_test)

print("Accuracy:", accuracy_score(Y_test, y_pred))

"""#2.3 Report L2 regularized test accuracy


# Evaluate the model
"""

print("Training scores: {:.2f}".format(logreg.score(X_train, Y_train)))


print("Test scores: {:.2f}".format(logreg.score(X_test,Y_test)))

print ("\n\n\n\n\n")

def find_theta(X, y):

m = X.shape[0] # Number of training examples.


# Appending a cloumn of ones in X to add the bias term.
X = np.append(X, np.ones((m,1)), axis=1)
# reshaping y to (m,1)
y = y.reshape(m,1)

# The Normal Equation


theta = np.dot(np.linalg.inv(np.dot(X.T, X)), np.dot(X.T, y))

return theta
def predict(X):

# Appending a cloumn of ones in X to add the bias term.


X = np.append(X, np.ones((X.shape[0],1)), axis=1)

# preds is y_hat which is the dot product of X and theta.


preds = np.dot(X, theta)

return preds

"""# Getting the Value of theta using the find_theta function.

"""

theta = find_theta(X, Y)
print ("The best values presented is as shown below: ", theta)

"""# Getting the predictions on X using the predict function.

"""

preds = predict(X)
# Plotting the predictions.
fig = plt.figure(figsize=(8,6))
plt.plot(X, Y, 'b.')
plt.plot(X, preds, 'c-')
plt.xlabel('X - Input')
plt.ylabel('y - target / true')
plt.show()

"""#2.4 Convert data to polynomial order 8. Report L2 regularized test accuracy and
Report none regularized test accuracy

"""

logreg = LogisticRegression(C = .01).fit(X_train, Y_train)


# Evaluate the model
print("Training scores: {:.2f}".format(logreg.score(X_train, Y_train)))
print("Test scores: {:.2f}".format(logreg.score(X_test,Y_test)))

You might also like