<jupyter_start><jupyter_text>Intro to Numpy  To create arrays<jupyter_code># Import libraries
import numpy as np
from matplotlib import pyplot as plt
# To create a numpy array
arr=np.array([3,1,2,4])
arr
# To create arrays filled with zeros
zeros=np.zeros(2, dtype=np.int)
zeros
# To create arrays filled with ones and specify floating point
ones=np.ones(4, dtype=np.float64)
ones
# To create a numpy array in a range of 2-10 in 2 by 2
np.arange(2,10,2)
# To create a numpy array with linspace to define interval an number of values used
np.linspace(0,10, num=5)
# To see the shape of an array
arr.shape
# To create a new axis
arr_2=arr[np.newaxis, :]
arr_2.shape
# To create a new axis doing reshape
arr_3=arr.reshape(1,4)
arr_3.shape<jupyter_output><empty_output><jupyter_text>Selecting elements in arrays<jupyter_code># Select the last element
arr[-1]
# Select the second and third element
arr[1:3]
# Select all the elements but the last
arr[:3]<jupyter_output><empty_output><jupyter_text>Operations with arrays<jupyter_code># To sort values in numpy
arr=np.sort(arr)
arr
# To concatenate arrays
arr_4=np.array([5,6,7,8])
np.concatenate((arr,arr_4))
# To reverse an array
np.flip(arr_4)
# To sum a constant to every value in the aray
arr+2
# To sum arrays
arr+arr_4<jupyter_output><empty_output><jupyter_text>To create matrices and save them<jupyter_code># To create a matrix in numpy
mat = np.array([[1,2,3], [4,5,6], [7,8,9]])
mat
# To create a random matrix
np.random.rand(3,3)
# To save numpy in disk
np.save('produced_files/filename', mat)
# To load in numpy
b=np.load('produced_files/filename.npy')
b
# To save numpy as .csv
np.savetxt('produced_files/new_file.csv', mat)
# To add specifications while exporting numpy to csv
np.savetxt('produced_files/new_file.csv', mat, fmt='%.2f')
# To load .csv as numpy
np.loadtxt('produced_files/new_file.csv')<jupyter_output><empty_output><jupyter_text>Selecting elements in matrix<jupyter_code># Selecting a specific column
mat[:, 0]
# Selecting values with logic operations
mat[mat > 5]<jupyter_output><empty_output><jupyter_text>Operations with matrices<jupyter_code># To find how many elements are in the matrix
mat.size
# To pile or stack matrices vertically
stack_1 = np.array([[1, 1],
               [2, 2]])

stack_2 = np.array([[3, 3],
               [4, 4]])
np.vstack((stack_1,stack_2))
# To pile or stack matrices horizontally
np.hstack((stack_1,stack_2))
# To find max, min and sum
print(mat.max(), mat.min(), mat.sum())
# To get the sum in each column
mat.sum(axis=0)
# To find unique values in numpy
np.unique(mat)
# To get the indices of unique values
unique_values, indices_list = np.unique(mat, return_index=True)
print(indices_list)
# To get the frequency count of unique values
unique_values, ocurrence_list = np.unique(mat, return_counts=True)
print(ocurrence_list)
# To flatten: Convert matrix to 1D array 
mat.flatten()<jupyter_output><empty_output><jupyter_text>Linear algebra with numpy<jupyter_code># Sum of a constant to a matrix
mat+5
# Broadcasting: Multiplication matrix and constant
np.multiply(mat, 5)
# Another way to multiply matrix and constant
mat*0.25
# To make every item zero for every dimension
mat*=0
mat
# Matrix multiplication
mat_2 = np.array([[1,3],
                  [5,7]])
mat_3 = np.array([[2,4],
                  [6,8]])
np.multiply(mat_2, mat_3)
# Another way of matrix multiplication
mat_2*mat_3
# Sum between matrices, with compatible shape
mat_2+mat_3
# Matrix dot product
np.dot(mat_2,mat_3)
# Matrix transpose
z=mat_2.T
z
# If we modify the transpose, the normal is modified too
z[1,1]=200
z
mat_2<jupyter_output><empty_output><jupyter_text>Numpy for plotting with matplotlib<jupyter_code># Graphs using functions with numpy
import numpy as np
t = np.arange(0., 5., 0.2)
# Plot 3 graphs at the same time
plt.plot(t, t, t, t**2, t, t**3)
plt.show()<jupyter_output><empty_output><jupyter_text>Numpy with statistics<jupyter_code>person_height = np.array([1.51, 1.63, 1.77, 1.59, 1.99,
                         1.85, 1.66, 1.83, 1.79, 1.89,
                         2.00, 1.92, 1.75, 1.69, 2.10,
                         1.64, 1.73, 1.73, 2.08, 1.70,
                         1.73, 1.60, 2.09, 1.88, 1.81,
                         1.68, 2.11, 1.73, 1.99, 1.83])
# Mean with numpy
np.mean(person_height, dtype=float)
# To find how many fulfill the condition divided by total
np.mean(person_height >= 1.68, dtype=float)
# Mean by axis
np.mean(mat, axis=1)
# Median with numpy
np.median(person_height)
# Percentiles with numpy
q=np.zeros(4)
q[0]=np.percentile(person_height, 25)
q[1]=np.percentile(person_height, 50)
q[2]=np.percentile(person_height, 75)
q[3]=np.percentile(person_height, 100)
x=range(len(q))
plt.bar(x,q)
# Standard deviation in numpy
np.std(person_height)
# Use of the mean score and standard deviation to generate a random 
# distribution model
scores = np.random.normal(1000, 50, size=100000)
plt.hist(scores, bins=1000, range=(800,1300))
plt.title('Student scores')
plt.xlabel('Scores')
plt.ylabel('Students')
plt.show()
# Generate a binomial distribution using random numbers
# 10,000 "experiments"
# N = 10 shots
# P = 0.25 (25% probability)
a = np.random.binomial(10, 0.25, size=10000)
plt.hist(a, range=(0, 10), bins=10, density=True) #density=True to normalize
plt.xlabel('Number of "successes"')
plt.ylabel('Frequency')
plt.show()
# Calculate the probability of getting 4 successes with 10 shots, given 25% chance of
# success
a = np.random.binomial(10, 0.25, size=10000)
np.mean(a == 4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12337662337662338, 'fraction_numerical': 0.050865800865800864, 'mean_word_length': 4.6066734074823055, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '575252', 'license': 'MIT', 'n_tokens_mistral': 2177, 'n_tokens_neox': 1990, 'n_words': 597, 'path': '5. Intro to Numpy.ipynb', 'repo_name': 'ricardopretelt/Intro-to-python-libraries', 'size': '5544'}