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 :: add row to dataframe with index 
Python :: python dictionary get value if key exists 
Python :: download files from url in flask 
Python :: python object name 
Python :: reply_photo bot telegram python 
Python :: how to combine two lists in python 
Python :: arange float step 
Python :: how to pop an exact number from a list in python 
Python :: python string starts with any char of list 
Python :: pd.concat in python 
Python :: execute command in python 
Python :: how to print 2 list in python as table 
Python :: find string in list and return index python 
Python :: python outlook 
Python :: join two querysets django 
Python :: i have two versions of python installed mac 
Python :: Add two numbers as a linked list 
Python :: csv in python 
Python :: python tuple 
Python :: append list python 
Python :: seaborn and matplotlib python 
Python :: python check if string is url 
Python :: python file save 
Python :: stop word python 
Python :: how to drop columns from pandas dataframe 
Python :: python increment filename by 1 
Python :: print on same line 
Python :: character in python 
Python :: pd.merge duplicate columns remove 
Python :: disable sns plot python 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =