Open mooksys opened 3 years ago
`##### import numpy
def main(): (N, X, Y) = read_data() print(N) print(X) print(Y)
def read_data():
N = int(input())
X = []
Y = []
for i in range(N):
line = input().strip().split(" ")
X.append(float(line[0]))
Y.append(float(line[1]))
return (N, X, Y)
if name == "main": main()`
`#####
import numpy import statsmodels.api from draw_graph import visualize
def main(): (N, X, Y) = read_data() results = do_simple_regression(N, X, Y)
visualize(X, Y, results)
def read_data():
# Copy-and-paste your code from the previous exercise
N = int(input())
X = []
Y = []
for i in range(N):
line = input().strip().split(" ")
X.append(float(line[0]))
Y.append(float(line[1]))
return (N, X, Y)
def do_simple_regression(N, X, Y):
X = statsmodels.api.add_constant(X)
results = statsmodels.api.OLS(Y, X).fit()
return results
if name == "main": main()`
import statsmodels.api as stapi import numpy import pandas as pd
def main(): (N, X, Y) = read_data()
results = do_multivariate_regression(N, X, Y)
print(results.summary())
effective_variables = get_effective_variables(results)
print(effective_variables)
def do_multivariate_regression(N, X, Y):
X = numpy.array(X)
results = stapi.OLS(Y,X).fit()
print(results)
return results
def get_effective_variables(results): eff_vars = []
n = 0
df_result = pd.DataFrame(results.summary().tables[1].data[1:])
for pvalue in df_result[4]:
if float(pvalue) < 0.05:
eff_vars.append(df_result.loc[0][n])
n+=1
return eff_vars
def read_data():
N = 0
X = []
Y = []
with open("students.txt") as f:
next(f)
for line in f:
splits = line.strip().split(" ")
numeric_data = [float(x) for x in splits]
x = numeric_data[0:-1]
y = numeric_data[-1]
X.append(x)
Y.append(y)
N += 1
# X must be numpy.array in (30 * 5) shape.
# Y must be 1-dimensional numpy.array.
X = numpy.array(X)
Y = numpy.array(Y)
return (N, X, Y)
if name == "main": main()
2020.10.09 Study Memo
https://nipa.elice.io/
Open kakao https://open.kakao.com/o/gcTXx9wc