In [2]:
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import pandas as pd

In [3]:
# CARGAR DATASET DE DROPBOX
#-----------------------------------------------------------------
data = pd.read_csv('../Recursos/indian_liver_patient.csv')
data=data.dropna()
data["Gender"] = pd.Categorical.from_array(data["Gender"]).codes
data['Dataset']=data['Dataset']-1

data = data.as_matrix()
data = np.matrix(data)


# CREA dataset TRAIN y TEST
#---------------------------------------------------------------------------------------------
np.random.seed(123)
m_train    = np.random.rand(len(data)) < 0.5
data_train = data[m_train,]
data_test  = data[~m_train,]


# CLASE
#---------------------------------------------------------------------------------------------
clase_train = data_train[:,-1]
print (clase_train)

clase_train = clase_train.A1 #convierte de matriz a vector 
clase_test  = data_test[:,-1]
clase_test  = clase_test.A1 #convierte de matriz a vector 


# MODELO
#---------------------------------------------------------------------------------------------
modelo_lr = LogisticRegression()
modelo_lr.fit(X=data_train[:,:-1],y=clase_train)


# PREDICCION
#---------------------------------------------------------------------------------------------
predicion = modelo_lr.predict(data_test[:,:-1])


# METRICAS
#---------------------------------------------------------------------------------------------
print(metrics.classification_report(y_true=clase_test, y_pred=predicion))
print(pd.crosstab(data_test[:,-1].A1, predicion, rownames=['REAL'], colnames=['PREDICCION']))


[[ 0.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 0.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 1.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.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
REAL                
0.0         176   37
1.0          45   27
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:11: FutureWarning: Categorical.from_array is deprecated, use Categorical instead
  # This is added back by InteractiveShellApp.init_path()

In [5]:
x  =  np . arange ( 10 ) 
x


Out[5]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [6]:
x [ 2 : 5 ] 
#array ([2, 3, 4])


Out[6]:
array([2, 3, 4])

In [7]:
x [: - 7 ] 
#array ([0, 1, 2])


Out[7]:
array([0, 1, 2])

In [8]:
x [ 1 : 7 : 2 ] 
#array ([1, 3, 5])


Out[8]:
array([1, 3, 5])

In [12]:
y = np.arange(35).reshape(5,7)
y


Out[12]:
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])

In [13]:
y [ 1 :5 : 2 , :: 3 ]


Out[13]:
array([[ 7, 10, 13],
       [21, 24, 27]])

In [ ]: