Você está na página 1de 7

Creating a 3*3 array

Description
Create a 3*3 array using list_1 = [1,2,3] list_2 = [4,5,6] list_3 = [7,8,9]
Execution time limit

5 seconds

import ast,sys

input_str = sys.stdin.read()

input_list = ast.literal_eval(input_str)

list_1 = input_list[0]

list_2 = input_list[1]

list_3 = input_list[2]

import numpy as np

array_1 = np.array([list_1,list_2,list_3])

print(array_1)

All sample testcases passed! (1/1)


Testcase #1

Status
Passed
Execution time
0.48s
CPU
0s
Memory
23MB
Description
Testcase passed! The solution's output matches the expected output.

Input

[[1,2,3],[4,5,6],[7,8,9]]

Solution output

[[1 2 3]
[4 5 6]
[7 8 9]]

Expected output

[[1 2 3]
[4 5 6]
[7 8 9]]

import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
list_1 = input_list[0]
list_2 = input_list[1]

import numpy as np
array_1 = np.array(list_1)
array_2 = np.array(list_2)
array_3 = array_1*array_2

print(list(array_3))

heck

All sample testcases passed! (1/1)


Testcase #1

Status
Passed
Execution time
0.53s
CPU
0s
Memory
22MB
Description
Testcase passed! The solution's output matches the expected output.

Input

[[2,3,4,5],[7,8,9,6]]

Solution output

[14, 24, 36, 30]

Expected output

[14, 24, 36, 30]

import ast,sys
input_str = sys.stdin.read()
input_tuple = ast.literal_eval(input_str)
list_1 = input_tuple[0]
tuple_1 = input_tuple[1]

import numpy as np
array_1 = np.array(list_1)
array_2 = np.array(tuple_1)

print(array_1)
print(array_2)

Description
Create an array of all ones of size 3*3 having integer values.
Hint: Use dtype to specify integer
import numpy as np
array_ones = np.ones((3,3),dtype=int)

print(array_ones)

All sample testcases passed! (1/1)


Testcase #1

Status
Passed
Execution time
0.51s
CPU
0s
Memory
22MB
Description
Testcase passed! The solution's output matches the expected output.

Solution output

[[1 1 1]
[1 1 1]
[1 1 1]]

Expected output

[[1 1 1]
[1 1 1]
[1 1 1]]

Description
Create an array of first 10 multiples of 5 using the 'arange' function.

import numpy as np
array_multipleof5 =np.arange(5,55,5)

print(array_multipleof5)

It is helpful to inspect the structure of NumPy arrays, especially while working


with large arrays. Some attributes of NumPy arrays are:

shape: Shape of array (n x m)


dtype: data type (int, float etc.)
ndim: Number of dimensions (or axes)
itemsize: Memory used by each array element in bytes

2D Array
Description
Create an array using list list_1 = [10,11,12,13] and list_2 = [15,12,13,14] and
print the shape and dimension of the array created.
import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
list_1 = input_list[0]
list_2 = input_list[1]

import numpy as np
array_1 = np.array([list_1,list_2])

print(array_1.shape)
print(array_1.ndim)

All sample testcases passed! (1/1)


Testcase #1

Status
Passed
Execution time
0.47s
CPU
0s
Memory
21MB
Description
Testcase passed! The solution's output matches the expected output.

Input

[[10,11,12,13],[15,12,13,14]]

Solution output

(2, 4)
2

Expected output

(2, 4)
2

2D Array
Description
From a 2D array extract all the rows of the 2 column.
Hint: 2 column will have index value as 1.
Execution time limit

import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
import numpy as np
array_2d =np.array(input_list)

print(array_2d[:,1])
All sample testcases passed! (1/1)
Testcase #1

Status
Passed
Execution time
0.51s
CPU
0s
Memory
22MB
Description
Testcase passed! The solution's output matches the expected output.

Input

[[5,6,7],[7,6,5],[0,8,7]]

Solution output

[6 6 8]

Expected output

[6 6 8]

You'll often work with extremely large datasets, and thus it is important to point
for you to understand how much computation time (and memory) you can save using
NumPy, compared to standard python lists.

Let's compare the computation times of arrays and lists for a simple task of
calculating the element-wise product of numbers.

In this case, NumPy is an order of magnitude faster than lists. This is with arrays
of size in millions, but you may work on much larger arrays of sizes in order of
billions. Then, the difference is even larger.

Some reasons for such difference in speed are:

NumPy is written in C, which is basically being executed behind the scenes


NumPy arrays are more compact than lists, i.e. they take much lesser storage
space than lists

Horizontally stack two arrays using hstack.


import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
list_1 = input_list[0:2]
list_2 = input_list[2:4]
import numpy as np
array_1 = np.array(list_1)
array_2 = np.array(list_2)

array_3 = np.hstack((array_1,array_2))

print(array_3)

Given an array, 'array_3' divide each element with 5.


Hint: Create a vectorized function, then apply it to the array_1

import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
list_1 = input_list[0:2]
list_2 = input_list[2:4]
import numpy as np
array_1 = np.array(list_1)
array_2 = np.array(list_2)
array_3 =np.hstack((array_1,array_2))

function = np.vectorize(lambda x: x/5)

print(function(array_3))

Find the inverse, eigenvalues, determinants of a given matrix 'array_1'.

import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
list_1 = input_list[0]
list_2 = input_list[1]
list_3 = input_list[2]

import numpy as np
array_1 = np.array([list_1,list_2,list_3])
eigen =np.linalg.eig(array_1)
inv = np.linalg.inv(array_1)
det = np.linalg.det(array_1)
print(eigen)
print(inv)
print(det)

Create a series using list = [6,7,8,9,2,3,4,5] and print the output series as the
square of each number in the list.
Hint: If input series = 1,2,3 the output series should be 1,4,25
Hint: First create the series and then using apply and lambda find the output
series.

import numpy as np

import pandas as pd
series_1 = pd.Series([6,7, 8, 9, 2,3,4,5])

series_2 = series_1**2

print(series_1)

print(series_2)

Given a dataframe 'd1' use the following commands and analyse the result.

describe()
columns
shape

import numpy as np
import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
print(df.describe())
print(df.columns)
print(df.shape)

Set Index in Dataframe


Description
Using set_index command set the column 'X' as the index of the dataset and then
print the head of the dataset.
Hint: Use inplace = False
import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
df_2 = df.set_index('X', inplace = False)
print(df_2.head())

Sorting Dataframes
Description
Sort the dataframe on 'month' and 'day' in ascending order in the dataframe 'df'.

import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
df_2 =df.sort_values(by=['month', 'day'], ascending = True)
print(df_2.head(20))

Você também pode gostar