Numpy python

Numpy is a library in python

numpy python with mathclasstutor


What is numpy?

 Numpy is most required library of python is numpy.

import numpy as np

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

How to create arry function in python?

# Creating ndarrays
#The easiest way to create an array is 
to use the array function
data1 = [67.5801]
data1
[6, 7.5, 8, 0, 1]
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. ])

How to create equal lengh list function in python?

#Nested sequences, like a list of equal-length
 lists, will be converted 
into a multidimensional array:
data2 = [[1234], [5678]]
arr2 = np.array(data2)
arr2
array([[1, 2, 3, 4], [5, 6, 7, 8]])
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

How to create zero matrix in python?

np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros((36)) 
array([[0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.]])
np.empty((232))
array([[[4.669867e-310, 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, 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

How to see data type in python?

arr1 = np.array([123], dtype=np.float64)
arr1
array([1., 2., 3.])
arr2 = np.array([123], dtype=np.int32)
arr2
array([1, 2, 3], dtype=int32)
arr1.dtype
dtype('float64')
arr2.dtype
dtype('int32')
arr = np.array([12345])
arr.dtype
dtype('int64')
float_arr = arr.astype(np.float64)
float_arr
array([1., 2., 3., 4., 5.])
float_arr.dtype
dtype('float64')
#Operations between Arrays and Scalars
Arrays are important because they enable you to express
 batch operations on data without writing any for loops. 
This is usually called vectorization. Any arithmetic operations 
between equal-size arrays applies the operation elementwise:
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
arr*arr
array([[ 1., 4., 9.], [16., 25., 36.]])
arr - arr
array([[0., 0., 0.], [0., 0., 0.]])
1 / arr
array([[1. , 0.5 , 0.33333333], [0.25 , 0.2 , 0.16666667]])
arr ** 0.5
array([[1. , 1.41421356, 1.73205081], [2. , 2.23606798, 2.44948974]])
Basic Indexing and Slicing *NumPy array 
indexing is a rich topic, as there are many ways
 you may want to select a subset of your data or 
individual elements. One-dimensional arrays are 
simple; on the surface they act similarly to Python lists: *

How to see individual element in python?

arr = np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[5]
5arr[5:8]
array([5, 6, 7])
arr[5:8] = 12
arr
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
arr_slice = arr[5:8]

arr_slice[1] = 12345
arr
array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 9])
arr_slice[:] = 64
arr
array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 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

For more updates of numpy exercise


Post a Comment

0 Comments