# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/bharathulaprasad/cs360ml/blob/main/Assignment1.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"base_uri": "https://localhost:8080/"} id="8UNitMGegPiF" outputId="36ba007b-08db-405f-a2d7-6d82bbe16500"
#Part 1 - Python Basics 

#1
#Write a command to get the Python version you are using.
# !python --version

# + colab={"base_uri": "https://localhost:8080/"} id="FFz5JAHSgvFV" outputId="bfc48244-7040-456f-c65b-6a37cdde5cea"
#2
# Write a command or program to locate Python site-packages
import site; 
print(site.getsitepackages())

# + colab={"base_uri": "https://localhost:8080/"} id="rn7vs4TSg9Ce" outputId="f72e0cdf-e992-4a21-b911-ca6ff8d45a5f"
#3
# Write a command or program to get the path and name of the file that is currently executing.
from pathlib import Path
import os

print('File dir:', os.path.dirname(''))
print('File name:', Path(os.path.abspath('')).resolve().stem)


# + colab={"base_uri": "https://localhost:8080/"} id="-maOOi7cjQgS" outputId="99b83061-8786-49be-c310-3b2d11f004c4"
#4
# Write a python program to randomly generate two matrices of
# size [2x3 and 3x2] and do the matrix multiplication using
# the two generated matrices. (Say A = BC)
import numpy as np
B= np.random.rand(2,3)
C=np.random.rand(3,2)
A=np.matmul(B,C)
A

# + colab={"base_uri": "https://localhost:8080/"} id="M5AHm72HkhWI" outputId="9b38b3ab-a3e4-4c04-b520-a953ec5fdfa6"
#5
#  Use a for loop to implement the above question five times
# and thereafter take the average of all the five matrices
# and display the result.
import numpy as np
#create for loop for 5 times
i = 0
while i < 5:
  B = np.random.rand(2,2)
  A = np.add(A,B)
  i += 1
np.divide(A,5)
print (A)


# + colab={"base_uri": "https://localhost:8080/"} id="84SV2-o5V8Vw" outputId="fc69ff44-aa41-43c3-af51-b8f07b82d47b"
#6
# Write a program to transfer the matrix (generated in
#question 5) to the CSV file and then read the same matrix
#and display the transposition of it.
from numpy import genfromtxt
np.savetxt("foo.csv", A, delimiter=",")
print(genfromtxt('foo.csv', delimiter=','))
print(A.transpose())

# + colab={"base_uri": "https://localhost:8080/"} id="0zvuECFdhh7_" outputId="f32db097-c26e-4ca3-8838-951f6a258438"
#7 
# Write a program to find the mean, mode, and median of the
#above-mentioned matrix.
from scipy import stats
A = np.array([[30,80,10],[80,80,10],[80,50,50]]) 
print(np.mean(A))
print(np.median(A))
print(stats.mode(A))

# + colab={"base_uri": "https://localhost:8080/"} id="xl8A9i1Qmfh-" outputId="fb0fa14b-6853-4fb1-8cd8-0e9976bc7bf2"
# 8 
# upper traingle of the above array
print(A[np.triu_indices(3)])

# + colab={"base_uri": "https://localhost:8080/"} id="a6zOa3zVnR5P" outputId="90bfadaa-5996-4b48-aaa9-8ec170ec0147"
# 9
# Write a Python program to read specific columns of a given
#CSV file and print the content of the columns
# (Column number should be given by the user)
import pandas as pd
column=input("input column number to read in csv like 0, 1, 2, or 3 : ")
name = ["aparna", "pankaj", "krishna", "sudhir"] 
degree = ["MBA", "BCA", "M.Tech", "MBA"] 
score = [90, 40, 80, 98] 
dict = {'name': name, 'degree': degree, 'score': score}       
df = pd.DataFrame(dict) 
df.to_csv('scores.csv') 
# saving the dataframe  
df = pd.read_csv('scores.csv',usecols=[int(column)], low_memory = True)
print(df)

