numpy pyhton basic concept

Python lists offer a few advantages when representing data:

  • lists can contain mixed types

  • lists can shrink and grow dynamically

Using Python lists to represent and work with data also has a few key disadvantages:

  • to support their flexibility, lists tend to consume lots of memory

  • they struggle to work with medium and larger sized datasets

image

Create a vector from the list [10, 20, 30].

  • Assign the result to the variable vector.

Create a matrix from the list of lists [[5, 10, 15], [20, 25, 30], [35, 40, 45]].

  • Assign the result to the variable matrix.
image

Import numpy and assign to the alias np.

Assign the shape of vector to vector_shape.

Assign the shape of matrix to matrix_shape.

Display both vector_shape and matrix_shape using the print()function.

image

Use the numpy.genfromtxt() function to read "world_alcohol.csv"into a NumPy array namedworld_alcohol.

Use the type() and print() functions to display the type for world_alcohol.

image

Assign the data type of world_alcohol to the variable world_alcohol_dtype.

Display world_alcohol_dtype using the print() function.

image

When reading in world_alcohol.csv using numpy.genfromtxt():

  • Use the "U75" data type

  • Skip the first line in the dataset

  • Use the comma delimiter.

Assign the result to world_alcohol.

Use theprint() function to display world_alcohol.

image

Assign the amount of alcohol Uruguayans drank in other beverages per capita in 1986 to uruguay_other_1986. This is the second row and fifth column.

Assign the country in the third row to third_country. Country is the third column.

image

Assign the whole third column from world_alcohol to the variable countries.

Assign the whole fifth column from world_alcohol to the variable alcohol_consumption.

image


Assign the whole third column from world_alcohol to the variable countries.

Assign the whole fifth column from world_alcohol to the variable alcohol_consumption.

image

Assign all the rows and the first 2 columns of world_alcohol to first_two_columns.

Assign the first 10 rows and the first column of world_alcohol to first_ten_years.

Assign the first 10 rows and all of the columns of world_alcohol to first_ten_rows.

image

Assign the first 20 rows of the columns at index 1 and 2 of world_alcohol to first_twenty_regions.

image

你可能感兴趣的:(numpy pyhton basic concept)