0% found this document useful (0 votes)
1 views

python

Uploaded by

youssef mahmoud
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

python

Uploaded by

youssef mahmoud
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Data analysis with python

Numpy:
NumPy is a powerful library in Python used for numerical computations. It
provides a high-performance multidimensional array object, known as
`numpy.ndarray`, as well as tools for working with these arrays.

Here are the key differences between NumPy arrays and Python lists:

### 1. **Performance**
- **NumPy Arrays**: They are much faster than Python lists, especially for large
data sets. NumPy is implemented in C, which allows for efficient memory
management and optimized performance.
- **Python Lists**: They are slower because they are implemented in Python,
which is an interpreted language. Each element in a list is a complete Python
object, which adds overhead.

### 2. **Data Types**


- **NumPy Arrays**: They require that all elements be of the same data type
(e.g., all integers, all floats). This uniformity allows NumPy to optimize storage and
computation.
- **Python Lists**: They can hold elements of different data types (e.g.,
integers, strings, and floats in the same list). This flexibility comes at the cost of
performance.

### 3. **Memory Efficiency**


- **NumPy Arrays**: They are more memory-efficient because they store data
in contiguous blocks of memory, leading to faster access and reduced overhead.
- **Python Lists**: They store elements as references to Python objects, which
can lead to increased memory usage.

### 4. **Functionality**
- **NumPy Arrays**: NumPy provides a vast range of mathematical functions
that can be applied to arrays, enabling complex operations like matrix
multiplication, statistical calculations, Fourier transforms, etc. These operations
are often vectorized, meaning they are applied to entire arrays at once, rather
than element by element.
- **Python Lists**: Python lists do not have built-in support for such operations.
You would need to use loops or list comprehensions to perform similar
operations, which can be slower and more cumbersome.

### 5. **Dimensionality**
- **NumPy Arrays**: They can be multi-dimensional (e.g., 2D arrays, 3D arrays),
making them ideal for representing matrices, tensors, and higher-dimensional
data structures.
- **Python Lists**: They are essentially one-dimensional. You can create lists of
lists to simulate multi-dimensional arrays, but this approach is not as efficient or
convenient as using NumPy arrays.

### 7. **Advanced Indexing**


- **NumPy Arrays**: NumPy provides advanced indexing capabilities, including
slicing, masking, and fancy indexing, which allows for more sophisticated data
manipulation.
- **Python Lists**: Lists support basic indexing and slicing but lack the advanced
indexing features of NumPy.

### Summary
- **Use Python lists** when you need a flexible container to store elements of
various types or when working with small datasets.
- **Use NumPy arrays** when you need efficient storage, fast computation, or
when working with large datasets, especially for numerical and scientific
applications.

# Import the numpy package as np


import numpy as np

baseball = [180, 215, 210, 210, 188, 176, 209, 200]

# Create a numpy array from baseball: np_baseball


np_baseball = np.array(baseball)

# Print out type of np_baseball


print(type(np_baseball))

# Import numpy
import numpy as np

# Create a numpy array from height_in: np_height_in


np_height_in = np.array(height_in)

# Print out np_height_in


print(np_height_in)

# Convert np_height_in to m: np_height_m


np_height_m= np_height_in * 0.0254

# Print np_height_m
print(np_height_m)

True is converted to 1, False is converted to 0.


matplotlib:

You might also like