当前位置:网站首页>Machine learning Assignment 1

Machine learning Assignment 1

2022-07-19 12:21:00 starmultiple

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

# 1
# Plot a scatter plot of the given data 
data = pd.read_csv('ex1data1.txt', header=None, names=['Population', 'Profit'])
data.head()

data.describe()
data.plot(kind='scatter', x='Population', y='Profit', figsize=(12,8))
plt.show()



 Insert picture description here

# Processing data 
#  Add a new column ,x0
data.insert(0, 'Ones', 1)
data.head()
""" Ones Population Profit 0 1 6.1101 17.5920 1 1 5.5277 9.1302 2 1 8.5186 13.6620 3 1 7.0032 11.8540 4 1 5.8598 6.8233"""
cols = data.shape[1]
X = data.iloc[:, 0:cols-1]
Y = data.iloc[:, cols-1:cols]

X = np.matrix(X.values)
Y = np.matrix(Y.values)
theta = np.matrix(np.array([0, 0]))
#  Cost function 
def computeCost(X, Y, theta):
    inner = np.power((X * theta.T) - Y, 2)
    return np.sum(inner) / (2 * len(X))
# Calculate the best straight line fitting by solving the normal equation 
def normalEqn(X, Y):
    theta = np.linalg.inv(X.[email protected])@X.[email protected]
    return theta

theta = normalEqn(X, Y)
#  Gradient down theta by  matrix([[-3.24140214, 1.1272942 ]])
theta

computeCost(X, Y, theta.reshape(1, -1))

#  Draw a fitted image 
x = np.linspace(data.Population.min(), data.Population.max(), 100)
f = theta[0,0] + theta[1,0] * x

plt.figure(figsize=(12, 8))
plt.xlabel('Population')
plt.ylabel('Profit')
l1 = plt.plot(x, f, label='Prediction', color='red')
# Draw a straight line above 
l2 = plt.scatter(data.Population, data.Profit, label='Traing Data', )
# Draw scattered dots on it 
plt.legend(loc='best')
plt.title('Predicted Profit vs Population Size')
plt.show()

 Insert picture description here

原网站

版权声明
本文为[starmultiple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171612403175.html