Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to make a numpy array

>>> x = np.array([2,3,1,0])
>>> x = np.array([2, 3, 1, 0])
>>> x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists,
    and types
>>> x = np.array([[ 1.+0.j, 2.+0.j], [ 0.+0.j, 0.+0.j], [ 1.+1.j, 3.+0.j]])
Comment

numpy creating arrays

import numpy as np

# create numpy array
arr = np.array([1, 2, 3, 4, 5])
print(type(arr))

# 1d-array
arr = np.array(42)
print(type(arr))

# 2d-array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

# 3d-array
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)

# copy an array any changes make to the copy of the array does not affect the original
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42

print(arr)
# output [42  2  3  4  5]

print(x)
# output [1 2 3 4 5]

# view an array any changes made to the view will affect the original array
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42

print(arr)
# output [42  2  3  4  5]

print(x)
# output [42  2  3  4  5]
Comment

create array numpy

import numpy as np
a = np.array([1, 2, 3])
Comment

array creation method in numpy

>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(2, 10, dtype=float)
array([ 2., 3., 4., 5., 6., 7., 8., 9.])
>>> np.arange(2, 3, 0.1)
array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
Comment

Array Creation in Numpy

>>> import numpy as np
>>> a = np.array([2, 3, 4])
>>> a
array([2, 3, 4])
>>> a.dtype
dtype('int64')
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64')
Comment

create array numpy

a = np.array([1, 2, 3, 4, 5, 6])
Comment

PREVIOUS NEXT
Code Example
Python :: python for loop range 
Python :: python concatenate dictionaries 
Python :: python pickle dataframe 
Python :: update django model with dict 
Python :: python random.sample 
Python :: return array of sorted objects 
Python :: reshape SAS matrix 
Python :: numpy arange 
Python :: python if file exists append else create 
Python :: get fields in object python 
Python :: django model choice field from another model 
Python :: print("hello world") 
Python :: flask orm update query 
Python :: # /usr/bin/env python windows 
Python :: json payload python function 
Python :: pop up window flutter 
Python :: append and extend in python 
Python :: get maximum value index after groupby 
Python :: Python Pandas - How to write in a specific column in an Excel Sheet 
Python :: np logical not 
Python :: primes python 
Python :: global array python 
Python :: how to form .cleaned data in class based views in django 
Python :: checking length of sets in python 
Python :: dm command in discord.py 
Python :: get full path of document 
Python :: python if in list 
Python :: python destructuring 
Python :: python django login register 
Python :: tree implementation in python 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =