Numpy is a library in python
What is numpy?
import numpy as np
How to create arry function in python?
[6, 7.5, 8, 0, 1]
How to create equal lengh list function in python?
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
How to create zero matrix in python?
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000]],
[[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000]]])
How to see data type in python?
[16., 25., 36.]])
[0., 0., 0.]])
[0.25 , 0.2 , 0.16666667]])
[2. , 2.23606798, 2.44948974]])
How to see individual element in python?
9])
Higher dimensional arrays, you have
many more options.
In a two-dimensional
array, the elements at each index are no longer scalars
but rather one-dimensional arrays
import numpy as np
How to create arry function in python?
[6, 7.5, 8, 0, 1]
How to create equal lengh list function in python?
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
How to create zero matrix in python?
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000]],
[[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000],
[0.000000e+000, 0.000000e+000]]])
How to see data type in python?
[16., 25., 36.]])
[0., 0., 0.]])
[0.25 , 0.2 , 0.16666667]])
[2. , 2.23606798, 2.44948974]])
How to see individual element in python?
9])
Higher dimensional arrays, you have
many more options.
In a two-dimensional
array, the elements at each index are no longer scalars
but rather one-dimensional arrays
Numpy is most required library of python is numpy.Numpy is a core library for computing with python that provides a foundation for all computational libraries and its usage patterns is a fundamental skill for python.
import pandas as pd
import matplotlib.pyplot as plt
Describe Numpy arry:
# ARRYS AND VECTRISED COMPUTATION
NumPy, short for Numerical Python, is the fundamental package. required for high performance scientific computing and data analysis. ndarray, a fast and space-efficient multidimensional array providing vectorized arithmetic operations and sophisticated broadcasting capabilities • Standard mathematical functions for fast operations on entire arrays of data without having to write loops • Tools for reading / writing array data to disk and working with memory-mapped files • Linear algebra, random number generation, and Fourier transform capabilities • Tools for integrating code written in C, C++, and Fortran
# Creating ndarrays
#The easiest way to create an array is
to use the array function
data1 = [6, 7.5, 8, 0, 1]
data1
data1*10
data1+data1
[6, 7.5, 8, 0, 1, 6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1
array([6. , 7.5, 8. , 0. , 1. ])
#Nested sequences, like a list of equal-length
lists, will be converted
into a multidimensional array:
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
arr2
arr2.ndim
2
arr2.shape
(2, 4)
arr1.dtype
dtype('float64')
arr2.dtype
dtype('int64')
#In addition to np.array, there are a
number of other functions for
creating new arrays.
# As examples, zeros and ones create
arrays of 0’s or 1’s, respectively,
with a given length or shape. empty
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((3, 6))
array([[0., 0., 0., 0., 0., 0.],
np.empty((2, 3, 2))
array([[[4.669867e-310, 0.000000e+000],
#arange is an array-valued version of the built-in
Python range function:
np.arange(15)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
array Convert input data (list, tuple, array, or other sequence type)
to an ndarray either by inferring a dtype or explicitly specifying a dtype.
Copies the input data by default. asarray Convert input to ndarray,
but do not copy if the input is already an ndarray arange
Like the built-in range but returns an ndarray instead of a list.
ones, ones_like Produce an array of all 1’s with the given
shape and dtype. ones_like takes another array and produces
a ones array of the same shape and dtype. zeros, zeros_like
Like ones and ones_like but producing arrays of 0’s instead
arr1 = np.array([1, 2, 3], dtype=np.float64)
arr1
array([1., 2., 3.])
arr2 = np.array([1, 2, 3], dtype=np.int32)
arr2
array([1, 2, 3], dtype=int32)
arr1.dtype
dtype('float64')
arr2.dtype
dtype('int32')
arr = np.array([1, 2, 3, 4, 5])
arr.dtype
dtype('int64')
float_arr = arr.astype(np.float64)
float_arr
array([1., 2., 3., 4., 5.])
float_arr.dtypedtype('float64')#Operations between Arrays and ScalarsArrays are important because they enable you to expressbatch operations on data without writing any for loops.This is usually called vectorization. Any arithmetic operationsbetween equal-size arrays applies the operation elementwise:arr = np.array([[1., 2., 3.], [4., 5., 6.]])arr*arrarray([[ 1., 4., 9.],arr - arrarray([[0., 0., 0.],1 / arrarray([[1. , 0.5 , 0.33333333],arr ** 0.5array([[1. , 1.41421356, 1.73205081],Basic Indexing and Slicing *NumPy arrayindexing is a rich topic, as there are many waysyou may want to select a subset of your data orindividual elements. One-dimensional arrays aresimple; on the surface they act similarly to Python lists: *arr = np.arange(10)arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])arr[5]5arr[5:8]array([5, 6, 7])arr[5:8] = 12arrarray([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])arr_slice = arr[5:8]arr_slice[1] = 12345arrarray([ 0, 1, 2, 3, 4, 12, 12345, 12, 8,arr_slice[:] = 64
0 Comments