# + colab={"base_uri": "https://localhost:8080/"} id="2fxY8_T0yLV_" outputId="92244873-d800-4f11-e419-3996a66a8b66"
# 10
# Write a Python program to replace a user-defined number
# present in the CSV file by the character ‘A’
import pandas as pd 

# Making data frame from the csv file 
df = pd.read_csv("scores.csv") 
# Printing the first 4 rows of the data frame for visualization 
print("before changing\n", ) 
print(df[:4])
column=input("value to replace: ");
df.replace(to_replace =int(column), 
                 value ='A', inplace=True) 

print("changing now")
df.to_csv("scores_changed.csv")
df = pd.read_csv("scores_changed.csv") 
print(df[:4])

# + colab={"base_uri": "https://localhost:8080/"} id="z1nOmROX8i4q" outputId="c04fa709-9424-4789-c49c-55cb01365538"
# Part 2 Numpy Basics
#1
# Write a NumPy program to create an array of 10 zeros,10 ones,
# 10 fives
import numpy as np
array=np.zeros(10)*0
print("An array of 10 zeros:")
print(array)
array=np.ones(10)*1
print("An array of 10 ones:")
print(array)
array=np.ones(10)*5
print("An array of 10 fives:")
print(array)


# + colab={"base_uri": "https://localhost:8080/"} id="82bhxgOOZwAf" outputId="7dc7bb98-96e4-4246-fd9b-352ccf733862"
#2
#Write a NumPy program to create an array of all the even
#integers from 10 to 50
import numpy as np
array=np.arange(10,50,2)
print(array) 

# + colab={"base_uri": "https://localhost:8080/"} id="VceoilBAaIGu" outputId="3e2258e9-f25d-4edf-df26-60993f4c3414"
#3
#Write a NumPy program to generate a random number between 0 and 1
import random as rand
rand.uniform(0,1)


# + colab={"base_uri": "https://localhost:8080/"} id="nZhw8GP9a1_V" outputId="8a010f81-3b82-43e8-9d3d-71afcdc359af"
#4
# Write a NumPy program to save the matrix
import numpy as np
A=np.random.rand(2,2)
print ("actual random array is :\n" + str(A))
np.savetxt("array_save.txt",A, fmt='%f')
print("reading from file :\n"+ str(np.loadtxt("array_save.txt", dtype=float)))

# + colab={"base_uri": "https://localhost:8080/"} id="UCygpJYIeHga" outputId="65558efc-5f50-454a-8474-7f0675f90bd0"
# Part 3
# 1
# Write a python program to read an image and save the image as a
# matrix to a .csv file using pandas
from PIL import Image
from numpy import matrix
import numpy as np

image = Image.open("sample_data/Home.jpg")
image.load()
imagearray = np.asarray(image)
# print first two elemnts as test
print(imagearray[:2])
np.save("sample_data/imagedata.csv",imagearray)



# + id="BTH6xx4Enqrv"
# 2
#Write a program to import excel data from the .csv file
#(generated in question 1) by excluding the last row and last column
import pandas as pd
read_file=pd.read_csv("sample_data/california_housing_test.csv")
# remove the last column
read_file.drop(read_file.columns[-1], axis=1, inplace=True)
# remove last row
read_file = read_file[:-1]
read_file.to_excel ('sample_data/sample.xlsx')


# + colab={"base_uri": "https://localhost:8080/"} id="I_UY4XD1s1cv" outputId="814a49ed-6d8d-4196-9f3f-d85bcda1b56f"
import pandas as pd
from datetime import date
now = pd.to_datetime(str(date.today()), format='%Y-%m-%d')
print("Today's date:")
print(now)

# + colab={"base_uri": "https://localhost:8080/"} id="y1Aw9RmstHHy" outputId="45ba3f56-7d59-4c93-e729-56313f2d47a8"
# Part 4
# Create a mini calculator to know the age of a person after
# putting the range (i.e. date of birth and current date)
birth = input("Input your birth year :" )
birth = int(birth)
today = date.today() 
age = today.year - birth
print(age)

StarcoderdataJupyterScriptsDedupFiltered