Muennighoff/xP3x-sample huggingface dataset:
inputs: As you can see, the chunkshape has been changed, which has also affected the memory size of the compressed data array. We have indeed reduced the number of chunks in the dataset, which reduces the number of data to store. This modification can also improve or deteriorate the I/O speed of access to your data array in the dataset. The reader is once again refered to dedicated documents to know more ion this matter: here and here.
Compression data and setting chunkshape upon creation of data items
Until here we have only modified the compression settings of already existing data items. In this process, the data items are replaced by the new compressed version of the data, which is a costly operation. For this reason, if they are known in advance, it is best to apply the compression filters and appropriate chunkshape when creating the data item.
If you have read through all the tutorials of this user guide, you should know all the method that allow to create data items in your datasets, like add_data_array, add_field, ædd_mesh... All of these methods accept the two arguments chunkshape and compression_options, that work exaclty as for the set_chunkshape_and_compression or set_nodes_compression_chunkshape methods. You hence use them to create your data items directly with the appropriate compression settings.
Let us see an example. We will get an array from our dataset, and try to recreate it with a new name and some data compression:

targets: # removing dataset to recreate a copy
del data
# creating a copy of the dataset to try out lossy compression methods
data = SD.copy_sample(src_sample_file=dataset_file, dst_sample_file='Test_compression', autodelete=True,
                      get_object=True, overwrite=True)

# getting the `orientation_map` array
array = data['Amitex_stress_1']

# create a new field for the CellData image group with the `orientation_map` array and add compression settings
compression_options = {'complib':'zlib', 'complevel':1, 'shuffle':True,  'least_significant_digit':2,
                       'normalization':'standard'}
new_cshape = (10,10,10,3)

# Add data array as field of the CellData Image Group
data.add_field(gridname='CellData', fieldname='test_compression', indexname='testC', array=array,
              chunkshape=new_cshape, compression_options=compression_options, replace=True)

# Check size and settings of new field
data.print_node_info('testC')
data.get_node_disk_size('testC')
data.print_node_compression_info('testC')
inputs: A behaviour profile looks like a nested list.  Entries are of the form profile[player][infoset][action].

targets: result[0]

result[0][g.players["Alice"]]

result[0][g.players["Bob"]]
inputs: %%writefile Snakefile
rule all:
    input: expand('original_data/dataset{id}.nix', id=range(10))
        
rule generate_data:
    output: 'original_data/dataset{id,[0-9]}.nix'
    shell: 'python generate_poisson.py {output}'
Given the above code, generate some markdown instructions for it.

targets: 
The first rule in the workflow knows how to utilize the script to generate 10 new datasets. We limit the ids of the generated datasets from 0 to 9 to be able to use higher ids for different datasets.
inputs: "<a name="kaggle"></a>
Kaggle Challenge Data
The Otto Group is one of the world’s biggest e-commerce companies, A consistent analysis of the performance of products is crucial. However, due to diverse global infrastructure, many identical products get classified differently.
For this competition, we have provided a dataset with 93 features for more than 200,000 products. The objective is to build a predictive model which is able to distinguish between our main product categories. 
Each row corresponds to a single product. There are a total of 93 numerical features, which represent counts of different events. All features have been obfuscated and will not be defined any further.
https://www.kaggle.com/c/otto-group-product-classification-challenge/data
For this section we will use the Kaggle Otto Group Challenge Data. You will find these data in
data/kaggle_ottogroup/ folder.
Logistic Regression
This algorithm has nothing to do with the canonical linear regression, but it is an algorithm that allows us to solve problems of classification(supervised learning). 
In fact, to estimate the dependent variable, now we make use of the so-called logistic function or sigmoid. 
It is precisely because of this feature we call this algorithm logistic regression.

Data Preparation"
Please write code following the instructions in jupyter notebook style.

targets: 
from kaggle_data import load_data, preprocess_data, preprocess_labels

X_train, labels = load_data('data/kaggle_ottogroup/train.csv', train=True)
X_train, scaler = preprocess_data(X_train)
Y_train, encoder = preprocess_labels(labels)

X_test, ids = load_data('data/kaggle_ottogroup/test.csv', train=False)

X_test, _ = preprocess_data(X_test, scaler)

nb_classes = Y_train.shape[1]
print(nb_classes, 'classes')

dims = X_train.shape[1]
print(dims, 'dims')

np.unique(labels)
inputs: # 创建元组，用小括号

t = ('Tom', 'Jerry', 'Mary')
print(t)

# 访问元组的元素

print(t[1])

# 元组创建后是不能修改的

# 会报错
t.append('Someone')

# 元组创建后是不能修改的

# 像列表一样去定义值也会报错
# 'tuple' object does not support item assignment
t[1] = 'aaa'

# 看看 tuple 有什么方法，你会发现很少

print(dir(tuple))

# 创建复杂一点的元组

t1  = ['A', 'B', 'C']
t2 =(t1, 100, 200)
print(t2)

# 变通的实现"可变"元组内容

t1  = ['A', 'B', 'C']
t2 =(t1, 100, 200)
print(t1)
print(t2)

# tuple的每个元素，指向永远不变，但指向的元素本身是可变的
t1.append('D')

print(t1)
print(t2)  

# 创建只有1个元素的元组

# 下面这样是不行的
t = (1)
# l成了一个整数，因为这里的括号有歧义，被认作数学计算里的小括号
print(type(t))  

# 1个元素的元组必须加逗号来消除歧义
t = (1,)
print(type(t))
print(t)
Given the above code, generate some markdown instructions for it.

targets: 
元组Tuple 用法
Tuple 也是一种有序列表，在存储数据方面和列表很相似，为了区分，我们叫它元组。
Tuple 一旦内容存储后，就不能修改；这样的好处是数据很安全。
应用范围：在我们需要使用列表功能的时候，但是又不需要改变这个列表的内容，用元组 Tuple 功能会很安全，不用担心程序中不小心修改了其内容。Python 在向函数传递多个参数的时候，就是采用元组，保证参数在被调用的过程中的安全。
inputs: # Code Block 7

for i in range(nt):
    mg["node"]["topographic__elevation"][mg.core_nodes] += uplift_per_step
    lin_diffuse.run_one_step(dt)
    time_counter += dt

    # All landscape evolution is the first two lines of loop.
    # Below is simply for plotting the topography halfway through the run
    if i == int(nt // 2):
        figure(1)
        imshow_grid(mg, "topographic__elevation")
        title("topography at time %s, with D = %s" % (time_counter, D))
        figure(2)
        elev_rast = mg.node_vector_to_raster(mg.at_node["topographic__elevation"])
        plot(ys_grid, elev_rast[:, 2], "k-", label="model")
        plot(ys, zs, "g--", label="analytical solution - SS")
        plot(ys, zs * 0.75, "b--", label="75% of analytical solution")
        plot(ys, zs * 0.5, "r--", label="50% of analytical solution")
        xlabel("horizontal distance (m)")
        ylabel("vertical distance (m)")
        legend(loc="lower center")
        title("topographic__elevation at time %s, with D = %s" % (time_counter, D))
Given the above code, generate some markdown instructions for it.

targets: 
Now we are ready to evolve the landscape and compare it to the steady state solution.
Below is the time loop that does all the calculations.
inputs: Below I'm running images through the VGG network in batches.

targets: # Set the batch size higher if you can fit in in your GPU memory
batch_size = 100
codes_list = []
labels = []
batch = []

codes = None
vgg = vgg16.Vgg16()
input_ = tf.placeholder(tf.float32, [None, 224, 224, 3])
with tf.name_scope("content_vgg"):
    vgg.build(input_)
        

with tf.Session() as sess:
    for each in classes:
        print("Starting {} images".format(each))
        class_path = data_dir + each
        files = os.listdir(class_path)
        for ii, file in enumerate(files, 1):
            # Add images to the current batch
            # utils.load_image crops the input images for us, from the center
            img = utils.load_image(os.path.join(class_path, file))
            batch.append(img.reshape((1, 224, 224, 3)))
            labels.append(each)
            
            # Running the batch through the network to get the codes
            if ii % batch_size == 0 or ii == len(files):
                
                # Image batch to pass to VGG network
                images = np.concatenate(batch)
                
                feed_dict = {input_: images}
                codes_batch =  sess.run(vgg.relu6, feed_dict=feed_dict)
                
                # Here I'm building an array of the codes
                if codes is None:
                    codes = codes_batch
                else:
                    codes = np.concatenate((codes, codes_batch))
                
                # Reset to start building the next batch
                batch = []
                print('{} images processed'.format(ii))

# write codes to file
with open('codes', 'w') as f:
    codes.tofile(f)
    
# write labels to file
import csv
with open('labels', 'w') as f:
    writer = csv.writer(f, delimiter='\n')
    writer.writerow(labels)
inputs: Aplicação do PageRank na Base da Dados Real
Após o teste, vamos aplicar o algoritmo de PageRank na base de dados de referências de páginas da web (arquivo web-Google.txt) encontrada no site da Stanford.

targets: diretorio_base = os.path.join('data')
caminho = os.path.join('web-Google.txt')
arquivo = os.path.join(diretorio_base, caminho)

linksGoogle = sc.textFile(arquivo).filter(lambda x: "#" not in x).map(lambda x: x.split("\t"))

linksAgrupados = linksGoogle.groupByKey().cache()

ranks = linksAgrupados.map(lambda url_agrupados: (url_agrupados[0], 1.0))

for x in range(1,8):
    # Adiciona ranks inicializados com 1.0 na posição [1][1] da matriz
    agrupaIdLinkComRank = linksAgrupados.join(ranks)\
                .flatMap(lambda url_rank: atualizaRank(url_rank[1][0], url_rank[1][1]))
    # Soma os valores com o mesmo id e adiciona o fator de normalização
    ranks = agrupaIdLinkComRank.reduceByKey(add)\
                                .mapValues(lambda rankFatorD: (rankFatorD * 0.85) + 0.15)
inputs: I am working on the file "Data Visualization/Seaborn/Seaborn Practice - Titanic Data.ipynb".
The first task is:
Two histograms ploted using FacetGrid based on age and  sex
Can you write Python code for it?

targets: 
g = sns.FacetGrid(data=titanic,col='sex')
g.map(plt.hist,'age')
inputs: <a href="http://spark.apache.org/docs/1.2.0/api/python/pyspark.html#pyspark.RDD.sample">
<img align=left src="files/images/pyspark-page10.svg" width=500 height=500 />
</a>

targets: # sample
x = sc.parallelize(range(7))
ylist = [x.sample(withReplacement=False, fraction=0.5) for i in range(5)] # call 'sample' 5 times
print('x = ' + str(x.collect()))
for cnt,y in zip(range(len(ylist)), ylist):
    print('sample:' + str(cnt) + ' y = ' +  str(y.collect()))
inputs: "Apply Imputer"
Please write code following the instructions in jupyter notebook style.

targets: 
# Apply the imputer to the df dataset
imputed_df = mean_imputer.transform(df.values)
inputs: <hr />
Systematic shifts we're going to study:

targets: cfg.add_shift("nominal", 1)
cfg.add_shift("lumi_up", 2, type="rate")
cfg.add_shift("lumi_down", 3, type="rate")
cfg.add_shift("scale_up", 4, type="shape")
cfg.add_shift("scale_down", 5, type="shape")
print(len(cfg.shifts))
inputs: "According to this analysis, Pedro Martinez is the highest paid Hall of Famer of all time.
4. Who is the lowest paid Hall of Famer of all-time?
I'll conclude by showing the lowest paid Hall of Famer of all-time by both metrics. I'll keep K fixed at 10 for brevity's sake."
Please write code following the instructions in jupyter notebook style.

targets: 
for key in sorted(d_larg,key=d_larg.get)[:5]:
    print key,' '.join(pid_to_name[key]),d_larg[key]

for key in sorted(d_ave,key=d_ave.get)[:5]:
    print key,' '.join(pid_to_name[key]),d_ave[key]
inputs: #reload(pynoddy.output)
geophys = pynoddy.output.NoddyGeophysics(output)

geophys.grv_data


fig = plt.figure(figsize = (5,5))
ax = fig.add_subplot(111)
# imshow(geophys.grv_data, cmap = 'jet')
# define contour levels
levels = np.arange(322,344,1)
cf = ax.contourf(geophys.grv_data, levels, cmap = 'gray', vmin = 324, vmax = 342)
cbar = plt.colorbar(cf, orientation = 'horizontal')
# print levels
Given the above code, generate some markdown instructions for it.

targets: 
We now get two files for the caluclated fields: '.grv' for gravity, and '.mag' for the magnetic field. We can extract the information of these files for visualisation and further processing in python:
inputs: # Create new variable in the dataframe
physicsOnlyDataFrame['AgeAwarded'] = physicsOnlyDataFrame.Year - physicsOnlyDataFrame.BirthYear
physicsOnlyDataFrame.head(5)
Given the above code, generate some markdown instructions for it.

targets: 
We have now successfully plotted the histogram of just the physics prizes after applying our pre-selection.
Calculations, Scatter Plots and 2D Histogram
Adding New Data to a Data Frame
You will find this section useful for when it comes to creating a Dalitz plot in the particle physics analysis.
We want to see what ages people have been awarded Nobel prizes and measure the spread in the ages.
Then we'll consider if over time people have been getting awarded Nobel prizes earlier or later in their life. 
First we'll need to calculate the age or the winners at the time the prize was awarded based on the Year and Birthdate columns. We create an AgeAwarded variable and add this to the data.
inputs: "Búsqueda en una Serie"
Please write code following the instructions in jupyter notebook style.

targets: 
deportes = {'Capoeira': 'Brasil',
          'Rayuela': 'Chile',
          'Pelota Vasca': 'País Vasco',
          'Béisbol': 'Cuba',
           'Rugby': 'Gales',
           'Golf': 'Escocia',
           'Corrida de Toros': 'España',
           'Sumo': 'Japón'}
s = pd.Series(deportes)
s
inputs: <a id="alos_land_change_load"></a> Load and Clean Data from the Data Cube &#9652;

targets: dataset = dc.load(product = product, platform = platform, 
                  latitude = lat, longitude = lon, 
                  time=time_extents)
inputs: 6. Generator Functions
Generator functions with yield

targets: def myrange(n):
    for i in range(n):
        yield i

max(myrange(5))
inputs: $$k_1 + k_2$$ density element is not null

targets: sum=K[1]+K[2]
sum.texLabel='k_1+k_2'
showDerivatives(f, 3, sum)
inputs: "As we have said, the children of node $c$ are at positions $2c + 1$ and $2c + 2$. 
So, here is the function that returns the children of a node in the queue.
Note that we must be careful to return the right number of children.
A node that has children has always two children, except if its only child is the last element of the queue; check the two following examples:
```
         6                           6                 
       /   \                       /   \               
      11    8                     11    8              
     / \   / \                   / \   /              
    17 19 13 12                 17 19 13               
[6, 11, 8, 17, 19, 13, 12]  [6, 11, 8, 17, 19, 13] 
```"
Please write code following the instructions in jupyter notebook style.

targets: 
def children(pq, p):
    if 2*p + 2 < len(pq):
        return [2*p + 1, 2*p + 2]
    else:
        return [2*p + 1]
inputs: Approach 1
We can now use these three time series and estimate the $\epsilon$ values using approach 1. 
The functions we can be found in:

pytesmo.scaling
pytesmo.metrics

We will use mean-standard deviation scaling. This type of scaling brings the data to the same mean and standard deviation as the reference dataset.

targets: import pytesmo.scaling as scaling
import pytesmo.metrics as metrics

# scale to x as the reference
y_scaled = scaling.mean_std(y, x)
z_scaled = scaling.mean_std(z, x)
plt.plot(coord, x, alpha=0.3, label='x')
plt.plot(coord, y_scaled, alpha=0.3, label='y scaled')
plt.plot(coord, z_scaled, alpha=0.3, label='z scaled')
plt.plot(coord, signal, 'k', label='$\Theta$')
plt.legend()
plt.show()
inputs: A dictionary of sources is return that contains pandas DataFrames listing the fluxes and errors of each componenet. 
NOTE: With proper error propagation, the total error is not always the sqrt of the sum of component errors squared!
Bayesian fitting
Now we will look at the results when a Bayesian fit is performed. 
We set our priors and then sample:

targets: pl_bb.K_1.prior = Log_uniform_prior(lower_bound = 1E-1, upper_bound = 1E2)
pl_bb.index_1.set_uninformative_prior(Uniform_prior)

pl_bb.K_2.prior = Log_uniform_prior(lower_bound = 1E-6, upper_bound = 1E-3)
pl_bb.kT_2.prior = Log_uniform_prior(lower_bound = 1E0, upper_bound = 1E4)



bayes = BayesianAnalysis(model2,data_list)
_=bayes.sample(30,100,500)
inputs: #projection of first sample, on to the first PC 
P11 = np.dot(eig_vecs[:,0], featurematrix_std[0,:]-mean_vec) 
mlP11 = mlPCA.Y[0,0]
SkP11 = SkPC[0,0]

P12 = np.dot(eig_vecs[:,1], featurematrix_std[0,:]-mean_vec) 
mlP12 = mlPCA.Y[0,1]
SkP12 = SkPC[0,1]

P152 = np.dot(eig_vecs[:,1], featurematrix_std[15,:]-mean_vec) 
mlP152 = mlPCA.Y[15,1]
SkP152 = SkPC[15,1]

print(P11, mlP11, SkP11)
print(P12, mlP12, SkP12)
print(P152, mlP152, SkP152)

print(mlloadings[0,7])
print(eig_vecs[0,7])
Given the above code, generate some markdown instructions for it.

targets: 
Check that all three give similar results
inputs: "2- Upsampling through transposed convolution

Reverse convolution in which forward and backward passes are swapped
aka deconvolution
Differentiability retained and training exactly the same as before
https://dspguru.com/dsp/faqs/multirate/interpolation/
https://github.com/vdumoulin/conv_arithmetic
<img src="https://d17h27t6h515a5.cloudfront.net/topher/2017/October/59d8670c_transposed-conv/transposed-conv.png">"
Please write code following the instructions in jupyter notebook style.

targets: 
def upsample(x):
    """
    Apply a two times upsample on x and return the result.
    :x: 4-Rank Tensor
    :return: TF Operation
    """
    # TODO: Use `tf.layers.conv2d_transpose`
    
    return tf.layers.conv2d_transpose(x,
                                      x.shape[3],
                                      kernel_size=(3, 3),
                                      strides=2,
                                      padding='SAME')


x = tf.constant(np.random.randn(1, 4, 4, 3), dtype=tf.float32)
conv = upsample(x)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    result = sess.run(conv)

    print('Input Shape: {}'.format(x.get_shape()))
    print('Output Shape: {}'.format(result.shape))
inputs: "3.3 Counting Other Things"
Please write code following the instructions in jupyter notebook style.

targets: 
fdist = FreqDist(len(w) for w in text1)
fdist

print(fdist.most_common())
print(fdist.max())
print(fdist[3])
print(fdist.freq(3))

tricky = sorted(w for w in set(text2) if 'cie' in w or 'cei' in w)
for word in tricky:
    print(word, end=' ')
inputs: Appendix: stack vs pivot
Pivot can be implemented by using set_index and unstack.

targets: test = pd.DataFrame({'foo':["one"] * 3 + ["two"] * 3, 'bar': list("ABC")*2, 'baz': list(range(6))})

test

test.pivot('foo', 'bar', 'baz')

test.set_index(['foo','bar']).unstack()['baz']
inputs: # Produce learning curves for varying training set sizes and maximum depths
vs.ModelLearning(features, prices)
Given the above code, generate some markdown instructions for it.

targets: 
Question 3 - Training and Testing
What is the benefit to splitting a dataset into some ratio of training and testing subsets for a learning algorithm?
Hint: What could go wrong with not having a way to test your model?
Answer:   the importance of split the data randomly, is by splitting the data the bias caused by the entire sample is removed in some way, to validate the quality of the model with the set of test data. And quoting from the course "By separating training and testing sets and graphing performance on each separately, we can get a better idea of how well the model can generalize to unseen data."

Analyzing Model Performance
In this third section of the project, you'll take a look at several models' learning and testing performances on various subsets of training data. Additionally, you'll investigate one particular algorithm with an increasing 'max_depth' parameter on the full training set to observe how model complexity affects performance. Graphing your model's performance based on varying criteria can be beneficial in the analysis process, such as visualizing behavior that may not have been apparent from the results alone.
Learning Curves
The following code cell produces four graphs for a decision tree model with different maximum depths. Each graph visualizes the learning curves of the model for both training and testing as the size of the training set is increased. Note that the shaded region of a learning curve denotes the uncertainty of that curve (measured as the standard deviation). The model is scored on both the training and testing sets using R<sup>2</sup>, the coefficient of determination.  
Run the code cell below and use these graphs to answer the following question.
inputs: "... and quickly plotting it via its matplotlib interface is pretty straightforward:"
Please write code following the instructions in jupyter notebook style.

targets: 
pl.figure(figsize=(12,7))
rrsUnc412.plot.hist(bins=50, range=(0,5e-4), histtype='stepfilled', label='uncertainty at 412nm',
                normed=True);
rrsUncGreen.plot.hist(bins=50, range=(0,5e-4),histtype='stepfilled', alpha=0.3,
                  label='uncertainty at all greenish bands: 510 & 555', normed=True)
pl.legend();
pl.title('Histogram of Rrs Uncertainty',fontsize=18);
pl.ylabel('freq', fontsize=16)
pl.xlabel(r'$sr^{-1}$', fontsize=16 );
inputs: <span id="water_detection_with_wofs_extents">Get the Maximum Extents of the Cube &#9652;</span>
With the platform and product, we can get the rest of the metadata. This includes the resolution of a pixel, the latitude/longitude extents, and the minimum and maximum dates available of the chosen platform/product combination.

targets: full_lat, full_lon, min_max_dates = get_overlapping_area(api, platforms, products)

print("Available Latitude Extents:", full_lat)
print("Available Longitude Extents:", full_lon)
print("Available Time Extents:", np.vectorize(dt_to_str)(min_max_dates))

display_map(latitude=full_lat, longitude=full_lon)
inputs: Comparision with Stata
Note, as indicated, to have comparable results running PCA in Python with other statistical programs, such as Stata, we will want to standardize the data. How do these results compare:
```stata
import delimited "iris_download.csv", clear
pca sepal_len sepal_wid petal_len petal_wid
Principal components/correlation                  Number of obs    =       150
                                                  Number of comp.  =         4
                                                  Trace            =         4
    Rotation: (unrotated = principal)             Rho              =    1.0000
--------------------------------------------------------------------------
   Component |   Eigenvalue   Difference         Proportion   Cumulative
-------------+------------------------------------------------------------
       Comp1 |      2.91082       1.9896             0.7277       0.7277
       Comp2 |      .921221      .773868             0.2303       0.9580
       Comp3 |      .147353      .126746             0.0368       0.9948
       Comp4 |     .0206077            .             0.0052       1.0000
--------------------------------------------------------------------------

Principal components (eigenvectors) 
--------------------------------------------------------------------
    Variable |    Comp1     Comp2     Comp3     Comp4 | Unexplained 
-------------+----------------------------------------+-------------
   sepal_len |   0.5224    0.3723   -0.7210   -0.2620 |           0 
   sepal_wid |  -0.2634    0.9256    0.2420    0.1241 |           0 
   petal_len |   0.5813    0.0211    0.1409    0.8012 |           0 
   petal_wid |   0.5656    0.0654    0.6338   -0.5235 |           0 
--------------------------------------------------------------------

```
Standardized PCA Plot

targets: pca_scatter(0, 1)
inputs: #setup the problem
prob = LpProblem('warehouse_routing',LpMinimize)

#add the objective
prob += objective()

#add all the constraints
for c in constraints():
    prob+=c
Given the above code, generate some markdown instructions for it.

targets: 
Finally ready to create the problem, add the objective, and add the constraints
inputs: 3. os模块

targets: import os
import string

def replace(file, search_for, replace_with):
    back = os.path.splitext(file)[0] + ".bak"
    temp = os.path.splitext(file)[0] + ".tmp"
    
    try:
        os.remove(temp)
    except os.error:
        pass
    
    fi = open(file)
    fo = open(temp, "w")
    
    for s in fi.readlines():
        fo.write(string.replace(s, search_for, replace_with))
    
    fi.close()
    fo.close()
        
    try:
        os.remove(back)
    except os.error:
        pass
    
    os.rename(file, back)
    os.rename(temp, file)

file = "samples/sample.txt"

replace(file, "hello", "tjena")

replace(file, "tjena", "hello")
inputs: "Complete a função pra encontrar o menor (mínimo) valor de uma lista:"
Please write code following the instructions in jupyter notebook style.

targets: 
def minimo(numeros):
    minimo = numeros[0]
    for numero in numeros[1:]:
        if numero < minimo:
            minimo = numero
    return minimo

# Testes para verificar a corretude da função soma
assert minimo([-6473.695, 438.199, 7959.093, 7842.664, 7233.021, -4149.884]) == -6473.695
assert minimo([6505.843, -7075.108]) == -7075.108
assert minimo([9151.244, -4605.586, -2920.8]) == -4605.586
assert minimo([-6586.813, -2264.44, 4460.277]) == -6586.813
assert minimo([1553.196, 1631.333]) == 1553.196
assert minimo([6876.253, 9349.21, -382.322]) == -382.322
assert minimo([9604.147, -378.112, 2574.795, -5597.589, 8751.16]) == -5597.589
assert minimo([2761.954, 4439.859, 9361.367, 884.972]) == 884.972
assert minimo([2989.882, 346.282, 9051.012, 4973.448, 1821.907]) == 346.282
assert minimo([4996.027, -5269.592]) == -5269.592
inputs: Connect to the database

We can use the sqlite3 library to connect to the MIMIC database
Once the connection is established, we'll run a simple SQL query.

targets: # Connect to the MIMIC database
try: 
    conn = psycopg2.connect("dbname='mimic' user='tompollard' host='localhost' password='postgres'")
except: 
    print('meh')

# Create our test query
test_query = """
SELECT subject_id, hadm_id, admittime, dischtime, diagnosis, admission_type, deathtime, discharge_location
FROM mimiciii.admissions;
"""

# Run the query and assign the results to a variable
test = pd.read_sql_query(test_query,conn)

# Display the first few rows
test
inputs: # Import Titanic data (local CSV)
titanic = h2o.import_file("kaggle_titanic.csv")
titanic.head(5)

# Convert 'Survived' and 'Pclass' to categorical values
titanic['Survived'] = titanic['Survived'].asfactor()
titanic['Pclass'] = titanic['Pclass'].asfactor()

titanic['Survived'].table()

titanic['Pclass'].table()

titanic['Sex'].table()

titanic['Age'].hist()

titanic['SibSp'].hist()

titanic['Parch'].hist()

titanic['Fare'].hist()

titanic['Embarked'].table()

# Define features (or predictors) manually
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']

# Split the H2O data frame into training/test sets
# so we can evaluate out-of-bag performance
titanic_split = titanic.split_frame(ratios = [0.8], seed = 1234)

titanic_train = titanic_split[0] # using 80% for training
titanic_test = titanic_split[1]  # using the rest 20% for out-of-bag evaluation

titanic_train.shape

titanic_test.shape
Given the above code, generate some markdown instructions for it.

targets: 
<br>
inputs: <br>
<br>
Next, we perform an eigendecomposition on the covariance matrix:

targets: cov_mat = np.cov(X_std.T)

eig_vals, eig_vecs = np.linalg.eig(cov_mat)

print('Eigenvectors \n%s' %eig_vecs)
print('\nEigenvalues \n%s' %eig_vals)
inputs: Added linewidth and number of bins arguments.  This should get pushed eventually.

targets: def draw_dist(vec, split=None, ax=None, legend=True, colors=None, lw=2, bins=300):
    """
    Draw a smooth distribution from data with an optional splitting factor.
    """
    _, ax = init_ax(ax)
    if split is None:
        split = pd.Series('s', index=vec.index)
        colors = {'s': colors} if colors is not None else None
    for l,v in vec.groupby(split):
        if colors is None:
            smooth_dist(v, bins=bins).plot(label=l, lw=lw, ax=ax)
        else:
            smooth_dist(v, bins=bins).plot(label=l, lw=lw, ax=ax, color=colors[l])
    if legend and len(split.unique()) > 1:
        ax.legend(loc='upper left', frameon=False)
inputs: <a id="watches"></a>
Controlling what the tape watches
The default behavior is to record all operations after accessing a trainable tf.Variable. The reasons for this are:

The tape needs to know which operations to record in the forward pass to calculate the gradients in the backwards pass.
The tape holds references to intermediate outputs, so you don't want to record unnecessary operations.
The most common use case involves calculating the gradient of a loss with respect to all a model's trainable variables.

For example, the following fails to calculate a gradient because the tf.Tensor is not "watched" by default, and the tf.Variable is not trainable:

targets: # A trainable variable
x0 = tf.Variable(3.0, name='x0')
# Not trainable
x1 = tf.Variable(3.0, name='x1', trainable=False)
# Not a Variable: A variable + tensor returns a tensor.
x2 = tf.Variable(2.0, name='x2') + 1.0
# Not a variable
x3 = tf.constant(3.0, name='x3')

with tf.GradientTape() as tape:
  y = (x0**2) + (x1**2) + (x2**2)

grad = tape.gradient(y, [x0, x1, x2, x3])

for g in grad:
  print(g)
inputs: I am working on the file "Machine_Learning_in_Python.ipynb".
The first task is:
Let's poke around and see what is in the data set.
Can you write Python code for it?

targets: 
data_set.keys()

data_set.data
inputs: # where(condition, value if true, value if false)
where(population < population.mean('time'), -population, population)
Given the above code, generate some markdown instructions for it.

targets: 
See with_total for more details and examples.
where
The where function can be used to apply some computation depending on a condition:
inputs: I am working on the file "Chapter 3 - Conditions.ipynb".
The first task is:
Finally we can use not to test for conditions that are not true.
Can you write Python code for it?

targets: 
if ("z" not in word):
    print("z is not in " + word)
inputs: I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Unpopular Articles
At the very least, we want our software to predict whether an article will be unpopular.  In this case, we define Unpopular as belonging in the bottom 25% in terms of shares amongst articles on a similar platform.
Can you write Python code for it?

targets: 
# Rank features by importance
feature_selection_model = ExtraTreesClassifier().fit(df[features], df['unpopular'])
feature_importance=feature_selection_model.feature_importances_
importance_matrix=np.array([features,list(feature_importance)]).T

def sortkey(s):
    return s[1]

sort=zip(features,list(feature_importance))

# Extract top 10 important features
f = pd.DataFrame(sorted(sort,key=sortkey,reverse=True),columns=['variables','importance'])[:10]
f

features2=f['variables']

#split data into two parts
np.random.seed(0)
x_train, x_test, y_train, y_test = train_test_split(df[features2], df.unpopular, test_size=0.4, random_state=None)
x_train.shape

# Decision Tree accuracy and time elapsed caculation
#t0=time()
#print "DecisionTree"
#dt = DecisionTreeClassifier(min_samples_split=25,random_state=1)
#clf_dt=dt.fit(x_train,y_train)
#y_predicted_dt = clf_dt.predict(x_test)
#t1=time()

# Observe how decision tree performed on its own.
#print(metrics.classification_report(y_test, y_predicted_dt))
#print "time elapsed: ", t1-t0

# Random Forest 

# Trainc classifier and time elapsed caculation
t2=time()

print "RandomForest"
rf = RandomForestClassifier(n_estimators=100,n_jobs=1)
clf_rf = rf.fit(x_train,y_train)
y_predicted_rf = clf_rf.predict(x_test)
t3=time()

# See how Random forest performed on its own.
print "Acurracy: ", clf_rf.score(x_test,y_test)
print "time elapsed: ", t3-t2

#KNN

from sklearn.neighbors import KNeighborsClassifier

# Determine K by cross-validation.
x_cv_train, x_cv_test, y_cv_train, y_cv_test = train_test_split(x_train, y_train, test_size=0.3, random_state=None)

# We use K-values ranging from 1-10
k=[5,10,15,20,25,30,35,40,45,50]

# Train a model on the training set and use that model to predict on the testing set
predicted_knn=[KNeighborsClassifier(n_neighbors=i).fit(x_cv_train,y_cv_train).predict(x_cv_test) for i in k]

#Compute accuracy on the testing set for each value of k
score_knn=[metrics.accuracy_score(predicted_knn[i],y_cv_test) for i in range(10)]

print score_knn

# Plot...
inputs: # variable assignments
x = 1.0
my_variable = 12.2
Given the above code, generate some markdown instructions for it.

targets: 
Variables and types
Symbol names
Variable names in Python can contain alphanumerical characters a-z, A-Z, 0-9 and some special characters such as _. Normal variable names must start with a letter. 
By convention, variable names start with a lower-case letter, and Class names start with a capital letter. 
In addition, there are a number of Python keywords that cannot be used as variable names. These keywords are:
and, as, assert, break, class, continue, def, del, elif, else, except, 
exec, finally, for, from, global, if, import, in, is, lambda, not, or,
pass, print, raise, return, try, while, with, yield

Assignment
The assignment operator in Python is =. Python is a dynamically typed language, so we do not need to specify the type of a variable when we create one.
Assigning a value to a new variable creates the variable:
inputs: ## Classic output example
## Hello World!  

## Note that Python 3.X requires parentheses around the arguments passed to the print function.  
## Parentheses were not necessary in earlier versions of Python (e.g. 2.7).

print("Hello World!")

## Input example

variable = input('Prompt for input.  Type in anything and hit enter: ')
print( 'Input variable is ' + variable)
Given the above code, generate some markdown instructions for it.

targets: 
Simple Input/Output
inputs: "As of this writing, the L1 h(t) data has a very large dynamic range at low frequencies. We need to remove this before doing anything."
Please write code following the instructions in jupyter notebook style.

targets: 
data_dt=1.e20*data.astype(float64).detrend()
filt=sig.firwin(int(8*srate)-1,9./nyquist,pass_zero=False,window='hann')
data_hp=fir_filter(data_dt,filt)
inputs: I am working on the file "5_root_finding_optimization.ipynb".
The first task is:
Example:
$$f(x) = x - e^{-x}$$
$$f'(x) = 1 + e^{-x}$$
$$x_{k+1} = x_k - \frac{f(x_k)}{f'(x_k)} = x_k - \frac{x_k - e^{-x_k}}{1 + e^{-x_k}}$$
Asymptotic Convergence of Newton's Method
For a simple root (non-multiplicative) - Let $g(x) = x - \frac{f(x)}{f'(x)}$, then
$$x_{k+1} = g(x_k)$$
Definitions of errors and iteration:
$$x_{k+1} = x^ + e_{k+1} ~~~~~ x_k = x^ + e_k$$
General Taylor expansion:
$$x^ + e_{k+1} = g(x^ + e_k) = g(x^) + g'(x^) e_k + \frac{g''(x^*) e_k^2}{2!} + \ldots$$
Note that as before $x^$ and $g(x^)$ cancel:
$$e_{k+1} = g'(x^) e_k + \frac{g''(x^) e_k^2}{2!} + \ldots$$
What about $g'(x^*)$ though:
$$g'(x) = 1 - \frac{f'(x)}{f'(x)} + \frac{f(x)}{f''(x)}$$
which simplifies when evaluated at $x = x^*$ to
$$g'(x^) = \frac{f(x^)}{f''(x^*)} = 0$$
The expansion then simplifies to 
$$e_{k+1} = \frac{g''(x^*) e_k^2}{2!} + \ldots$$
leading to the conclusion that 
$$|e_{k+1}| = \left | \frac{g''(x^*)}{2!} \right | |e_k|^2$$
Newton's method is therefore quadratically convergent where the the constant is controlled by the second derivative.
For a multiple root (e.g. $f(x) = (x-1)^2$) the case is not particularly rosey unfortunately.
Example:
$f(x) = \sin (2 \pi x)$
$$x_{k+1} = x_k - \frac{\sin (2 \pi x)}{2 \pi \cos (2 \pi x)}= x_k - \frac{1}{2 \pi} \tan (2 \pi x)$$
Can you write Python code for it?

targets: 
x = numpy.linspace(0, 2, 1000)
f = lambda x: numpy.sin(2.0 * numpy.pi * x)
f_prime = lambda x: 2.0 * numpy.pi * numpy.cos(2.0 * numpy.pi * x)

fig = plt.figure()
axes = fig.add_subplot(1, 1, 1)
axes.plot(x, f(x),'b')
axes.plot(x, f_prime(x), 'r')
axes.set_xlabel("x")
axes.set_ylabel("y")
axes.set_title("Comparison of $f(x)$ and $f'(x)$")
axes.set_ylim((-2,2))
axes.set_xlim((0,2))
axes.plot(x, numpy.zeros(x.shape), 'k--')

x_k = 0.3
axes.plot([x_k, x_k], [0.0, f(x_k)], 'ko')
axes.plot([x_k, x_k], [0.0, f(x_k)], 'k--')
axes.plot(x, f_prime(x_k) * (x - x_k) + f(x_k), 'k')


x_k = x_k - f(x_k) / f_prime(x_k)
axes.plot([x_k, x_k], [0.0, f(x_k)], 'ko')
axes.plot([x_k, x_k], [0.0, f(x_k)], 'k--')

plt.show()

x = numpy.linspace(0, 2, 1000)
f = lambda x: numpy.sin(2.0 * numpy.pi * x)
x_kp = lambda x: x - 1.0 / (2.0 * numpy.pi) * numpy.tan(2.0 * numpy.pi * x)

fig = plt.figure()
axes = fig.add_subplot(1, 1, 1)
axes.plot(x, f(x),'b')
axes.plot(x, x_kp(x), 'r')
axes.set_xlabel("x")
axes.set_ylabel("y")
axes.set_title("Comparison of $f(x)$ and $f'(x)$")
axes.set_ylim((-2,2))
axes.set_xlim((0,2))
axes.plot(x, numpy.zeros(x.shape), 'k--')

plt.show()
inputs: #eltelt ido
t=[-3.14159265, -2.81089869, -2.48020473, -2.14951076, -1.8188168 ,
       -1.48812284, -1.15742887, -0.82673491, -0.49604095, -0.16534698,
        0.16534698,  0.49604095,  0.82673491,  1.15742887,  1.48812284,
        1.8188168 ,  2.14951076,  2.48020473,  2.81089869,  3.14159265];
#megtett ut
s=[-0.18917675,  0.30733932,  1.10617134,  0.99301702,  0.26214699,
       -0.21526465, -0.99894001, -1.34350261, -1.38845013, -0.00443704,
        0.06863757,  0.33781392,  0.75168117,  1.15622707, -0.13737408,
       -0.34339706, -1.14117694, -1.0584432 , -0.63149239,  0.43437089];
#meresi hiba
err=[0.2, 0.1, 0.1, 0.2, 0.3, 0.1, 0.4, 0.2, 0.4, 0.1, 0.1, 
     0.4, 0.4, 0.5, 0.1, 0.2, 0.1, 0.2, 0.2, 0.2]
Given the above code, generate some markdown instructions for it.

targets: 
Egy példa mérés adatgyűjtéssel, illesztéssel és ábra készítéssel
Induljunk ki egy mérési adatsorból:
eltelt idő [s]|megtett út [m]|mérési hiba [m]
----------:|--------------:|-------------------:
       -3.14159265| -0.18917675|  0.2
       -2.81089869|  0.30733932|  0.1
       -2.48020473|  1.10617134|  0.1
       -2.14951076|  0.99301702|  0.2
       -1.8188168 |  0.26214699|  0.3
       -1.48812284| -0.21526465|  0.1
       -1.15742887| -0.99894001|  0.4
       -0.82673491| -1.34350261|  0.2
       -0.49604095| -1.38845013|  0.4
       -0.16534698| -0.00443704|  0.1
        0.16534698|  0.06863757|  0.1
        0.49604095|  0.33781392|  0.4
        0.82673491|  0.75168117|  0.4
        1.15742887|  1.15622707|  0.5
        1.48812284| -0.13737408|  0.1
        1.8188168 | -0.34339706|  0.2
        2.14951076| -1.14117694|  0.1
        2.48020473| -1.0584432 |  0.2
        2.81089869| -0.63149239|  0.2
        3.14159265|  0.43437089|  0.2
Töltsük be ezeket az adatokat 3 python tömbbe:
inputs: %%sql

--  Select all
SELECT distinct *

-- From the criminals table
FROM criminals

-- Where age is greater than 23
WHERE age = 23
Given the above code, generate some markdown instructions for it.

targets: 
View Rows Where Age Is 23
inputs: "<img src="images/hanford_variables.png">
3. Calculate the basic descriptive statistics on the data"
Please write code following the instructions in jupyter notebook style.

targets: 
df.mean()

df.median()

iqr = df.quantile(q=0.75)- df.quantile(q=0.25)
iqr

UAL= (iqr*1.5) + df.quantile(q=0.75)
UAL

LAL= df.quantile(q=0.25) - (iqr*1.5)  
LAL
inputs: I am working on the file "2DRodFF_1.ipynb".
The first task is:
The result shows that this numerical method does not preserve the Hamiltonian. Therefore, we try now with the implicit midpoint rule, which according to Hairer et al. (2006), p. 247 preserves quadratic invariants
$$N_{n+1} = N_n + \frac{h}{4EI} (Q_n + Q_{n+1})(M_n + M_{n+1})$$
$$Q_{n+1} = Q_n - \frac{h}{4EI} (N_n + N_{n+1})(M_n + M_{n+1})$$
$$M_{n+1} = M_n - \frac{h}{2} (Q_n + Q_{n+1})$$  
Rearranging and dividing the first equation by the second one yields $N_{n+1}^2 + Q_{n+1}^2 = N_n^2 + Q_n^2$ (preservation of the Casimir). Substituting the third into the first shows the preservation of the Hamiltonian.
The drawback is that we end up with an implicit scheme in which the $n+1$ variables can't be isolated. In the next experiment we will advance each step using scipy.optimize.root non-linear solver.
Can you write Python code for it?

targets: 
import scipy.optimize

def implicitMidpoint(Xn1, Xn, h, EI):
    f = np.empty(3)
    f[0] = Xn1[0] - Xn[0] - h*(Xn[1] + Xn1[1])*(Xn[2] + Xn1[2])/4./EI
    f[1] = Xn1[1] - Xn[1] + h*(Xn[0] + Xn1[0])*(Xn[2] + Xn1[2])/4./EI
    f[2] = Xn1[2] - Xn[2] + h*(Xn[1] + Xn1[1])/2.
    return f

sectionForces = []
N0 = F3*np.cos(theta0)
Q0 = -F3*np.sin(theta0)
M0 = 0.
sectionForces.append([N0, Q0, M0])

nEdges = 50
h = 0.1

Xn = np.array([N0, Q0, M0])
for n1 in range(1, nEdges+1):
    eqSystem = lambda Xn1: implicitMidpoint(Xn1, Xn, h, EI) 
    solution = scipy.optimize.root(eqSystem, Xn)
    Xn1 = solution.x
    sectionForces.append(Xn1)
    Xn = Xn1

axialForce = np.array([force[0] for force in sectionForces])
shearForce = np.array([force[1] for force in sectionForces])
bendingMoment = np.array([force[2] for force in sectionForces])
H = bendingMoment**2/2/EI + axialForce

fig = plt.figure(figsize=(9., 9.))

ax = fig.gca(projection='3d')
ax.plot(N3, Q3, M3, color='b')
ax.plot(N3, Q3, -M3, color='b')
ax.scatter(axialForce, shearForce, bendingMoment, color='r')
ax.set_xlabel('$N$')
ax.set_ylabel('$Q$')
ax.set_zlabel('$M$')

fig = plt.figure(figsize=(7., 7.))

ax = fig.gca()
ax.plot(np.linspace(0, h*nEdges, nEdges+1), H)
ax.set_xlabel('$s$')
ax.set_ylabel('$\mathcal{H}$')
inputs: "Create a Cloud Storage bucket
The following steps are required, regardless of your notebook environment.
{TODO: Adjust wording in the first paragraph to fit your use case - explain how your tutorial uses the Cloud Storage bucket. The example below shows how Vertex AI uses the bucket for training.}
When you submit a training job using the Cloud SDK, you upload a Python package
containing your training code to a Cloud Storage bucket. Vertex AI runs
the code from this package. In this tutorial, Vertex AI also saves the
trained model that results from your job in the same bucket. Using this model artifact, you can then
create Vertex AI model and endpoint resources in order to serve
online predictions.
Set the name of your Cloud Storage bucket below. It must be unique across all
Cloud Storage buckets."
Please write code following the instructions in jupyter notebook style.

targets: 
BUCKET_NAME = "[your-bucket-name]"  # @param {type:"string"}
BUCKET_URI = f"gs://{BUCKET_NAME}"

if BUCKET_NAME == "" or BUCKET_NAME is None or BUCKET_NAME == "[your-bucket-name]":
    BUCKET_NAME = PROJECT_ID + "aip-" + TIMESTAMP
    BUCKET_URI = f"gs://{BUCKET_NAME}"
inputs: "Construct a smaller box that will fit inside of the aoi_shape"
Please write code following the instructions in jupyter notebook style.

targets: 
x_diff = maxx - minx
minx2 = minx + x_diff / 5
maxx2 = maxx - x_diff / 5
y_diff = maxy - miny
miny2 = miny + y_diff / 5
maxy2 = maxy - y_diff / 5
smaller_box = shapely.geometry.box(minx2, miny2, maxx2, maxy2)
print(smaller_box.bounds)
smaller_box
inputs: %matplotlib inline
import json
import pandas as pd
import matplotlib.pyplot as plt

tweets_data_path = '/home/nipun/Downloads/Twitter_scrape/Twitter_scrape.txt'

tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
    try:
        tweet = json.loads(line)
        tweets_data.append(tweet)
    except:
        continue
        
print len(tweets_data)

tweets_df = pd.DataFrame()
tweets_df['Username'] = map(lambda tweet: tweet['user']['name'], tweets_data)
tweets_df['statuses_count'] = map(lambda tweet: tweet['user']['statuses_count'], tweets_data)
tweets_df['friends_count'] = map(lambda tweet: tweet['user']['friends_count'], tweets_data)
tweets_df['followers_count'] = map(lambda tweet: tweet['user']['followers_count'], tweets_data)
tweets_df['text'] = map(lambda tweet: tweet['text'], tweets_data)
tweets_df['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets_df['location'] = map(lambda tweet: tweet['user']['location'], tweets_data)
tweets_df['created_at'] = map(lambda tweet: tweet['created_at'], tweets_data)

tweets_df.dtypes
Given the above code, generate some markdown instructions for it.

targets: 
Configure your python file and write that data inside text file
for storing data<br>
$python twitter_streaming.py &gt; twitter_data.txt
JSON Format

JSON is a syntax for storing and exchanging data.
JSON is "self-describing" and easy to understand ( I am not kidding! ) > Let's Visualise it.
Key values kind of structure.

Data preprocessing and cleaning
1. Extracting main data from JSON
inputs: I am working on the file "0/More on Python Tools.ipynb".
The first task is:
Let's see whether this works for our sin example from above:
Can you write Python code for it?

targets: 
x = linspace(0,2*pi)
dsin = nderiv(sin(x),x)
plot(x,dsin,label='numerical')
plot(x,cos(x),label='analytical')
title("Comparison of numerical and analytical derivatives of sin(x)")
legend()
inputs: #predict facies of unclassified data
y_unknown = gbModel.predict(X_unknown)
well_data['Facies'] = y_unknown
well_data

well_data['Well Name'].unique()
Given the above code, generate some markdown instructions for it.

targets: 
Finally we predict facies labels for the unknown data, and store the results in a Facies column of the test_data dataframe.
inputs: "2015년 1월 거래가 평균"
Please write code following the instructions in jupyter notebook style.

targets: 
weed_ca_jan2015.mean()
inputs: AutoCSV

targets: # generate a csv from a dataset
def generate_csv(path, labelmap=None, folder='train'):
    """Infers a csv from directory structure. 
       `labelmap` is a dictionary mapping class folders to class names.
       Class names are taken from class folder names if no mapping is provided.
       For Single-Category Classification.
    """
    # find categories
    catfolders = [f for f in os.listdir(path/folder) if (path/folder/f).is_dir()]
    if labelmap is None: labelmap = {cf:cf for cf in catfolders}

    rows = []

    for cat in catfolders:
        catpath = path/folder/cat
        fpaths  = list(map(lambda x: cat+'/'+x, os.listdir(catpath)))
        rows.extend(list(zip(fpaths,[labelmap[cat] for i in range(len(fpaths))])))

    df = pd.DataFrame(rows, columns=['file','class'])
    return df

%%time
csv = generate_csv(PATH)

csv.sample(n=10)
inputs: "Chart visualization
Some Earth Engine functions produce tabular data that can be plotted by
data visualization packages such as matplotlib. The following example
demonstrates the display of tabular data from Earth Engine as a scatter
plot. See Charting in Colaboratory
for more information."
Please write code following the instructions in jupyter notebook style.

targets: 
# Import the matplotlib.pyplot module.
import matplotlib.pyplot as plt

# Fetch a Landsat image.
img = ee.Image('LANDSAT/LT05/C01/T1_SR/LT05_034033_20000913')

# Select Red and NIR bands, scale them, and sample 500 points.
samp_fc = img.select(['B3','B4']).divide(10000).sample(scale=30, numPixels=500)

# Arrange the sample as a list of lists.
samp_dict = samp_fc.reduceColumns(ee.Reducer.toList().repeat(2), ['B3', 'B4'])
samp_list = ee.List(samp_dict.get('list'))

# Save server-side ee.List as a client-side Python list.
samp_data = samp_list.getInfo()

# Display a scatter plot of Red-NIR sample pairs using matplotlib.
plt.scatter(samp_data[0], samp_data[1], alpha=0.2)
plt.xlabel('Red', fontsize=12)
plt.ylabel('NIR', fontsize=12)
plt.show()
inputs: I am working on the file "NotesDeCours/17-exemples.ipynb".
The first task is:
On vérifie que la fonction retourne bien que 4 et 8 ne sont pas des nombres premiers:
Can you write Python code for it?

targets: 
[n for n in range(20) if est_premier(n)]