title: What is the difference between dtype= and .astype() in numpy?
body: <p>Context: I would like to use <code>numpy ndarrays</code> with <code>float32</code> instead of <code>float64</code>.</p>
  
  <p>Edit: Additional context -  I'm concerned about how <code>numpy</code> is executing these calls because they will be happening repeatedly as part of a backpropagation routine in a neural net. I'd like the net to carry out all addition/subtraction/multiplication/division in <code>float32</code> for validation purposes, as I want to compare results with another group's work. It seems like initialization for methods like <code>randn</code> will always go from <code>float64</code> -> <code>float32</code> with <code>.astype()</code> casting. Once my <code>ndarray</code> is of type <code>float32</code> if i use <code>np.dot</code> for example will those multiplications happen in <code>float32</code>? How can I verify?</p>
  
  <p>The documentation is not clear to me - <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html" rel="noreferrer">http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html</a></p>
  
  <p>I figured out I can just add <code>.astype('float32')</code> to the end of a numpy call, for example, <code>np.random.randn(y, 1).astype('float32')</code>.</p>
  
  <p>I also see that <code>dtype=np.float32</code> is an option, for example, <code>np.zeros(5, dtype=np.float32)</code>. However, trying <code>np.random.randn((y, 1), dtype=np.float32)</code> returns the following error: </p>
  
  <pre><code>    b = np.random.randn((3,1), dtype=np.float32)
  TypeError: randn() got an unexpected keyword argument 'dtype'
  </code></pre>
  
  <p>What is the difference between declaring the type as <code>float32</code> using <code>dtype</code> and using <code>.astype()</code>?</p>
  
  <p>Both <code>b = np.zeros(5, dtype=np.float32)</code> and <code>b = np.zeros(5).astype('float32')</code> when evaluated with: </p>
  
  <pre><code>print(type(b))
  print(b[0])
  print(type(b[0]))
  </code></pre>
  
  <p>prints:</p>
  
  <pre><code>[ 0.  0.  0.  0.  0.]
  &lt;class 'numpy.ndarray'&gt;
  0.0
  &lt;class 'numpy.float32'&gt;
  </code></pre>