Commit e86d6f8f authored by Dr.李's avatar Dr.李

fixed a issue for array ordering

parent 5df30c1b
...@@ -13,7 +13,6 @@ from libc.math cimport sqrt ...@@ -13,7 +13,6 @@ from libc.math cimport sqrt
from libc.math cimport fabs from libc.math cimport fabs
from libc.stdlib cimport malloc from libc.stdlib cimport malloc
from libc.stdlib cimport free from libc.stdlib cimport free
from numpy import array
from libcpp.vector cimport vector as cpp_vector from libcpp.vector cimport vector as cpp_vector
from libcpp.unordered_map cimport unordered_map as cpp_map from libcpp.unordered_map cimport unordered_map as cpp_map
from cython.operator cimport dereference as deref from cython.operator cimport dereference as deref
...@@ -220,6 +219,8 @@ cpdef np.ndarray[double, ndim=2] transform(long[:] groups, double[:, :] x, str f ...@@ -220,6 +219,8 @@ cpdef np.ndarray[double, ndim=2] transform(long[:] groups, double[:, :] x, str f
cdef size_t loop_idx1 cdef size_t loop_idx1
cdef size_t loop_idx2 cdef size_t loop_idx2
x = np.ascontiguousarray(x)
try: try:
if func == 'mean': if func == 'mean':
value_data_ptr = agg_mean(mapped_groups, max_g[0], &x[0, 0], length, width) value_data_ptr = agg_mean(mapped_groups, max_g[0], &x[0, 0], length, width)
...@@ -258,6 +259,8 @@ cpdef np.ndarray[double, ndim=2] aggregate(long[:] groups, double[:, :] x, str f ...@@ -258,6 +259,8 @@ cpdef np.ndarray[double, ndim=2] aggregate(long[:] groups, double[:, :] x, str f
cdef double* value_data_ptr cdef double* value_data_ptr
cdef np.ndarray[double, ndim=2] res cdef np.ndarray[double, ndim=2] res
x = np.ascontiguousarray(x)
try: try:
if func == 'mean': if func == 'mean':
value_data_ptr = agg_mean(mapped_groups, max_g[0], &x[0, 0], length, width) value_data_ptr = agg_mean(mapped_groups, max_g[0], &x[0, 0], length, width)
......
...@@ -50,13 +50,13 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp ...@@ -50,13 +50,13 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
if output_explained: if output_explained:
explained = ls_explain(x, b) explained = ls_explain(x, b)
elif output_exposure: if output_exposure:
exposure = b exposure = b
output_dict = {} output_dict = {}
if output_explained: if output_explained:
output_dict['explained'] = explained output_dict['explained'] = explained
elif output_exposure: if output_exposure:
output_dict['exposure'] = exposure output_dict['exposure'] = exposure
if output_dict: if output_dict:
...@@ -95,5 +95,4 @@ if __name__ == '__main__': ...@@ -95,5 +95,4 @@ if __name__ == '__main__':
y = np.random.randn(3000, 2) y = np.random.randn(3000, 2)
groups = np.random.randint(30, size=3000) groups = np.random.randint(30, size=3000)
b = ls_fit(x, y) print(neutralize(x, y, groups, output_explained=True, output_exposure=True))
ls_explained(x, y, b)
\ No newline at end of file
...@@ -6,12 +6,14 @@ Created on 2017-4-25 ...@@ -6,12 +6,14 @@ Created on 2017-4-25
""" """
import numpy as np import numpy as np
from numpy import ascontiguousarray
from alphamind.aggregate import transform from alphamind.aggregate import transform
def standardize(x: np.ndarray, groups: np.ndarray=None) -> np.ndarray: def standardize(x: np.ndarray, groups: np.ndarray=None) -> np.ndarray:
x = ascontiguousarray(x)
if groups is not None: if groups is not None:
mean_values = transform(groups, x, 'mean') mean_values = transform(groups, x, 'mean')
std_values = transform(groups, x, 'std') std_values = transform(groups, x, 'std')
...@@ -19,3 +21,13 @@ def standardize(x: np.ndarray, groups: np.ndarray=None) -> np.ndarray: ...@@ -19,3 +21,13 @@ def standardize(x: np.ndarray, groups: np.ndarray=None) -> np.ndarray:
return (x - mean_values) / std_values return (x - mean_values) / std_values
else: else:
return (x - x.mean(axis=0)) / x.std(axis=0) return (x - x.mean(axis=0)) / x.std(axis=0)
if __name__ == '__main__':
import pandas as pd
df = pd.read_csv('d:/test_data.csv', index_col=0)
x = df.values
groups = df.index.values.astype(int)
standardize(x, groups)
...@@ -6,11 +6,12 @@ Created on 2017-4-25 ...@@ -6,11 +6,12 @@ Created on 2017-4-25
""" """
import numpy as np import numpy as np
from numpy import ascontiguousarray
from alphamind.aggregate import transform from alphamind.aggregate import transform
def winsorize_normal(x: np.ndarray, num_stds: int=3, groups: np.ndarray=None) -> np.ndarray: def winsorize_normal(x: np.ndarray, num_stds: int=3, groups: np.ndarray=None) -> np.ndarray:
x = ascontiguousarray(x)
if groups is not None: if groups is not None:
mean_values = transform(groups, x, 'mean') mean_values = transform(groups, x, 'mean')
......
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