Merge pull request '仓库合并' (#1) from master into main
@ -0,0 +1,30 @@
|
||||
-1000.000000 -303.919733
|
||||
-934.000000 -350.424580
|
||||
-868.000000 -193.369993
|
||||
-802.000000 -474.832293
|
||||
-736.000000 -101.068322
|
||||
-670.000000 -33.916365
|
||||
-604.000000 -164.504704
|
||||
-538.000000 -147.298119
|
||||
-472.000000 -124.814141
|
||||
-406.000000 -139.033018
|
||||
-340.000000 -69.265051
|
||||
-274.000000 -90.553084
|
||||
-208.000000 -114.709905
|
||||
-142.000000 -103.201058
|
||||
-76.000000 -108.245882
|
||||
-10.000000 56.710600
|
||||
56.000000 18.503031
|
||||
122.000000 185.633626
|
||||
188.000000 152.615324
|
||||
254.000000 47.216669
|
||||
320.000000 256.009421
|
||||
386.000000 270.790257
|
||||
452.000000 61.971014
|
||||
518.000000 121.635726
|
||||
584.000000 163.906089
|
||||
650.000000 166.204626
|
||||
716.000000 315.640621
|
||||
782.000000 425.649206
|
||||
848.000000 454.347899
|
||||
914.000000 289.943295
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 -2412.037897
|
||||
-934.000000 -1906.704565
|
||||
-868.000000 -1515.407287
|
||||
-802.000000 -1286.984468
|
||||
-736.000000 -991.348043
|
||||
-670.000000 -699.579002
|
||||
-604.000000 -834.074507
|
||||
-538.000000 -694.557977
|
||||
-472.000000 -503.237387
|
||||
-406.000000 -288.033015
|
||||
-340.000000 -248.449961
|
||||
-274.000000 -317.763223
|
||||
-208.000000 -254.540871
|
||||
-142.000000 -269.952677
|
||||
-76.000000 -252.274149
|
||||
-10.000000 -373.987676
|
||||
56.000000 -129.655207
|
||||
122.000000 -325.928443
|
||||
188.000000 -98.240616
|
||||
254.000000 -325.921235
|
||||
320.000000 -161.796895
|
||||
386.000000 96.648543
|
||||
452.000000 -245.636986
|
||||
518.000000 190.848311
|
||||
584.000000 225.026237
|
||||
650.000000 317.523583
|
||||
716.000000 772.606080
|
||||
782.000000 645.906241
|
||||
848.000000 1091.495759
|
||||
914.000000 1225.588183
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 1318.415137
|
||||
-934.000000 971.366429
|
||||
-868.000000 950.320306
|
||||
-802.000000 635.325663
|
||||
-736.000000 774.823634
|
||||
-670.000000 659.035975
|
||||
-604.000000 449.284057
|
||||
-538.000000 262.096521
|
||||
-472.000000 141.521962
|
||||
-406.000000 65.441983
|
||||
-340.000000 162.273141
|
||||
-274.000000 209.914028
|
||||
-208.000000 62.504373
|
||||
-142.000000 -160.439640
|
||||
-76.000000 -201.140961
|
||||
-10.000000 -178.764894
|
||||
56.000000 -194.056288
|
||||
122.000000 -221.386689
|
||||
188.000000 -236.245098
|
||||
254.000000 -317.086262
|
||||
320.000000 -385.718988
|
||||
386.000000 -198.736083
|
||||
452.000000 -256.246461
|
||||
518.000000 -226.361577
|
||||
584.000000 -329.214396
|
||||
650.000000 98.251218
|
||||
716.000000 -132.314476
|
||||
782.000000 62.741428
|
||||
848.000000 120.654860
|
||||
914.000000 356.397702
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 6580.374854
|
||||
-934.000000 5230.299130
|
||||
-868.000000 4064.603309
|
||||
-802.000000 2824.002023
|
||||
-736.000000 2151.187862
|
||||
-670.000000 1341.067791
|
||||
-604.000000 997.401541
|
||||
-538.000000 512.994167
|
||||
-472.000000 103.881466
|
||||
-406.000000 85.927912
|
||||
-340.000000 -256.482213
|
||||
-274.000000 -97.197686
|
||||
-208.000000 -264.665117
|
||||
-142.000000 -237.702195
|
||||
-76.000000 -151.105677
|
||||
-10.000000 -130.269796
|
||||
56.000000 -102.814456
|
||||
122.000000 -62.662181
|
||||
188.000000 -357.518806
|
||||
254.000000 -149.197016
|
||||
320.000000 -350.461131
|
||||
386.000000 -264.914106
|
||||
452.000000 -94.032078
|
||||
518.000000 -188.344844
|
||||
584.000000 -60.854287
|
||||
650.000000 85.277762
|
||||
716.000000 413.822510
|
||||
782.000000 695.033449
|
||||
848.000000 1169.069640
|
||||
914.000000 1762.153163
|
@ -0,0 +1,16 @@
|
||||
1.000000 -14.349565
|
||||
67.000000 321.611473
|
||||
133.000000 375.298867
|
||||
199.000000 386.511032
|
||||
265.000000 408.069269
|
||||
331.000000 423.359694
|
||||
397.000000 435.982565
|
||||
463.000000 458.199244
|
||||
529.000000 451.417416
|
||||
595.000000 471.376855
|
||||
661.000000 484.015127
|
||||
727.000000 489.640132
|
||||
793.000000 500.047206
|
||||
859.000000 499.698564
|
||||
925.000000 492.870266
|
||||
991.000000 509.779156
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 282.188733
|
||||
-934.000000 -121.080276
|
||||
-868.000000 89.565718
|
||||
-802.000000 -36.875112
|
||||
-736.000000 -2.179209
|
||||
-670.000000 -33.756961
|
||||
-604.000000 -199.536348
|
||||
-538.000000 -50.378169
|
||||
-472.000000 7.288867
|
||||
-406.000000 197.918480
|
||||
-340.000000 18.543165
|
||||
-274.000000 -80.038062
|
||||
-208.000000 -0.092604
|
||||
-142.000000 24.234519
|
||||
-76.000000 112.628196
|
||||
-10.000000 57.868872
|
||||
56.000000 -75.900181
|
||||
122.000000 53.411782
|
||||
188.000000 14.463053
|
||||
254.000000 -157.854773
|
||||
320.000000 3.419914
|
||||
386.000000 -95.442953
|
||||
452.000000 -511.818439
|
||||
518.000000 -785.633833
|
||||
584.000000 -1377.367723
|
||||
650.000000 -2606.119973
|
||||
716.000000 -5115.078474
|
||||
782.000000 -9770.399635
|
||||
848.000000 -19298.079697
|
||||
914.000000 -37316.681269
|
@ -0,0 +1,133 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def generate_dataset(func_type, coeff, n_samples=100, noise_level=0.0):
|
||||
"""
|
||||
Generate a dataset based on a mathematical function.
|
||||
|
||||
Parameters:
|
||||
func_type (str): Type of function ('linear', 'quadratic', 'cubic', 'quartic', 'exponential', 'logarithmic').
|
||||
coeff (list): Coefficients of the function.
|
||||
n_samples (int): Number of samples to generate.
|
||||
noise_level (float): Standard deviation of Gaussian noise added to the data.
|
||||
|
||||
Returns:
|
||||
np.ndarray: Generated dataset.
|
||||
"""
|
||||
# Generate x values
|
||||
x_values = np.linspace(-1000, 1000, n_samples)
|
||||
|
||||
# Define the functions
|
||||
functions = {
|
||||
'linear': lambda x: coeff[0] + coeff[1] * x,
|
||||
'quadratic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2,
|
||||
'cubic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3,
|
||||
'quartic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3 + coeff[4] * x**4,
|
||||
'exponential': lambda x: coeff[0] * np.exp(coeff[1] * x),
|
||||
'logarithmic': lambda x: coeff[0] + coeff[1] * np.log(np.abs(x) + 1) # Avoid log(0)
|
||||
}
|
||||
|
||||
# Generate y values based on the selected function
|
||||
y_values = functions[func_type](x_values)
|
||||
|
||||
# Add noise
|
||||
noise = np.random.normal(0, noise_level, n_samples)
|
||||
y_values_noisy = y_values + noise
|
||||
|
||||
# Combine x and y values
|
||||
dataset = np.vstack((x_values, y_values_noisy)).T
|
||||
|
||||
return dataset
|
||||
|
||||
|
||||
def plot_dataset(dataset, func_type, coeff):
|
||||
"""
|
||||
Plot the generated dataset.
|
||||
|
||||
Parameters:
|
||||
dataset (np.ndarray): Generated dataset.
|
||||
func_type (str): Type of function used to generate the dataset.
|
||||
coeff (list): Coefficients of the function.
|
||||
"""
|
||||
x_values = dataset[:, 0]
|
||||
y_values = dataset[:, 1]
|
||||
|
||||
# Plotting the dataset
|
||||
plt.figure(figsize=(10, 6))
|
||||
plt.scatter(x_values, y_values, color='blue', label='Generated Data')
|
||||
|
||||
# Plot the original function without noise for comparison
|
||||
if func_type == 'linear':
|
||||
y_original = coeff[0] + coeff[1] * x_values
|
||||
elif func_type == 'quadratic':
|
||||
y_original = coeff[0] + coeff[1] * x_values + coeff[2] * x_values**2
|
||||
elif func_type == 'cubic':
|
||||
y_original = coeff[0] + coeff[1] * x_values + coeff[2] * x_values**2 + coeff[3] * x_values**3
|
||||
elif func_type == 'quartic':
|
||||
y_original = coeff[0] + coeff[1] * x_values + coeff[2] * x_values**2 + coeff[3] * x_values**3 + coeff[4] * x_values**4
|
||||
elif func_type == 'exponential':
|
||||
y_original = coeff[0] * np.exp(coeff[1] * x_values)
|
||||
elif func_type == 'logarithmic':
|
||||
y_original = coeff[0] + coeff[1] * np.log(np.abs(x_values) + 1)
|
||||
|
||||
plt.plot(x_values, y_original, color='red', label='Original Function')
|
||||
|
||||
plt.title(f'{func_type.capitalize()} Function Dataset Visualization')
|
||||
plt.xlabel('X Values')
|
||||
plt.ylabel('Y Values')
|
||||
plt.legend()
|
||||
plt.grid(True)
|
||||
plt.show()
|
||||
|
||||
def generate_dataset2(func_type, coeff, x_range=(-1000, 1000), n_samples=100, noise_level=0.0):
|
||||
"""
|
||||
Generate a dataset based on a mathematical function with a specified x range.
|
||||
|
||||
Parameters:
|
||||
func_type (str): Type of function ('linear', 'quadratic', 'cubic', 'quartic', 'exponential', 'logarithmic').
|
||||
coeff (list): Coefficients of the function.
|
||||
x_range (tuple): The range of x values (min, max).
|
||||
n_samples (int): Number of samples to generate.
|
||||
noise_level (float): Standard deviation of Gaussian noise added to the data.
|
||||
|
||||
Returns:
|
||||
np.ndarray: Generated dataset.
|
||||
"""
|
||||
# Generate x values within the specified range
|
||||
x_values = np.linspace(x_range[0], x_range[1], n_samples)
|
||||
|
||||
# Define the functions
|
||||
functions = {
|
||||
'linear': lambda x: coeff[0] + coeff[1] * x,
|
||||
'quadratic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2,
|
||||
'cubic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3,
|
||||
'quartic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3 + coeff[4] * x**4,
|
||||
'exponential': lambda x: coeff[0] * np.exp(coeff[1] * x),
|
||||
'logarithmic': lambda x: coeff[0] + coeff[1] * np.log(np.abs(x) + 1) # Avoid log(0)
|
||||
}
|
||||
|
||||
# Generate y values based on the selected function
|
||||
y_values = functions[func_type](x_values)
|
||||
|
||||
# Clip y values to stay within the specified range
|
||||
y_values = np.clip(y_values, x_range[0], x_range[1])
|
||||
|
||||
# Add noise
|
||||
noise = np.random.normal(0, noise_level, n_samples)
|
||||
y_values_noisy = y_values + noise
|
||||
|
||||
# Clip noisy y values to stay within the specified range
|
||||
y_values_noisy = np.clip(y_values_noisy, x_range[0], x_range[1])
|
||||
|
||||
# Combine x and y values
|
||||
dataset = np.vstack((x_values, y_values_noisy)).T
|
||||
|
||||
return dataset
|
||||
|
||||
# Example usage
|
||||
dataset = generate_dataset('quadratic', [1, 2, 3], n_samples=10, noise_level=10)
|
||||
print(dataset)
|
||||
|
||||
# Visualize the dataset
|
||||
dataset = generate_dataset('quadratic', [1, 2, 3], n_samples=10, noise_level=10)
|
||||
plot_dataset(dataset, 'quadratic', [1, 2, 3])
|
@ -0,0 +1,83 @@
|
||||
import random
|
||||
import numpy as np
|
||||
import data as gl_data
|
||||
|
||||
|
||||
def linear_function(coefficient, x): # 构造一次函数y = a * X + b
|
||||
return coefficient[0] * x + coefficient[1]
|
||||
|
||||
|
||||
def quadratic_function(coefficient, x): # 构造二次函数y = a * X^2 + b * X + c
|
||||
return coefficient[0] * x ** 2 + coefficient[1] * x + coefficient[2]
|
||||
|
||||
|
||||
def qubic_function(coefficient, x): # 构造三次函数y = a* X^3 + b * X^2 + c * X + d
|
||||
return coefficient[0] * x ** 3 + coefficient[1] * x ** 2 + coefficient[2] * x + coefficient[3]
|
||||
|
||||
|
||||
def quartic_function(coefficient, x): # 构造四次函数
|
||||
return coefficient[0] * x ** 4 + coefficient[1] * x ** 3 + coefficient[2] * x ** 2 + coefficient[3] * x + \
|
||||
coefficient[4]
|
||||
|
||||
|
||||
# 编程4.1 -----------------------------------------
|
||||
def random_points(sampleNum, Low, High):
|
||||
sx, sy=[], []
|
||||
for i in range(sampleNum):
|
||||
sx.append(random.uniform(Low, High)) # 生成随机浮点数
|
||||
sy.append(random.uniform(Low, High))
|
||||
sx, sy = np.array(sx), np.array(sy) # 列表转数组
|
||||
gl_data.SampleData = np.array(list(zip(sx, sy))) # 拼接成二维数组并存入全局变量
|
||||
return gl_data.SampleData
|
||||
# if __name__ == '__main__':
|
||||
# sampleData = random_points(20, 0, 1000)
|
||||
# print(sampleData )
|
||||
# 编程4.1 END-----------------------------------------
|
||||
|
||||
|
||||
# 编程4.2 -----------------------------------------
|
||||
def compute_curveData(Low, High, step, coefficient, m): # coefficient代表二次函数系数
|
||||
x_values = np.arange(Low, High, step)
|
||||
if m == 1:
|
||||
y_values = linear_function(coefficient, x_values)
|
||||
if m == 2:
|
||||
y_values = quadratic_function(coefficient, x_values) # 调用quadratic_function(x)函数得到y值
|
||||
if m == 3:
|
||||
y_values = qubic_function(coefficient, x_values)
|
||||
if m == 4:
|
||||
y_values = quartic_function(coefficient, x_values)
|
||||
gl_data.CurveData = np.column_stack((x_values, y_values)) # 将x,y堆叠成二维数组
|
||||
return gl_data.CurveData
|
||||
# if __name__ == '__main__':
|
||||
# coefficient = [1, 2, 3]
|
||||
# curveData = compute_curveData(gl_data.LOW, gl_data.HIGH, 1, coefficient,2)
|
||||
# print(curveData)
|
||||
|
||||
|
||||
# 由于出现了数据稳定性的问题,因此在这里多了一个需要进行标准化处理的步骤
|
||||
def compute_curveData2(low, high, step, theta, m, x_mean, x_std):
|
||||
x_fit = np.arange(low, high, step)
|
||||
# 将x_fit标准化
|
||||
x_fit_normalized = (x_fit - x_mean) / x_std
|
||||
A_fit = np.vander(x_fit_normalized, m + 1, increasing=True)
|
||||
y_fit = A_fit @ theta
|
||||
gl_data.CurveData = np.column_stack((x_fit, y_fit))
|
||||
return gl_data.CurveData
|
||||
|
||||
|
||||
# 在X5中由于拓展了函数因此修改
|
||||
def compute_curveData_X5(func, popt):
|
||||
x_values = np.arange(gl_data.LOW, gl_data.HIGH, 1)
|
||||
y_values = func(x_values, *popt)
|
||||
gl_data.CurveData = np.column_stack((x_values, y_values))
|
||||
return gl_data.CurveData
|
||||
|
||||
# 进行标准化的X5函数
|
||||
def compute_curveData_X5_2(func, popt, sampleData):
|
||||
sx, sy = sampleData[:, 0], sampleData[:, 1]
|
||||
x_values = np.arange(gl_data.LOW, gl_data.HIGH, 1)
|
||||
x_values_normalized = (x_values - np.mean(sx)) / np.std(sx)
|
||||
y_values = func(x_values_normalized, *popt)
|
||||
gl_data.CurveData = np.column_stack((x_values, y_values))
|
||||
return gl_data.CurveData
|
||||
# 编程4.2 END-----------------------------------------
|
After Width: | Height: | Size: 403 B |
After Width: | Height: | Size: 403 B |
After Width: | Height: | Size: 4.9 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 61 KiB |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 125 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 567 B |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 695 B |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 808 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 1.3 KiB |
@ -0,0 +1,145 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
"""
|
||||
@Author: packy945
|
||||
@FileName: function.py
|
||||
@DateTime: 2023/6/6 14:45
|
||||
@SoftWare: PyCharm
|
||||
"""
|
||||
import numpy as np
|
||||
import math
|
||||
import data
|
||||
import pandas as pd
|
||||
from pandas import DataFrame
|
||||
# 函数库
|
||||
Show = []
|
||||
Fun = []
|
||||
Fit_type_library = {}
|
||||
|
||||
|
||||
class Function:
|
||||
def __init__(self, no, name, v, fun, complete='', demo=''):
|
||||
# 函数名称
|
||||
self.name = name
|
||||
# 变量个数
|
||||
self.variable = v
|
||||
self.function = fun # 函数表达式
|
||||
# 函数编号
|
||||
self.no = no
|
||||
# 是否显示
|
||||
self.show = 0
|
||||
# 完整函数
|
||||
self.complete = complete
|
||||
# self.print = ''
|
||||
self.demo = demo
|
||||
|
||||
def get_fun(self):
|
||||
if self.variable == 2:
|
||||
return self.fun2
|
||||
elif self.variable == 3: # 以二次函数为例,省略其他函数
|
||||
return self.fun3
|
||||
elif self.variable == 4:
|
||||
return self.fun4
|
||||
elif self.variable == 5:
|
||||
return self.fun5
|
||||
elif self.variable == 6:
|
||||
return self.fun6
|
||||
|
||||
def fun2(self, x, a0, a1):
|
||||
return eval(self.function)
|
||||
|
||||
def fun3(self, x, a0, a1, a2):
|
||||
return eval(self.function)
|
||||
|
||||
def fun4(self, x, a0, a1, a2, a3):
|
||||
return eval(self.function)
|
||||
|
||||
def fun5(self, x, a0, a1, a2, a3, a4):
|
||||
return eval(self.function)
|
||||
|
||||
def fun6(self, x, a0, a1, a2, a3, a4, a5):
|
||||
return eval(self.function)
|
||||
|
||||
def print_f(self, a):
|
||||
# print('self.show='+self.complete)
|
||||
exec('self.show='+self.complete)
|
||||
# print(self.show)
|
||||
return self.show
|
||||
|
||||
|
||||
# def f_init():
|
||||
# cur = 0 # 函数编号
|
||||
# # 一次函数
|
||||
# newfun = Function(cur, '一次函数', 2, 'a0*x+a1', "f'{a[0]}x+{a[1]}'", 'ax+b')
|
||||
# newfun.show = 1
|
||||
# Fun.append(newfun)
|
||||
# cur += 1
|
||||
# # 二次函数
|
||||
# newfun = Function(cur, '二次函数', 3, 'a0*x*x+a1*x+a2', "f'{a[0]}x^2+{a[1]}x+{a[2]}'", 'ax^2+bx+c')
|
||||
# newfun.show = 1
|
||||
# Fun.append(newfun)
|
||||
# cur += 1
|
||||
# # 三次函数
|
||||
# newfun = Function(cur, '三次函数', 4, 'a0*x*x*x+a1*x*x+a2*x+a3', "f'{a[0]}x^3+{a[1]}x^2+{a[2]}x+{a[3]}'", 'ax^3+bx^2+cx+d')
|
||||
# newfun.show = 0
|
||||
# Fun.append(newfun)
|
||||
# cur += 1
|
||||
# # 四次函数
|
||||
# newfun = Function(cur, '四次函数', 5, 'a0*x*x*x*x+a1*x*x*x+a2*x*x+a3*x+a4'
|
||||
# , "f'{a[0]}x^4+{a[1]}x^3+{a[2]}x^2+{a[3]}x+{a[4]}'", 'ax^4+bx^3+cx^2+dx+e')
|
||||
# newfun.show = 0
|
||||
# Fun.append(newfun)
|
||||
# cur += 1
|
||||
# # 指数函数
|
||||
# newfun = Function(cur, '指数函数', 2, 'a0*np.exp(0.01*x)+a1', "f'{a[0]}e^(0.01x)+{a[1]}'", 'a*e^(0.01x)+b')
|
||||
# newfun.show = 0
|
||||
# Fun.append(newfun)
|
||||
# cur += 1
|
||||
# # 对数函数
|
||||
# newfun = Function(cur, '对数函数', 3, 'a0*(np.log(x) + 1e-5) / (np.log(a1) + 1e-5) + a2',
|
||||
# "f'{a[0]}log(x,{a[1]})+{a[2]}'", 'alog(x,b)+c')
|
||||
# newfun.show = 0
|
||||
# Fun.append(newfun)
|
||||
# cur += 1
|
||||
# # 高斯函数
|
||||
|
||||
|
||||
# F_init()
|
||||
def write():
|
||||
file_path = 'functions.xlsx'#设置路径
|
||||
df = pd.read_excel(io=file_path, header=1)#设置表格
|
||||
print(df)
|
||||
df.columns = ['no', 'name', 'variable', 'function', 'complete', 'demo', 'show']#设置表格标题
|
||||
cur = 0#重置计数
|
||||
for f in Fun:#遍历函数类型
|
||||
df.loc[cur] = [cur, f.name, f.variable, f.function, f.complete, f.demo, f.show]#写进表格
|
||||
cur += 1#计数加一
|
||||
DataFrame(df).to_excel(file_path, sheet_name='Sheet1', index=False, header=True)#保存xlsx
|
||||
|
||||
|
||||
def f_read():
|
||||
file_path = 'functions.xlsx'#设置路径
|
||||
df = pd.read_excel(io=file_path, header=0)#设置表格
|
||||
cur = 0
|
||||
for i in range(len(df)):#逐行读取excel
|
||||
newfun = Function(df.loc[i][0], df.loc[i][1], df.loc[i][2], df.loc[i][3], df.loc[i][4], df.loc[i][5])
|
||||
newfun.show = df.loc[i][6]
|
||||
Show.append(df.loc[i][6])#读取函数显示信息
|
||||
Fun.append(newfun)#读取函数信息并添加
|
||||
cur += 1
|
||||
for f in Fun:#建立简表fit_type_library
|
||||
Fit_type_library[f.name] = [f.no, f.demo, f.name]
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
f_read()
|
||||
# for f in fun:
|
||||
# print([f.no, f.demo, f.name])
|
||||
for f in Fun:
|
||||
Fit_type_library[f.name] = [f.no, f.demo, f.name]
|
||||
|
||||
print("Fun",Fun)
|
||||
|
||||
print(Fit_type_library)
|
||||
a = [1,2,3,4,5]
|
||||
x = 2
|
||||
pass
|
@ -0,0 +1,89 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
"""
|
||||
@Author: packy945
|
||||
@FileName: input.py
|
||||
@DateTime: 2023/7/19 16:59
|
||||
@SoftWare: PyCharm
|
||||
"""
|
||||
import tkinter as tk
|
||||
from tkinter import *
|
||||
import numpy as np
|
||||
|
||||
import data as gl_data
|
||||
from X2 import selfdata_show
|
||||
|
||||
global top
|
||||
|
||||
|
||||
def input_num(root_tk):
|
||||
global top
|
||||
top = tk.Toplevel(root_tk)
|
||||
label1 = Label(top, text="坐标点个数")
|
||||
label1.grid(row=0) # 这里的side可以赋值为LEFT RTGHT TOP BOTTOM
|
||||
num1 = IntVar()
|
||||
entry1 = Entry(top, textvariable=num1)
|
||||
num1.set(0)
|
||||
entry1.grid(row=0, column=1)
|
||||
Label(top, text=" ").grid(row=0, column=3)
|
||||
Button(top, text="确定", command=lambda: input_data(root_tk, int(entry1.get()))).grid(row=0, column=3)
|
||||
top.mainloop()
|
||||
|
||||
def input_data(root_tk, num):
|
||||
global top
|
||||
sample_x = []
|
||||
sample_y = []
|
||||
sample_data = []
|
||||
top.destroy()
|
||||
top = tk.Toplevel(root_tk)
|
||||
|
||||
def add_sample_data():
|
||||
try:
|
||||
x = float(entry_x.get())
|
||||
y = float(entry_y.get())
|
||||
except:
|
||||
label_status.config(text="输入不合法")
|
||||
return
|
||||
entry_x.delete(0, tk.END)
|
||||
entry_y.delete(0, tk.END)
|
||||
if min(x, y) < gl_data.LOW or max(x, y) > gl_data.HIGH:
|
||||
label_status.config(text="输入超过范围")
|
||||
return
|
||||
elif len(sample_data) < num:
|
||||
label_status.config(text="点对已添加")
|
||||
sample_data.append((x, y))
|
||||
sample_x.append(x)
|
||||
sample_y.append(y)
|
||||
else:
|
||||
label_status.config(text="已达到最大数量")
|
||||
|
||||
def check_sample_data():
|
||||
if len(sample_data) == num:
|
||||
label_status.config(text="已达到最大数量")
|
||||
gl_data.X = np.array(sample_x)
|
||||
gl_data.Y = np.array(sample_y)
|
||||
print('已添加', sample_data)
|
||||
top.destroy()
|
||||
selfdata_show(gl_data.SampleData, gl_data.LOW, gl_data.HIGH)
|
||||
else:
|
||||
label_status.config(text="还需输入{}个点对".format(num - len(sample_data)))
|
||||
print(sample_data)
|
||||
|
||||
label_x = tk.Label(top, text="X 值:")
|
||||
label_x.pack()
|
||||
entry_x = tk.Entry(top)
|
||||
entry_x.pack()
|
||||
label_y = tk.Label(top, text="Y 值:")
|
||||
label_y.pack()
|
||||
entry_y = tk.Entry(top)
|
||||
entry_y.pack()
|
||||
button_add = tk.Button(top, text="添加", command=add_sample_data)
|
||||
button_add.pack()
|
||||
button_check = tk.Button(top, text="检查", command=check_sample_data)
|
||||
button_check.pack()
|
||||
label_status = tk.Label(top, text="")
|
||||
label_status.pack()
|
||||
top.mainloop()
|
||||
|
||||
if __name__ == '__main__':
|
||||
root = tk.Tk()
|
||||
input_num(root)
|
@ -0,0 +1,16 @@
|
||||
# This is a sample Python script.
|
||||
|
||||
# Press Shift+F10 to execute it or replace it with your code.
|
||||
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
|
||||
|
||||
|
||||
def print_hi(name):
|
||||
# Use a breakpoint in the code line below to debug your script.
|
||||
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
|
||||
|
||||
|
||||
# Press the green button in the gutter to run the script.
|
||||
if __name__ == '__main__':
|
||||
print_hi('PyCharm')
|
||||
|
||||
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|
After Width: | Height: | Size: 11 KiB |
@ -0,0 +1,30 @@
|
||||
-1000.000000 -303.919733
|
||||
-934.000000 -350.424580
|
||||
-868.000000 -193.369993
|
||||
-802.000000 -474.832293
|
||||
-736.000000 -101.068322
|
||||
-670.000000 -33.916365
|
||||
-604.000000 -164.504704
|
||||
-538.000000 -147.298119
|
||||
-472.000000 -124.814141
|
||||
-406.000000 -139.033018
|
||||
-340.000000 -69.265051
|
||||
-274.000000 -90.553084
|
||||
-208.000000 -114.709905
|
||||
-142.000000 -103.201058
|
||||
-76.000000 -108.245882
|
||||
-10.000000 56.710600
|
||||
56.000000 18.503031
|
||||
122.000000 185.633626
|
||||
188.000000 152.615324
|
||||
254.000000 47.216669
|
||||
320.000000 256.009421
|
||||
386.000000 270.790257
|
||||
452.000000 61.971014
|
||||
518.000000 121.635726
|
||||
584.000000 163.906089
|
||||
650.000000 166.204626
|
||||
716.000000 315.640621
|
||||
782.000000 425.649206
|
||||
848.000000 454.347899
|
||||
914.000000 289.943295
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 -2412.037897
|
||||
-934.000000 -1906.704565
|
||||
-868.000000 -1515.407287
|
||||
-802.000000 -1286.984468
|
||||
-736.000000 -991.348043
|
||||
-670.000000 -699.579002
|
||||
-604.000000 -834.074507
|
||||
-538.000000 -694.557977
|
||||
-472.000000 -503.237387
|
||||
-406.000000 -288.033015
|
||||
-340.000000 -248.449961
|
||||
-274.000000 -317.763223
|
||||
-208.000000 -254.540871
|
||||
-142.000000 -269.952677
|
||||
-76.000000 -252.274149
|
||||
-10.000000 -373.987676
|
||||
56.000000 -129.655207
|
||||
122.000000 -325.928443
|
||||
188.000000 -98.240616
|
||||
254.000000 -325.921235
|
||||
320.000000 -161.796895
|
||||
386.000000 96.648543
|
||||
452.000000 -245.636986
|
||||
518.000000 190.848311
|
||||
584.000000 225.026237
|
||||
650.000000 317.523583
|
||||
716.000000 772.606080
|
||||
782.000000 645.906241
|
||||
848.000000 1091.495759
|
||||
914.000000 1225.588183
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 1318.415137
|
||||
-934.000000 971.366429
|
||||
-868.000000 950.320306
|
||||
-802.000000 635.325663
|
||||
-736.000000 774.823634
|
||||
-670.000000 659.035975
|
||||
-604.000000 449.284057
|
||||
-538.000000 262.096521
|
||||
-472.000000 141.521962
|
||||
-406.000000 65.441983
|
||||
-340.000000 162.273141
|
||||
-274.000000 209.914028
|
||||
-208.000000 62.504373
|
||||
-142.000000 -160.439640
|
||||
-76.000000 -201.140961
|
||||
-10.000000 -178.764894
|
||||
56.000000 -194.056288
|
||||
122.000000 -221.386689
|
||||
188.000000 -236.245098
|
||||
254.000000 -317.086262
|
||||
320.000000 -385.718988
|
||||
386.000000 -198.736083
|
||||
452.000000 -256.246461
|
||||
518.000000 -226.361577
|
||||
584.000000 -329.214396
|
||||
650.000000 98.251218
|
||||
716.000000 -132.314476
|
||||
782.000000 62.741428
|
||||
848.000000 120.654860
|
||||
914.000000 356.397702
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 6580.374854
|
||||
-934.000000 5230.299130
|
||||
-868.000000 4064.603309
|
||||
-802.000000 2824.002023
|
||||
-736.000000 2151.187862
|
||||
-670.000000 1341.067791
|
||||
-604.000000 997.401541
|
||||
-538.000000 512.994167
|
||||
-472.000000 103.881466
|
||||
-406.000000 85.927912
|
||||
-340.000000 -256.482213
|
||||
-274.000000 -97.197686
|
||||
-208.000000 -264.665117
|
||||
-142.000000 -237.702195
|
||||
-76.000000 -151.105677
|
||||
-10.000000 -130.269796
|
||||
56.000000 -102.814456
|
||||
122.000000 -62.662181
|
||||
188.000000 -357.518806
|
||||
254.000000 -149.197016
|
||||
320.000000 -350.461131
|
||||
386.000000 -264.914106
|
||||
452.000000 -94.032078
|
||||
518.000000 -188.344844
|
||||
584.000000 -60.854287
|
||||
650.000000 85.277762
|
||||
716.000000 413.822510
|
||||
782.000000 695.033449
|
||||
848.000000 1169.069640
|
||||
914.000000 1762.153163
|
@ -0,0 +1,16 @@
|
||||
1.000000 -14.349565
|
||||
67.000000 321.611473
|
||||
133.000000 375.298867
|
||||
199.000000 386.511032
|
||||
265.000000 408.069269
|
||||
331.000000 423.359694
|
||||
397.000000 435.982565
|
||||
463.000000 458.199244
|
||||
529.000000 451.417416
|
||||
595.000000 471.376855
|
||||
661.000000 484.015127
|
||||
727.000000 489.640132
|
||||
793.000000 500.047206
|
||||
859.000000 499.698564
|
||||
925.000000 492.870266
|
||||
991.000000 509.779156
|
@ -0,0 +1,30 @@
|
||||
-1000.000000 282.188733
|
||||
-934.000000 -121.080276
|
||||
-868.000000 89.565718
|
||||
-802.000000 -36.875112
|
||||
-736.000000 -2.179209
|
||||
-670.000000 -33.756961
|
||||
-604.000000 -199.536348
|
||||
-538.000000 -50.378169
|
||||
-472.000000 7.288867
|
||||
-406.000000 197.918480
|
||||
-340.000000 18.543165
|
||||
-274.000000 -80.038062
|
||||
-208.000000 -0.092604
|
||||
-142.000000 24.234519
|
||||
-76.000000 112.628196
|
||||
-10.000000 57.868872
|
||||
56.000000 -75.900181
|
||||
122.000000 53.411782
|
||||
188.000000 14.463053
|
||||
254.000000 -157.854773
|
||||
320.000000 3.419914
|
||||
386.000000 -95.442953
|
||||
452.000000 -511.818439
|
||||
518.000000 -785.633833
|
||||
584.000000 -1377.367723
|
||||
650.000000 -2606.119973
|
||||
716.000000 -5115.078474
|
||||
782.000000 -9770.399635
|
||||
848.000000 -19298.079697
|
||||
914.000000 -37316.681269
|
@ -0,0 +1,133 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def generate_dataset(func_type, coeff, n_samples=100, noise_level=0.0):
|
||||
"""
|
||||
Generate a dataset based on a mathematical function.
|
||||
|
||||
Parameters:
|
||||
func_type (str): Type of function ('linear', 'quadratic', 'cubic', 'quartic', 'exponential', 'logarithmic').
|
||||
coeff (list): Coefficients of the function.
|
||||
n_samples (int): Number of samples to generate.
|
||||
noise_level (float): Standard deviation of Gaussian noise added to the data.
|
||||
|
||||
Returns:
|
||||
np.ndarray: Generated dataset.
|
||||
"""
|
||||
# Generate x values
|
||||
x_values = np.linspace(-1000, 1000, n_samples)
|
||||
|
||||
# Define the functions
|
||||
functions = {
|
||||
'linear': lambda x: coeff[0] + coeff[1] * x,
|
||||
'quadratic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2,
|
||||
'cubic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3,
|
||||
'quartic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3 + coeff[4] * x**4,
|
||||
'exponential': lambda x: coeff[0] * np.exp(coeff[1] * x),
|
||||
'logarithmic': lambda x: coeff[0] + coeff[1] * np.log(np.abs(x) + 1) # Avoid log(0)
|
||||
}
|
||||
|
||||
# Generate y values based on the selected function
|
||||
y_values = functions[func_type](x_values)
|
||||
|
||||
# Add noise
|
||||
noise = np.random.normal(0, noise_level, n_samples)
|
||||
y_values_noisy = y_values + noise
|
||||
|
||||
# Combine x and y values
|
||||
dataset = np.vstack((x_values, y_values_noisy)).T
|
||||
|
||||
return dataset
|
||||
|
||||
|
||||
def plot_dataset(dataset, func_type, coeff):
|
||||
"""
|
||||
Plot the generated dataset.
|
||||
|
||||
Parameters:
|
||||
dataset (np.ndarray): Generated dataset.
|
||||
func_type (str): Type of function used to generate the dataset.
|
||||
coeff (list): Coefficients of the function.
|
||||
"""
|
||||
x_values = dataset[:, 0]
|
||||
y_values = dataset[:, 1]
|
||||
|
||||
# Plotting the dataset
|
||||
plt.figure(figsize=(10, 6))
|
||||
plt.scatter(x_values, y_values, color='blue', label='Generated Data')
|
||||
|
||||
# Plot the original function without noise for comparison
|
||||
if func_type == 'linear':
|
||||
y_original = coeff[0] + coeff[1] * x_values
|
||||
elif func_type == 'quadratic':
|
||||
y_original = coeff[0] + coeff[1] * x_values + coeff[2] * x_values**2
|
||||
elif func_type == 'cubic':
|
||||
y_original = coeff[0] + coeff[1] * x_values + coeff[2] * x_values**2 + coeff[3] * x_values**3
|
||||
elif func_type == 'quartic':
|
||||
y_original = coeff[0] + coeff[1] * x_values + coeff[2] * x_values**2 + coeff[3] * x_values**3 + coeff[4] * x_values**4
|
||||
elif func_type == 'exponential':
|
||||
y_original = coeff[0] * np.exp(coeff[1] * x_values)
|
||||
elif func_type == 'logarithmic':
|
||||
y_original = coeff[0] + coeff[1] * np.log(np.abs(x_values) + 1)
|
||||
|
||||
plt.plot(x_values, y_original, color='red', label='Original Function')
|
||||
|
||||
plt.title(f'{func_type.capitalize()} Function Dataset Visualization')
|
||||
plt.xlabel('X Values')
|
||||
plt.ylabel('Y Values')
|
||||
plt.legend()
|
||||
plt.grid(True)
|
||||
plt.show()
|
||||
|
||||
def generate_dataset2(func_type, coeff, x_range=(-1000, 1000), n_samples=100, noise_level=0.0):
|
||||
"""
|
||||
Generate a dataset based on a mathematical function with a specified x range.
|
||||
|
||||
Parameters:
|
||||
func_type (str): Type of function ('linear', 'quadratic', 'cubic', 'quartic', 'exponential', 'logarithmic').
|
||||
coeff (list): Coefficients of the function.
|
||||
x_range (tuple): The range of x values (min, max).
|
||||
n_samples (int): Number of samples to generate.
|
||||
noise_level (float): Standard deviation of Gaussian noise added to the data.
|
||||
|
||||
Returns:
|
||||
np.ndarray: Generated dataset.
|
||||
"""
|
||||
# Generate x values within the specified range
|
||||
x_values = np.linspace(x_range[0], x_range[1], n_samples)
|
||||
|
||||
# Define the functions
|
||||
functions = {
|
||||
'linear': lambda x: coeff[0] + coeff[1] * x,
|
||||
'quadratic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2,
|
||||
'cubic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3,
|
||||
'quartic': lambda x: coeff[0] + coeff[1] * x + coeff[2] * x**2 + coeff[3] * x**3 + coeff[4] * x**4,
|
||||
'exponential': lambda x: coeff[0] * np.exp(coeff[1] * x),
|
||||
'logarithmic': lambda x: coeff[0] + coeff[1] * np.log(np.abs(x) + 1) # Avoid log(0)
|
||||
}
|
||||
|
||||
# Generate y values based on the selected function
|
||||
y_values = functions[func_type](x_values)
|
||||
|
||||
# Clip y values to stay within the specified range
|
||||
y_values = np.clip(y_values, x_range[0], x_range[1])
|
||||
|
||||
# Add noise
|
||||
noise = np.random.normal(0, noise_level, n_samples)
|
||||
y_values_noisy = y_values + noise
|
||||
|
||||
# Clip noisy y values to stay within the specified range
|
||||
y_values_noisy = np.clip(y_values_noisy, x_range[0], x_range[1])
|
||||
|
||||
# Combine x and y values
|
||||
dataset = np.vstack((x_values, y_values_noisy)).T
|
||||
|
||||
return dataset
|
||||
|
||||
# Example usage
|
||||
dataset = generate_dataset('quadratic', [1, 2, 3], n_samples=10, noise_level=10)
|
||||
print(dataset)
|
||||
|
||||
# Visualize the dataset
|
||||
dataset = generate_dataset('quadratic', [1, 2, 3], n_samples=10, noise_level=10)
|
||||
plot_dataset(dataset, 'quadratic', [1, 2, 3])
|