Question

Using the data in the attached table info.csv Find the k-nearest neighbors for record #10 using...

Using the data in the attached table info.csv

Find the k-nearest neighbors for record #10 using k = 3

Write a Python 3 program that solves the above problem

Python program must read the info.csv file from within the program

The info.csv file contains the following data table

1, 22, Single, 46156.98, Bad loss

2, 33, Married, 24188.10, Bad loss

3, 28, Other, 28787.34, Bad loss

4, 51, Other, 23886.72, Bad loss

5, 25, Single, 47281.44, Bad loss

6, 39, Single, 33994.90, Good risk

7, 54, Single, 28716.50, Good risk

8, 55, Married, 49186.75, Good risk

9, 50, Married, 46726.50, Good risk

10, 66, Married, 36120.34, Good risk

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

# importing the required libraries

import numpy as np
import pandas as pd
from sklearn.neighbors import NearestNeighbors
from sklearn import preprocessing
# Storing the data provided and converting it to dataframe

data= [[1, 22, "Single", 46156.98, "Bad loss"],[2, 33, "Married", 24188.10, "Bad loss"],[3, 28, "Other", 28787.34, "Bad loss"],
[4, 51, "Other", 23886.72, "Bad loss"],[5, 25, "Single", 47281.44, "Bad loss"],[6, 39, "Single", 33994.90, "Good risk"],
[7, 54, "Single", 28716.50, "Good risk"],[8, 55, "Married", 49186.75, "Good risk"],[9, 50, "Married", 46726.50, "Good risk"],
[10, 66, "Married", 36120.34, "Good risk"]]

df = pd.DataFrame(data, columns = ['rec','Age', 'Status', 'val', 'risk'])

# droping(deleting) the column representing the record number since it is managed by the df.

df.drop(columns=['rec'])
#converting string values to integer so that it can be operated by the model

le = preprocessing.LabelEncoder()

le.fit(df.Status)
df.Status = le.transform(df.Status)

le.fit(df.risk)
df.risk = le.transform(df.risk)

df
# creating the model and fitting into the model

neigh = NearestNeighbors(n_neighbors=3)
neigh.fit(df)
# 1st array represents the distance and second shows the record number (starting from 0) from which the distance is displayed

print(neigh.kneighbors(df.loc[[9]]))

Add a comment
Know the answer?
Add Answer to:
Using the data in the attached table info.csv Find the k-nearest neighbors for record #10 using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are given SmallSample.csv data. Please complete the following using this data and write a rscript....

    You are given SmallSample.csv data. Please complete the following using this data and write a rscript. Please read SmallSample.csv data and create a data frame variable call smallsample. Please show the first six records to check data frame format Please show the structure of the data. Please check every column’s class: age& income should be numeric; gender, marital, and risk should be factor. numkids (number of kids) should be integer. If they are not the correct type, please change them...

  • 4.) Using k-Nearest Neighbors with k=3 and the following table, classify corn (sweetness = 6, crunchiness...

    4.) Using k-Nearest Neighbors with k=3 and the following table, classify corn (sweetness = 6, crunchiness = 6) as either a fruit, protein, or vegetable. 3pts Crunchiness Ingredient apple bacon Sweetness 10 1 banana 10 Food type fruit protein fruit vegetable vegetable protein 1 10 10 carrot celery cheese 1

  • Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm...

    Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm to solve a classification problem. The kNN is a simple and robust classifier, which is used in different applications. The goal is to train kNN algorithm to distinguish the species from one another. The dataset can be downloaded from UCI Machine Learning Repository: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/ (Links to an external site.)Links to an external site.. Download `iris.data` file from the Data Folder. The Data Set description...

  • blockpy python review 16 processing json files second pic is the report.json file data BlockPy: Review 16) Process JSON The given file contains JSON data. Use the data associated with the "Employ...

    blockpy python review 16 processing json files second pic is the report.json file data BlockPy: Review 16) Process JSON The given file contains JSON data. Use the data associated with the "Employees" key (a list inside of a dictionary inside of a dictionary inside of a dictionary) to plot the distribution of employees. Note: You cannot embed the contents of the file directly in your program. Use the appropriate file handling style to access the data in the file. Note:...

  • Using the data in the file named “Dataset 1,” to determine is there was a change...

    Using the data in the file named “Dataset 1,” to determine is there was a change in the number of tons of paper used after the implementation of a recycling program in 25 different districts. a. Is this study using dependent or independent samples? b. What is your research hypothesis? c. Using Excel, calculate your test statistic and p-value. Paste the output here. d. What do you conclude? Below is the data set that is mentioned above Before Recycling 20...

  • For Questions 1-2, use table below. It is data for two stocks over 30 days (it...

    For Questions 1-2, use table below. It is data for two stocks over 30 days (it is sample data). Day High stock Sky stock Day High stock Sky stock 1 27 60 16 43 39 2 33 57 17 45 37 3 34 56 18 46 35 4 35 55 19 47 34 5 36 54 20 47 32 6 37 51 21 48 29 7 38 50 22 50 26 8 38 47 23 50 26 9 39 47...

  • The following data represents the number of consecutive minutes (to the nearest minute) 2. spent on...

    The following data represents the number of consecutive minutes (to the nearest minute) 2. spent on mobile phones by students attending the University of A. 23 2 26 40 22 52 28 40 19 55 50 52 51 46 51 60 59 45 26 2 48 1 34 10 18 45 29 41 11 43 50 32 37 15 57 17 57 35 50 45 7 22 15 13 20 13 15 32 13 39 25 38 31 16 7...

  • Student Name Student Number 18. Describe the center and spread of the data using either the...

    Student Name Student Number 18. Describe the center and spread of the data using either the mean and standard deviation or the five-number summary. Justify your choice by constructing a box-and-whisker plot for the data. Be sure to include the scale. A. 47, 16, 70, 80, 28, 33, 91, 55, 60, 45, 86, 54, 30, 98, 34, 87, 44, 35,64,58, 27,67, 72,68, 31, 95, 37, 41, 97,56, 49, 71, 84, 66, 45, 93 B. 40, 39, 37, 26, 25, 40,...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • Suppose a binary tree data (in tiny written size) is stored in an array (A) as...

    Suppose a binary tree data (in tiny written size) is stored in an array (A) as given below and root is placed at “0”index. Note the array indices are in larger written size (0 to 74). Show the traversal data of the given tree for a)      In-Order Traversal b)     Post Order Traversal A 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3 28 13 36 15 9 22 44 7 10 75 33 19 15...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT