<jupyter_start><jupyter_text>PL1 - G4  FIRST EXERCISEFist step is read the file "satelites.csv" to obtain the radios for each planet.<jupyter_code># Load needed packages
import csv

# Read CSV File, here we obtain a list of str
def ReadCSV(FileName):
    with open(FileName, newline = '') as File:
        reader = csv.reader(File)
        data = []
        for row in reader:
            data.append(row)
        return(data)

# Before having the radios, we need to filter the list in order to
# delete unesefull data, such as '\t' or headers

def GetData(data):
    listFilter = []
    for i in range(len(data)):
        fil = "".join(map(str, list(filter(lambda x: x!= '\t', list(data[i]))))).split()
        listFilter.append(fil)

    result= []
    # We start in 1, just to delete headers
    for i in range(1,len(listFilter)):
        #Get the radius data with int tyipe
        result.append(int(listFilter[i].pop(1)))
    return(result)

    
# IMPORTANT: If the file is not inside the same place as this code, we necessary have 
# to put the completed route to the file
s = (GetData(ReadCSV('satelites.csv')))
print(s)<jupyter_output>[13, 16, 22, 33, 39, 42, 27, 34, 20, 30, 20, 15]<jupyter_text>Now, analysis as mode, median, range, variance... will be apply in the data.The main libraries used in this analysis are: statistics, panda and numpy; wich provides easily ways to made this operations.<jupyter_code># Package for statistics' function
import statistics as stats

def mean(data):
    return (stats.mean(data) )

print("AVERAGE MEAN:     ",mean(s))
def Range(data):
    return (max(data)-min(data))

print("RANGE:     ",Range(s))
def Mode(data):
    return (stats.mode(data))

print("MODE:      ", Mode(s))
def Median(data):
    return(stats.median(data))
print("MEDIAN:     ",Median(s))
def Variance(data):
    return(stats.variance(data)) #it can be calculate with variance's function
print("VARIANCE", Variance(s))
def StandarDev(data):
    return(stats.pstdev(data)) #o con stdev

print("STANDAR DESVIATION: ", StandarDev(s) )
import pandas as pd
def absFrec(data):
    datos = pd.Series(data)
    return (datos.value_counts())

print("ABSOLUTE FRECUENCY:\n")

absFrec(s).to_frame().T
def absFrec_acum(data):
    datos = pd.Series(absFrec(data))
    return datos.cumsum()
 

print("ABSOLUTE ACUMULATED FRECUENCY: ")
absFrec_acum(s).to_frame().T
def relFrec(data):
    datos = pd.Series(data)
    for i in range(len(data)):
        return (absFrec(data)/len(data))
        
print("RELATIVE ACUMULATED FRECUENCY: ")

relFrec(s).to_frame().T
def relFrec_acum(data):
    datos = pd.Series(relFrec(data))
    return datos.cumsum()
 

print("RELATIVE ACUMULATED FRECUENCY: ")
relFrec_acum(s).to_frame().T

import numpy as np
def quantiles(data, num):
    datos = pd.DataFrame(np.array(data))
    return datos.quantile(num)

print("54ª QUANTILE: ",quantiles(s, .54))
def rootMeanSqr(data):
    for i in range(len(data)):
        data[i] = data[i]**2
    return(stats.mean(data)**(1/2))
rootMeanSqr(s)
def rootMeanSqrAn (data, media):
    res = 0
    for i in range(len(data)):
        anomaly =  data[i]- media + res
        res = anomaly
    return ((res**2)**(1/2))/len(data)
rootMeanSqrAn(s, rootMeanSqr(s) )
    
    
# Command necessary for Jupyter to install the library
!pip install pyreadstat

import pyreadstat

def getDataSav():
    df, meta = pyreadstat.read_sav("cardata.sav")
    return df

print(getDataSav())<jupyter_output>mpg  cylinders  displace  horsepower  accel  year  weight  origin  \
0    36.1        4.0      98.0        66.0   14.4  78.0  1800.0     1.0   
1    19.9        8.0     260.0       110.0   15.5  78.0  3365.0     1.0   
2    19.4        8.0     318.0       140.0   13.2  78.0  3735.0     1.0   
3    20.2        8.0     302.0       139.0   12.8  78.0  3570.0     1.0   
4    19.2        6.0     231.0       105.0   19.2  78.0  3535.0     1.0   
5    20.5        6.0     200.0        95.0   18.2  78.0  3155.0     1.0   
6    20.2        6.0     200.0        85.0   15.8  78.0  2965.0     1.0   
7    25.1        4.0     140.0        88.0   15.4  78.0  2720.0     1.0   
8    20.5        6.0     225.0       100.0   17.2  78.0  3430.0     1.0   
9    19.4        6.0     232.0        90.0   17.2  78.0  3210.0     1.0   
10   20.6        6.0     231.0       105.0   15.8  78.0  3380.0     1.0   
11   20.8        6.0     200.0        85.0   16.7  78.0  3070.0     1.0   
12   18.6        6.0     [...]<jupyter_text>Now, we are going to do the same analysis than before but with the file "cardata.sav" were mpg is going to be our data.<jupyter_code>import numpy as np
import pandas 
df = getDataSav()
data = np.array(df.get("mpg").dropna())
print(data)
import statistics as stats
print("""
    Mean: %d
    Median: %d
    Mode: %d
    Range: %d
""" % (stats.mean(data),stats.median(data),stats.mode(data), max(data)-min(data)))


print("""
    Variance: %d
    Standar Desviation: %d
""" % (stats.pvariance(data),stats.pstdev(data)))
import pandas as pd
data = data = np.array(df.get("mpg_agrupada").dropna())

def absFrec(data):
    datos = pd.Series(data)
    return (datos.value_counts())

def absFrec_acum(data):
    datos = pd.Series(absFrec(data))
    return datos.cumsum()
 
print("Absolute frecuency group by mpg: \n", (pd.Series(data)).value_counts().to_frame().T)
print("\nAbsolute acumulative frecuency group by mpg: \n", ((pd.Series(data)).value_counts()).cumsum().to_frame().T)
def relFrec(data):
    datos = pd.Series(data)
    for i in range(len(data)):
        return (absFrec(data)/len(data))
        

def relFrec_acum(data):
    datos = pd.Series(relFrec(data))
    return datos.cumsum()
 

print("Absolute frecuency group by mpg: \n", relFrec(data))
print("\nAbsolute acumulative frecuency group by mpg: \n", relFrec_acum(data))<jupyter_output>Absolute frecuency group by mpg: 
 6.0     0.168831
7.0     0.168831
3.0     0.155844
5.0     0.142857
4.0     0.110390
8.0     0.097403
2.0     0.090909
10.0    0.032468
9.0     0.025974
1.0     0.006494
dtype: float64

Absolute acumulative frecuency group by mpg: 
 6.0     0.168831
7.0     0.337662
3.0     0.493506
5.0     0.636364
4.0     0.746753
8.0     0.844156
2.0     0.935065
10.0    0.967532
9.0     0.993506
1.0     1.000000
dtype: float64
{'fraction_non_alphanumeric': 0.13209955328653478, 'fraction_numerical': 0.08806636885768986, 'mean_word_length': 2.5298423423423424, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '384637', 'license': 'MIT', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2408, 'n_words': 634, 'path': 'PL1/docs/jupyter/G4-PL1.ipynb', 'repo_name': 'davecraciunescu/DataScience', 'size': '6268'}