0% found this document useful (0 votes)
6 views5 pages

Machine Learningn

The document is a Jupyter Notebook containing Python code for data processing and analysis using the Iris dataset. It includes functions for loading data, converting data types, splitting data into training and testing sets, and calculating statistical measures such as mean, covariance, and variance. The notebook demonstrates the extraction of specific columns for training and testing purposes, showcasing the lengths of the resulting datasets.

Uploaded by

minibitesadmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Machine Learningn

The document is a Jupyter Notebook containing Python code for data processing and analysis using the Iris dataset. It includes functions for loading data, converting data types, splitting data into training and testing sets, and calculating statistical measures such as mean, covariance, and variance. The notebook demonstrates the extraction of specific columns for training and testing purposes, showcasing the lengths of the resulting datasets.

Uploaded by

minibitesadmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

3/9/24, 10:10 a.m.

Untitled17 - Jupyter Notebook

localhost:8888/notebooks/Untitled17.ipynb?kernel_name=python3 1/5
3/9/24, 10:10 a.m. Untitled17 - Jupyter Notebook

localhost:8888/notebooks/Untitled17.ipynb?kernel_name=python3 2/5
3/9/24, 10:10 a.m. Untitled17 - Jupyter Notebook

In [30]: 1 from csv import reader


2 import numpy as np
3 ​
4 def cargarDatos(ruta):
5 with open(ruta,"r") as file:
6 listaDatos = []
7 datos=reader(file)
8 for fila in datos:
9 listaDatos.append(fila)
10 return listaDatos
11 ​
12 def pasarFloat(data):
13 for j in range(len(data)):
14 for i in range(len(data[0])):
15 data[j][i]=float(data[j][i])
16
17 def trainTesting(data, split=0.2):
18 listaTraining = data.copy()
19 tam = int(len(data)*split)
20 listaTesting = []
21 while len(listaTesting) < tam:
22 indice=np.random.randint(len(listaTraining))
23 val=listaTraining.pop(indice)
24 listaTesting.append(val)
25 return listaTesting, listaTraining
26
27
28 def media(columna):
29 m = sum(columna)/len(columna)
30 return m
31 def covarianza(x,mena_x,y,mean_y):
32 cov=0
33 for i in range(len(x)):
34 cov = cov + (x[i]-mean_x)*(y[i]-mean_y)
35 return cov/len(x)
36 ​
37 def varianza(columna,media):
38 va=sum([(x-media)**2 for x in columna])
39 return va/len(columna)
40
41 def calculoCoeficientes(X,Y,X_media,Y_media):
42 cov=covarianza(X,X_media,Y,Y_media)
43 var=varianza(X,X_media)
44 b = cov/var
45 a = Y_media - b*X_media
46 return a,b
47
48
49 ruta = "C:/ciclo_2024_2_/principios_Machine_Learning_/iris.csv"
50 data=cargarDatos(ruta)
51 data= data[1:]
52 for i in range(len(data)):
53 data[i]=data[i][0:-1]
54 pasarFloat(data)
55 #print(data)
56 """"print("------------------Tomando la primera y tercera columna---
57 X=[fila[0] for fila in data]
58 print("-----------------X---------------")
59 print(X)
60 Y=[fila[2] for fila in data]
61 print("-----------------Y---------------")
localhost:8888/notebooks/Untitled17.ipynb?kernel_name=python3 3/5
3/9/24, 10:10 a.m. Untitled17 - Jupyter Notebook
62 print(Y)"""
63 listaTesting, listaTraining=trainTesting(data, split=0.2)
64 print(len(listaTesting))
65 print(len(listaTraining))
66 ​
67 print("-----------------------------X_train, Y_train----------------
68 X_train=[fila[0] for fila in listaTraining]
69 print("-----------------X---------------")
70 print(X_train)
71 Y_train=[fila[2] for fila in listaTraining]
72 print("-----------------Y---------------")
73 print(Y_train)
74 ​
75 print("-----------------------------X_test, Y_test------------------
76 X_test=[fila[0] for fila in listaTesting]
77 print("-----------------X---------------")
78 print(X_test)
79 Y_test=[fila[2] for fila in listaTesting]
80 print("-----------------Y---------------")
81 print(Y_test)

30
120
-----------------------------X_train, Y_train------------------------
-----------------X---------------
[5.1, 4.7, 4.6, 5.0, 5.4, 4.6, 4.4, 4.9, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4,
5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 4.8, 5.0, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2,
5.5, 4.9, 5.0, 5.5, 4.9, 5.1, 5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 5.2, 5.2, 5.4]
-----------------------------X_test, Y_test------------------------
-----------------X---------------
[6.5, 5.0, 6.1, 5.4, 5.5, 5.1, 5.9, 5.8, 4.4, 6.0, 6.3, 6.3, 6.6, 5.6,
5.0, 6.4, 5.6, 4.6, 6.5, 4.9, 6.3, 5.7, 6.7, 5.5, 7.7, 5.4, 7.7, 5.0,
6.9, 6.7]
-----------------Y---------------
[5.8, 1.6, 4.7, 1.5, 4.0, 1.7, 5.1, 5.1, 1.3, 5.0, 5.6, 4.7, 4.4, 4.5,
3.3, 5.5, 3.6, 1.4, 5.1, 1.4, 5.0, 4.2, 4.4, 3.8, 6.1, 4.5, 6.9, 1.5,
5.7, 5.7]

localhost:8888/notebooks/Untitled17.ipynb?kernel_name=python3 4/5
3/9/24, 10:10 a.m. Untitled17 - Jupyter Notebook

localhost:8888/notebooks/Untitled17.ipynb?kernel_name=python3 5/5

You might also like