当前位置:网站首页>机器学习作业1
机器学习作业1
2022-07-17 16:13:00 【starmultiple】
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# 1
#所给数据绘制散点图
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()

#处理数据
# 新增一列,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]))
# 代价函数
def computeCost(X, Y, theta):
inner = np.power((X * theta.T) - Y, 2)
return np.sum(inner) / (2 * len(X))
#通过求解正规方程来计算最佳直线拟合
def normalEqn(X, Y):
theta = np.linalg.inv(X.[email protected])@X.[email protected]
return theta
theta = normalEqn(X, Y)
# 梯度下降的theta为 matrix([[-3.24140214, 1.1272942 ]])
theta
computeCost(X, Y, theta.reshape(1, -1))
# 画出拟合图像
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')
#上画直线
l2 = plt.scatter(data.Population, data.Profit, label='Traing Data', )
#上画散点
plt.legend(loc='best')
plt.title('Predicted Profit vs Population Size')
plt.show()

边栏推荐
- C语言绘图示例-调色板
- 人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
- Nintendo patent shows that the follow-up products of fitness ring accessories may be under development
- 对Rapidly-exploring Random Trees(RRT)路径规划方法的理解
- Conversion between Swift binary data and hexadecimal string
- PPPoE dial up
- 李宏毅《机器学习》|1. Introduction of this course(机器学习介绍)
- Detailed explanation of SQL blind annotation
- Mysql-1366 - Incorrect string value: ‘\xE5\xBC\xA0\xE4\xB8\x89‘ for column ‘userName‘ at row 1
- 文件一键备份
猜你喜欢

Example of C language painting - progress bar

Mysql-1366 - Incorrect string value: ‘\xE5\xBC\xA0\xE4\xB8\x89‘ for column ‘userName‘ at row 1

LeetCode_17_电话号码的字母组合

编辑技巧篇

C语言绘图示例-商标徽标
![[embedded unit test] construction of C language unit test framework](/img/d4/ba7e5ffa1a782c2478d1f6682f2637.png)
[embedded unit test] construction of C language unit test framework

Familiar with nestjs (beginner)

RAID 磁盘阵列详解,RAID分类及优缺点

Two misunderstandings of digital transformation

01 knapsack interview questions series (I)
随机推荐
HCIP (7)
Microcomputer principle and technical interface experiment five basic IO operation temperature control experiment
Example of C language drawing - 20 examples of chromatic diagram
阿荷投资的思考
psd. JS parsing PSD file
Gradient button function button drawing C language example
Research and implementation of 5g network Slicing Based on AI intelligent correlation technology
【机器学习】多标签分类的评价指标与代码实现
PPPoE dial up
3. Golang string type
MapGIS IGServer九州-驾驭国产化环境下的拓展服务开发
Linux下MySQL的安装与使用
Baidu document translation API
[original] magisk+shamiko has been tested by app root
zabbix-snmp监控
Research on downlink spectrum efficiency of 6G space earth integrated network high altitude platform base station
Leetcode 239. 滑动窗口最大值
STL string input / output overload
使用原生js实现按钮的全选功能,简单清晰
Matlab (4) functions and files