Commit 45fc0125 authored by Dr.李's avatar Dr.李

some adjust

parent 414ed809
......@@ -26,9 +26,9 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
exposure = zeros(x.shape + (y.shape[1],))
else:
if output_explained:
explained = zeros(x.shape)
explained = zeros(x.shape + (1,))
if output_exposure:
exposure = zeros(x.shape)
exposure = zeros(x.shape + (1,))
groups_ids = groupby(groups)
......@@ -37,13 +37,13 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
curr_y = y[curr_idx]
b = ls_fit(x[curr_idx], y[curr_idx])
res[curr_idx] = ls_res(curr_x, curr_y, b)
if output_exposure and exposure.ndim == 3:
if output_exposure:
for i in range(exposure.shape[2]):
exposure[curr_idx, :, i] = b[:, i]
elif output_exposure:
exposure[curr_idx] = b
if output_explained:
explained[curr_idx] = ls_explain(curr_x, b)
for i in range(explained.shape[2]):
b
explained[curr_idx, :, i] = ls_explain(curr_x, b)
else:
b = ls_fit(x, y)
res = ls_res(x, y, b)
......@@ -80,11 +80,11 @@ def ls_explain(x: np.ndarray, b: np.ndarray) -> np.ndarray:
return b * x
else:
n_samples = x.shape[0]
dependends = b.shape[1]
to_explain = b.shape[1]
factors = x.shape[1]
explained = zeros((n_samples, factors, dependends))
explained = zeros((n_samples, factors, to_explain))
for i in range(dependends):
for i in range(to_explain):
explained[:, :, i] = b[:, i] * x
return explained
......
......@@ -8,11 +8,8 @@ Created on 2017-4-26
import numpy as np
from numpy import zeros
from numpy import max as nmax
cimport numpy as np
cimport cython
from libc.math cimport sqrt
from libc.math cimport fabs
from libcpp.vector cimport vector as cpp_vector
from libcpp.unordered_map cimport unordered_map as cpp_map
from cython.operator cimport dereference as deref
......
......@@ -8,20 +8,12 @@ Created on 2017-4-26
import numpy as np
import numba as nb
from numpy import zeros
from numpy import zeros_like
from alphamind.groupby import groupby
@nb.njit
def set_value_bool(mat, used_level, to_fill):
length, width = used_level.shape
for i in range(length):
for j in range(width):
k = used_level[i, j]
mat[k, j] = to_fill
@nb.njit
def set_value_double(mat, used_level, to_fill):
def set_value(mat, used_level, to_fill):
length, width = used_level.shape
for i in range(length):
for j in range(width):
......@@ -38,33 +30,26 @@ def rank_build(er: np.ndarray, use_rank: int, groups: np.ndarray=None) -> np.nda
weights = zeros((length, 1))
if groups is not None:
group_ids = groupby(groups)
masks = zeros((length, 1), dtype=bool)
for current_index in group_ids:
current_ordering = neg_er[current_index].argsort()
current_ordering.shape = -1, 1
set_value_bool(masks, current_index[current_ordering[:use_rank]], True)
weights[masks] = 1.
set_value(weights, current_index[current_ordering[:use_rank]], 1.)
else:
ordering = neg_er.argsort()
weights[ordering[:use_rank]] = 1.
return weights.reshape(er.shape)
else:
length = er.shape[0]
width = er.shape[1]
neg_er = -er
weights = zeros((length, width))
weights = zeros_like(er)
if groups is not None:
group_ids = groupby(groups)
masks = zeros((length, width), dtype=bool)
for current_index in group_ids:
current_ordering = neg_er[current_index].argsort(axis=0)
set_value_bool(masks, current_index[current_ordering[:use_rank]], True)
for j in range(width):
weights[masks[:, j], j] = 1.
set_value(weights, current_index[current_ordering[:use_rank]], 1)
else:
ordering = neg_er.argsort(axis=0)
set_value_double(weights, ordering[:use_rank], 1.)
set_value(weights, ordering[:use_rank], 1.)
return weights
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment