Skip to content

Instantly share code, notes, and snippets.

@leouieda
Created February 18, 2014 21:03
Show Gist options
  • Save leouieda/9080042 to your computer and use it in GitHub Desktop.
Save leouieda/9080042 to your computer and use it in GitHub Desktop.
Mostra como selecionar valores em janelas em arrays do numpy
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x, y, z, a, b = np.random.random((5, 100))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"array([ 0.38347703, 0.72930132, 0.80833064, 0.59948685, 0.00450876,\n",
" 0.195882 , 0.3440675 , 0.37689126, 0.97034056, 0.49223124,\n",
" 0.96085161, 0.20620406, 0.42863137, 0.08838655, 0.9763976 ,\n",
" 0.18416946, 0.50958441, 0.81300376, 0.12885084, 0.56437768,\n",
" 0.94714501, 0.57329239, 0.68517223, 0.47344085, 0.37550968,\n",
" 0.3568382 , 0.13230695, 0.17185914, 0.29903128, 0.1840064 ,\n",
" 0.84165317, 0.999084 , 0.69054322, 0.28187987, 0.60381256,\n",
" 0.6594763 , 0.35319701, 0.90825385, 0.56087865, 0.00780448,\n",
" 0.83186407, 0.52946156, 0.85971096, 0.49146173, 0.32100071,\n",
" 0.64076938, 0.74206113, 0.61970082, 0.62109085, 0.31128781,\n",
" 0.52162857, 0.03403476, 0.19988967, 0.95955889, 0.64165197,\n",
" 0.67379284, 0.57519088, 0.23727708, 0.18329255, 0.98401507,\n",
" 0.47364799, 0.52727449, 0.19787549, 0.13898673, 0.56610483,\n",
" 0.97824382, 0.3695975 , 0.93710168, 0.09924009, 0.83482483,\n",
" 0.49763417, 0.92573068, 0.45149784, 0.16510749, 0.3778679 ,\n",
" 0.93288464, 0.25670374, 0.16021516, 0.25897521, 0.52594776,\n",
" 0.64765792, 0.65871185, 0.56315786, 0.47720363, 0.95981874,\n",
" 0.43611608, 0.56131352, 0.63838471, 0.25712586, 0.40705896,\n",
" 0.94806679, 0.32704949, 0.79501072, 0.44492026, 0.47516401,\n",
" 0.00177196, 0.32696117, 0.50971069, 0.57179914, 0.19541469])"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Quero separar todos os pontos que caem em x < 0.5, y < 0.5 e os que caem em x >= 0.5 e y >= 0.5 (janelar)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"maior = (x >= 0.5) & (y >= 0.5) # o & significa \"e\"\n",
"maior"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"array([False, True, False, False, False, False, False, False, True,\n",
" False, False, False, False, False, False, False, False, False,\n",
" False, True, False, True, True, False, False, False, False,\n",
" False, False, False, False, True, True, False, True, True,\n",
" False, True, True, False, False, False, True, False, False,\n",
" True, False, True, False, False, True, False, False, True,\n",
" False, True, True, False, False, True, False, False, False,\n",
" False, True, True, False, False, False, True, False, False,\n",
" False, False, False, True, False, False, False, False, True,\n",
" True, False, False, True, False, True, False, False, False,\n",
" False, False, False, False, False, False, False, True, True, False], dtype=bool)"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pega s\u00f3 os valores que s\u00e3o True (tem x e y >= 0.5):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x1, y1, z1, a1, b1 = x[maior], y[maior], z[maior], a[maior], b[maior]\n",
"x1, y1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"(array([ 0.72930132, 0.97034056, 0.56437768, 0.57329239, 0.68517223,\n",
" 0.999084 , 0.69054322, 0.60381256, 0.6594763 , 0.90825385,\n",
" 0.56087865, 0.85971096, 0.64076938, 0.61970082, 0.52162857,\n",
" 0.95955889, 0.67379284, 0.57519088, 0.98401507, 0.56610483,\n",
" 0.97824382, 0.83482483, 0.93288464, 0.64765792, 0.65871185,\n",
" 0.95981874, 0.56131352, 0.50971069, 0.57179914]),\n",
" array([ 0.70895394, 0.72248934, 0.57252256, 0.6337384 , 0.52655792,\n",
" 0.55436443, 0.69973717, 0.91472746, 0.87137035, 0.84904184,\n",
" 0.63892212, 0.64034837, 0.66456271, 0.65786306, 0.90756032,\n",
" 0.76401514, 0.95246724, 0.83315264, 0.87994763, 0.76256055,\n",
" 0.81114195, 0.99396222, 0.70135391, 0.52988068, 0.8642717 ,\n",
" 0.56054641, 0.74745299, 0.97597317, 0.79143307]))"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pega os valores que s\u00e3o False:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x2, y2, z2, a2, b2 = x[~maior], y[~maior], z[~maior], a[~maior], b[~maior] # ~ quer dizer \"n\u00e3o\"\n",
"x2, y2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"(array([ 0.38347703, 0.80833064, 0.59948685, 0.00450876, 0.195882 ,\n",
" 0.3440675 , 0.37689126, 0.49223124, 0.96085161, 0.20620406,\n",
" 0.42863137, 0.08838655, 0.9763976 , 0.18416946, 0.50958441,\n",
" 0.81300376, 0.12885084, 0.94714501, 0.47344085, 0.37550968,\n",
" 0.3568382 , 0.13230695, 0.17185914, 0.29903128, 0.1840064 ,\n",
" 0.84165317, 0.28187987, 0.35319701, 0.00780448, 0.83186407,\n",
" 0.52946156, 0.49146173, 0.32100071, 0.74206113, 0.62109085,\n",
" 0.31128781, 0.03403476, 0.19988967, 0.64165197, 0.23727708,\n",
" 0.18329255, 0.47364799, 0.52727449, 0.19787549, 0.13898673,\n",
" 0.3695975 , 0.93710168, 0.09924009, 0.49763417, 0.92573068,\n",
" 0.45149784, 0.16510749, 0.3778679 , 0.25670374, 0.16021516,\n",
" 0.25897521, 0.52594776, 0.56315786, 0.47720363, 0.43611608,\n",
" 0.63838471, 0.25712586, 0.40705896, 0.94806679, 0.32704949,\n",
" 0.79501072, 0.44492026, 0.47516401, 0.00177196, 0.32696117,\n",
" 0.19541469]),\n",
" array([ 0.79238169, 0.45928511, 0.36045462, 0.49243362, 0.62236971,\n",
" 0.47420809, 0.44779191, 0.54080347, 0.38014737, 0.95726386,\n",
" 0.20129776, 0.14067673, 0.18700342, 0.87753595, 0.36907347,\n",
" 0.15897792, 0.18476607, 0.44849952, 0.66068619, 0.302322 ,\n",
" 0.37055578, 0.64533868, 0.7518407 , 0.96289352, 0.1160509 ,\n",
" 0.27984468, 0.27807043, 0.74228032, 0.31862221, 0.43234465,\n",
" 0.46217636, 0.54348263, 0.47654905, 0.35092138, 0.09297288,\n",
" 0.46518861, 0.86397391, 0.18692285, 0.44394745, 0.55846222,\n",
" 0.31006777, 0.27520676, 0.09141696, 0.74307628, 0.27063032,\n",
" 0.78585713, 0.16223304, 0.51821689, 0.97835713, 0.08932138,\n",
" 0.02334494, 0.94038898, 0.32985983, 0.70415965, 0.27996473,\n",
" 0.38156796, 0.15089327, 0.25023931, 0.37451291, 0.68251706,\n",
" 0.37874188, 0.97485291, 0.58054213, 0.04724522, 0.70607863,\n",
" 0.12575271, 0.23215378, 0.68190163, 0.4490063 , 0.67170735,\n",
" 0.50895717]))"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment