import numpy as np
32 Importar archivos con NumPy
¿Preparades para explorar las opciones de carga de datos en NumPy? En esta sesión nos sumergiremos en las profundidades de las funciones loadtxt y genfromtxt, dos pilares fundamentales para la importación de datos en esta poderosa biblioteca.
Con loadtxt, experimentarás la simplicidad y la velocidad al trabajar con datos homogéneos. Esta función es ideal para cargar datos simples y uniformes, permitiéndote un flujo de trabajo ágil y sin complicaciones. Por otro lado, genfromtxt se destaca por su capacidad para manejar la complejidad. Ya sea que estés lidiando con valores faltantes o estructuras irregulares, esta herramienta se adapta a tus necesidades, ofreciendo una flexibilidad inigualable.
A lo largo de esta sesión, no solo identificarás las características únicas de tus datos, sino que también aprenderás a seleccionar la herramienta adecuada para cada escenario. Al final de la sesión, tendrás el conocimiento para decidir cuál función se alinea mejor con tus objetivos analíticos, optimizando así tu flujo de trabajo en el análisis de datos.
= '../data/matriz3x3.txt'
f = np.loadtxt(f)
m3x3 m3x3
array([[1. , 0.674, 0.392],
[0.183, 1. , 0.729],
[0.438, 0.255, 1. ]])
type(m3x3[0,0])
numpy.float64
0] m3x3[
array([1. , 0.674, 0.392])
0][0] m3x3[
1.0
type(m3x3[0][0])
numpy.float64
= '../data/matriz10x10.txt'
f = np.genfromtxt(f)
m10x10 m10x10
array([[1. , 0.891773 , 0.96366276, 0.38344152, 0.79172504,
0.52889492, 0.56804456, 0.92559664, 0.07103606, 0.0871293 ],
[0.0202184 , 1. , 0.77815675, 0.87001215, 0.97861834,
0.79915856, 0.46147936, 0.78052918, 0.11827443, 0.63992102],
[0.14335329, 0.94466892, 1. , 0.41466194, 0.26455561,
0.77423369, 0.45615033, 0.56843395, 0.0187898 , 0.6176355 ],
[0.61209572, 0.616934 , 0.94374808, 1. , 0.3595079 ,
0.43703195, 0.6976312 , 0.06022547, 0.66676672, 0.67063787],
[0.21038256, 0.1289263 , 0.31542835, 0.36371077, 1. ,
0.43860151, 0.98837384, 0.10204481, 0.20887676, 0.16130952],
[0.65310833, 0.2532916 , 0.46631077, 0.24442559, 0.15896958,
1. , 0.65632959, 0.13818295, 0.19658236, 0.36872517],
[0.82099323, 0.09710128, 0.83794491, 0.09609841, 0.97645947,
0.4686512 , 1. , 0.60484552, 0.73926358, 0.03918779],
[0.28280696, 0.12019656, 0.2961402 , 0.11872772, 0.31798318,
0.41426299, 0.0641475 , 1. , 0.56660145, 0.26538949],
[0.52324805, 0.09394051, 0.5759465 , 0.9292962 , 0.31856895,
0.66741038, 0.13179786, 0.7163272 , 1. , 0.18319136],
[0.58651293, 0.02010755, 0.82894003, 0.00469548, 0.67781654,
0.27000797, 0.73519402, 0.96218855, 0.24875314, 1. ]])
np.loadtxt(f)
array([[1. , 0.891773 , 0.96366276, 0.38344152, 0.79172504,
0.52889492, 0.56804456, 0.92559664, 0.07103606, 0.0871293 ],
[0.0202184 , 1. , 0.77815675, 0.87001215, 0.97861834,
0.79915856, 0.46147936, 0.78052918, 0.11827443, 0.63992102],
[0.14335329, 0.94466892, 1. , 0.41466194, 0.26455561,
0.77423369, 0.45615033, 0.56843395, 0.0187898 , 0.6176355 ],
[0.61209572, 0.616934 , 0.94374808, 1. , 0.3595079 ,
0.43703195, 0.6976312 , 0.06022547, 0.66676672, 0.67063787],
[0.21038256, 0.1289263 , 0.31542835, 0.36371077, 1. ,
0.43860151, 0.98837384, 0.10204481, 0.20887676, 0.16130952],
[0.65310833, 0.2532916 , 0.46631077, 0.24442559, 0.15896958,
1. , 0.65632959, 0.13818295, 0.19658236, 0.36872517],
[0.82099323, 0.09710128, 0.83794491, 0.09609841, 0.97645947,
0.4686512 , 1. , 0.60484552, 0.73926358, 0.03918779],
[0.28280696, 0.12019656, 0.2961402 , 0.11872772, 0.31798318,
0.41426299, 0.0641475 , 1. , 0.56660145, 0.26538949],
[0.52324805, 0.09394051, 0.5759465 , 0.9292962 , 0.31856895,
0.66741038, 0.13179786, 0.7163272 , 1. , 0.18319136],
[0.58651293, 0.02010755, 0.82894003, 0.00469548, 0.67781654,
0.27000797, 0.73519402, 0.96218855, 0.24875314, 1. ]])
= '../data/matriz3x3_comment01.txt'
f np.loadtxt(f)
array([[1. , 0.674, 0.392],
[0.183, 1. , 0.729],
[0.438, 0.255, 1. ]])
= '../data/matriz3x3_comment02.txt'
f np.loadtxt(f)
ValueError: could not convert string '--' to float64 at row 0, column 1.
= '../data/matriz3x3_comment02.txt'
f ='--') np.loadtxt(f,comments
array([[1. , 0.674, 0.392],
[0.183, 1. , 0.729],
[0.438, 0.255, 1. ]])
= '../data/matriz3x3_comment02.txt'
f =1) np.loadtxt(f,skiprows
array([[1. , 0.674, 0.392],
[0.183, 1. , 0.729],
[0.438, 0.255, 1. ]])
= '../data/matriz3x3_comment02.txt'
f =1,usecols=[0]) np.loadtxt(f,skiprows
array([1. , 0.183, 0.438])
= '../data/matriz3x3_comment02.txt'
f =1,usecols=[0,2]) np.loadtxt(f,skiprows
array([[1. , 0.392],
[0.183, 0.729],
[0.438, 1. ]])
= '../data/matriz3x3_faltantes.txt'
f np.loadtxt(f)
ValueError: could not convert string 'Nulo' to float64 at row 1, column 2.
= '../data/matriz3x3_faltantes.txt'
f np.genfromtxt(f)
array([[1. , 0.674, 0.392],
[0.183, nan, 0.729],
[0.438, 0.255, 1. ]])
= '../data/matriz3x3_faltantes.txt'
f =0) np.genfromtxt(f,filling_values
array([[1. , 0.674, 0.392],
[0.183, 0. , 0.729],
[0.438, 0.255, 1. ]])