You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fitt/data.py

132 lines
4.1 KiB

# -*- encoding: utf-8 -*-
"""
@Author: packy945
@FileName: data.py
@DateTime: 2023/5/31 11:42
@SoftWare: PyCharm
"""
import tkinter as tk
from tkinter import *
import random
import numpy as np
11 months ago
FITT_SAVE = {}
FITT_LIST = []
X = [] # 等待拟合的x值
Y = [] # 等待拟合的y值
global Canvas2 # 用于显示函数的画布
Img1 = None # 绘制的dot.png图像
Img2 = None # 绘制的line.png图像
Xian_index = 0 # 当前选择的函数编号
Quadrant = 0 # 当前选择的象限信息0为四象限1为一象限
MAXV = 1000 # 最大值
11 months ago
INDEX = 5 # 拟合函数索引
Out = '' # 拟合输出信息
LOW = -MAXV # 坐标轴显示上界
HIGH = MAXV # 坐标轴显示下界
11 months ago
MAGNIDICATION = 1.0 # 放大倍数
def random_points():
x=[]
y=[]
for i in range(20):
x_= random.uniform(0, 1000)#生成随机浮点数
y_= random.uniform(0, 1000)#生成随机浮点数
x.append(x_) #加入列表
y.append(y_)
x=np.array(x) #列表转数组
y=np.array(y)
arr = np.array(list(zip(x, y)))# 将两个一维数组拼接成二维数组
return arr
def compute_curveData(num, step, coefficient):#coefficient代表二次函数系数数组形式
def quadratic_function(x):#构造二次函数y = a * X^2 + b * X + C
return coefficient[0] * x ** 2 + coefficient[1] * x + coefficient[2]
x_values = np.arange(-num, num, step)
y_values = quadratic_function(x_values)#调用quadratic_function(x)函数得到y值
curve_data = np.column_stack((x_values, y_values))#将两个一维数组堆叠成二维数组,形成(x,y)的形式
return curve_data
# #################################拟合优度R^2的计算######################################
def __sst(y_no_fitting):
"""
计算SST(total sum of squares) 总平方和
:param y_no_predicted: List[int] or array[int] 待拟合的y
:return: 总平方和SST
"""
y_mean = sum(y_no_fitting) / len(y_no_fitting)
s_list =[(y - y_mean)**2 for y in y_no_fitting]
sst = sum(s_list)
return sst
def __ssr(y_fitting, y_no_fitting):
"""
计算SSR(regression sum of squares) 回归平方和
:param y_fitting: List[int] or array[int] 拟合好的y值
:param y_no_fitting: List[int] or array[int] 待拟合y值
:return: 回归平方和SSR
"""
y_mean = sum(y_no_fitting) / len(y_no_fitting)
s_list =[(y - y_mean)**2 for y in y_fitting]
ssr = sum(s_list)
return ssr
def __sse(y_fitting, y_no_fitting):
"""
计算SSE(error sum of squares) 残差平方和
:param y_fitting: List[int] or array[int] 拟合好的y值
:param y_no_fitting: List[int] or array[int] 待拟合y值
:return: 残差平方和SSE
"""
s_list = [(y_fitting[i] - y_no_fitting[i])**2 for i in range(len(y_fitting))]
sse = sum(s_list)
return sse
def goodness_of_fit(y_fitting, y_no_fitting):
"""
计算拟合优度R^2
:param y_fitting: List[int] or array[int] 拟合好的y值
:param y_no_fitting: List[int] or array[int] 待拟合y值
:return: 拟合优度R^2
"""
ssr = __ssr(y_fitting, y_no_fitting)
sst = __sst(y_no_fitting)
rr = ssr /sst
return rr
def least_sqaure(x,y):
n = len(x)
sumx, sumy, sumxy, sumxx = 0, 0, 0, 0
for i in range(0, n):
sumx += x[i]
sumy += y[i]
sumxx += x[i] * x[i]
sumxy += x[i] * y[i]
a = (n * sumxy - sumx * sumy) / (n * sumxx - sumx * sumx)
b = (sumxx * sumy - sumx * sumxy) / (n * sumxx - sumx * sumx)
return a, b
import time
def gradient_descent(x, y):
s = time.time()
x = 10000 * np.random.randn(1) # 产生服从正太分布的一个数(均值=0方差=1)扩大10000倍
eta = 0.09
i = 1
while True:
y = x ^ 2 / 2 - 2 * x
x -= eta * 2 * x
if y <= 0.0001:
print('x is %.6f\ny is %.6f\nand steps are %d' % (x, y, i))
break
i += 1
e = time.time()
print('time is %.4f second' % (e - s))
if __name__ == '__main__':
random_points()
pass