mirror of
https://github.com/shizand/statapp.git
synced 2025-04-01 23:23:45 +03:00
убираю лишнее
This commit is contained in:
parent
45f91c53f2
commit
094a456d52
@ -19,12 +19,9 @@
|
||||
#
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
from PySide2 import QtCore
|
||||
from PySide2.QtWidgets import QApplication
|
||||
|
||||
from statapp import calculations
|
||||
from statapp.calculations import generateXValues, generateYValues
|
||||
from statapp.main_window import MainWindow
|
||||
|
||||
|
||||
@ -43,18 +40,4 @@ def main():
|
||||
return app.exec_()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Для быстрой отладки
|
||||
N = 10
|
||||
y = generateYValues(100, 5, N)
|
||||
x1 = generateXValues(20, 2, 0, y)
|
||||
x2 = generateXValues(10, 1, 0, y)
|
||||
|
||||
data = np.concatenate([y, x1, x2], axis=1)
|
||||
|
||||
out = calculations.squaredPolynom(data)
|
||||
|
||||
coef = []
|
||||
|
||||
print()
|
||||
|
||||
sys.exit(main())
|
||||
|
@ -1,6 +1,5 @@
|
||||
# Copyright (c) 2023 Matthew Rocklin
|
||||
# All rights reserved.
|
||||
import numpy as np
|
||||
# This source code is distributed under the terms of the BSD license,
|
||||
# which allows you to use, modify, and distribute it
|
||||
# as long as you comply with the license terms.
|
||||
@ -9,11 +8,10 @@ import numpy as np
|
||||
# is now also licensed under the GPL-3.0.
|
||||
# See the GPL-3.0 license for details.
|
||||
|
||||
# TODO: remove
|
||||
# pylint: skip-file
|
||||
|
||||
from numpy import linalg, zeros, ones, hstack, asarray, diagonal
|
||||
import itertools
|
||||
import numpy as np
|
||||
from numpy import linalg, zeros, ones, hstack, asarray, diagonal
|
||||
from sympy import symbols, Mul, Add, S
|
||||
|
||||
|
||||
def basisVector(n, i):
|
||||
@ -29,11 +27,14 @@ def basisVector(n, i):
|
||||
x[i] = 1
|
||||
return x
|
||||
|
||||
|
||||
def asTall(x):
|
||||
""" Turns a row vector into a column vector """
|
||||
return x.reshape(x.shape + (1,))
|
||||
|
||||
def multipolyfit(xs, y, deg, full=False, model_out=False, powers_out=False):
|
||||
|
||||
def multipolyfit(xs, y, deg, full=False, modelOut=False, powersOut=False):
|
||||
# pylint: disable-msg=too-many-locals
|
||||
"""
|
||||
Least squares multivariate polynomial fit
|
||||
|
||||
@ -49,10 +50,10 @@ def multipolyfit(xs, y, deg, full=False, model_out=False, powers_out=False):
|
||||
y-coordinates of the sample points.
|
||||
deg : int
|
||||
Degree o fthe fitting polynomial
|
||||
model_out : bool (defaults to True)
|
||||
modelOut : bool (defaults to True)
|
||||
If True return a callable function
|
||||
If False return an array of coefficients
|
||||
powers_out : bool (defaults to False)
|
||||
powersOut : bool (defaults to False)
|
||||
Returns the meaning of each of the coefficients in the form of an
|
||||
iterator that gives the powers over the inputs and 1
|
||||
For example if xs corresponds to the covariates a,b,c then the array
|
||||
@ -63,8 +64,9 @@ def multipolyfit(xs, y, deg, full=False, model_out=False, powers_out=False):
|
||||
numpy.polyfit
|
||||
|
||||
"""
|
||||
# pylin
|
||||
y = asarray(y).squeeze()
|
||||
rows = y.shape[0]
|
||||
# rows = y.shape[0]
|
||||
xs = asarray(xs)
|
||||
numCovariates = xs.shape[1]
|
||||
xs = hstack((ones((xs.shape[0], 1), dtype=xs.dtype) , xs))
|
||||
@ -81,10 +83,10 @@ def multipolyfit(xs, y, deg, full=False, model_out=False, powers_out=False):
|
||||
result = linalg.lstsq(a, y, rcond=None)
|
||||
beta = result[0]
|
||||
|
||||
if model_out:
|
||||
return mk_model(beta, powers)
|
||||
if modelOut:
|
||||
return mkModel(beta, powers)
|
||||
|
||||
if powers_out:
|
||||
if powersOut:
|
||||
return beta, powers
|
||||
|
||||
if full:
|
||||
@ -100,7 +102,8 @@ def multipolyfit(xs, y, deg, full=False, model_out=False, powers_out=False):
|
||||
|
||||
return beta
|
||||
|
||||
def mk_model(beta, powers):
|
||||
|
||||
def mkModel(beta, powers):
|
||||
""" Create a callable python function out of beta/powers from multipolyfit
|
||||
|
||||
This function is callable from within multipolyfit using the model_out flag
|
||||
@ -108,23 +111,23 @@ def mk_model(beta, powers):
|
||||
# Create a function that takes in many x values
|
||||
# and returns an approximate y value
|
||||
def model(*args):
|
||||
num_covariates = len(powers[0]) - 1
|
||||
if len(args)!=(num_covariates):
|
||||
raise ValueError("Expected %d inputs"%num_covariates)
|
||||
numCovariates = len(powers[0]) - 1
|
||||
if len(args) != numCovariates:
|
||||
raise ValueError(f"Expected {numCovariates} inputs")
|
||||
xs = asarray((1,) + args)
|
||||
return sum([coeff * (xs**p).prod()
|
||||
for p, coeff in zip(powers, beta)])
|
||||
return sum(coeff * (xs**p).prod()
|
||||
for p, coeff in zip(powers, beta))
|
||||
return model
|
||||
|
||||
def mk_sympy_function(beta, powers):
|
||||
from sympy import symbols, Add, Mul, S
|
||||
terms = get_terms(powers)
|
||||
|
||||
def mkSympyFunction(beta, powers):
|
||||
terms = getTerms(powers)
|
||||
return Add(*[coeff * term for term, coeff in zip(terms, beta)])
|
||||
|
||||
def get_terms(powers):
|
||||
from sympy import symbols, Add, Mul, S
|
||||
num_covariates = len(powers[0])
|
||||
xs = (S.One,) + symbols('x1:%d' % num_covariates)
|
||||
|
||||
def getTerms(powers):
|
||||
numCovariates = len(powers[0])
|
||||
xs = (S.One,) + symbols(f'x1:{numCovariates}')
|
||||
|
||||
terms = [Mul(*[x ** deg for x, deg in zip(xs, power)]) for power in powers]
|
||||
return terms
|
||||
|
@ -21,7 +21,7 @@ from dataclasses import dataclass
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from statapp._vendor.multipolyfit import multipolyfit, mk_sympy_function
|
||||
from statapp._vendor.multipolyfit import multipolyfit
|
||||
|
||||
DIRECT_LINK = 0
|
||||
INDIRECT_LINK = 1
|
||||
@ -112,18 +112,13 @@ class ExtendedRegressionResult:
|
||||
def squaredPolynom(inputData):
|
||||
x = inputData[:, 1:]
|
||||
y = inputData[:, 0]
|
||||
data = pd.DataFrame(x)
|
||||
result, powers, tStatistics, mse = multipolyfit(x, y, 2, full=True)
|
||||
betas = result[0]
|
||||
res = mk_sympy_function(betas, powers)
|
||||
print(data)
|
||||
print(res)
|
||||
|
||||
out = pd.DataFrame()
|
||||
out[0] = betas
|
||||
out[1] = tStatistics
|
||||
|
||||
|
||||
return ExtendedRegressionResult(
|
||||
out.to_numpy(),
|
||||
np.float64(mse[0]),
|
||||
|
@ -18,7 +18,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
from statapp._vendor.multipolyfit import get_terms
|
||||
from statapp._vendor.multipolyfit import getTerms
|
||||
from statapp.models.linear_polynom_model import LinearPolynomModel
|
||||
|
||||
|
||||
@ -26,4 +26,4 @@ class SquaredPolynomModel(LinearPolynomModel):
|
||||
powers: list
|
||||
|
||||
def getVerticalHeader(self):
|
||||
return ['c' if str(x) == '1' else str(x) for x in get_terms(self.powers)]
|
||||
return ['c' if str(x) == '1' else str(x) for x in getTerms(self.powers)]
|
||||
|
Loading…
Reference in New Issue
Block a user