Building an Application That Can Create Various Income Classes

In this example, we look at how to use the Python source in order to create an application that can create different classifications for various income levels for an entire population as a whole. This can work very well for the Federal Government, especially when it comes to tax purposes and/or giving out entitlements and benefits.

This example makes use of a dataset that consists of a population of 25,000 people:

# Input file containing

Data input_file = ‘income_data.txt’

# Read the data X=[]


Count_Classl = 0 Count_Class2 = 0 Max_datapoints = 25000 With open (input_file, V) as f:

For line in f. readiness ():

If count_Class >=max_datapoints and Count_Class2 >=max_ datapoints Break If *?’ in line:


Data = line [:-1 ] .split (Y)

If data [-1] ==”<=50K” and Count_Classl < max_datapoints; X.append(data)

Count_Classl ==1

If data [-1] ====”>50K” and Count_Class2 < max_datapoints; X.append(data)

Count_Class2 +-1 # Convert to numpy array X = np.array

#Convert string data to numerical data Label_encoder = []

X_encoded = np.empty (X.shape)

For I, item in enumerate (X[0]);

If item.isdigit ();

X_Encoded [;, i] = X [:, 1]


Label_encoder.append(preprocessing.LabelEncoder ()) X_Encoded [:, i] = label_enocder [-l].fit.tranform (X[:, 1])

X = X_encoded [:, :-l], astype (int)

Y = X_encoded [:, :-l], astype (int)

#Create SVM classifier

Classifier = OneVaOneClassifier (LinearSVC (random_state=0);

#Train the Classifier Classifier, fit (X, y)

#Cross Validation

X_train, X_test, y_train, y_test = train_test_split.train_test_

Split (X, y, test_size=0.2, random_state=5)

Classifier = OneVaOneClassifier (LinearSVC (random_state=0); (X_train, y_train)

Y_test_pred = classifier.predict (X_test)

#Compute the F1 score of the SVM Classifier

FI = train_test_split.cross_val_score (classifier, X, y, scoring=’fl_weighted’, cv=3)

Print (“Fl score: + str(round(100*fl .mean(), 2)} + “%”

#Predict output for a test datapoint

Input_data = [‘32’, ‘Public’ or ‘Private’, ‘34456’, 'College Graduate’, ‘Married’, ‘Physician’ ‘Has Family, ‘Caucasian’, ‘Female’, 23’, ‘United States’] #Encode test datapoint Input_Data_Encoded = [-1] * len(input_data)

Count = 0

For I, item in enumerate (input_data);

If item.isdigit ():

Input_data_encoded [i] = int (input_data [i])


Input_data_encoded[i] = int (label_encoder[count]. Transform(input_data[i]))

Input_data_encoded = np.array(input_data_encoded)

#Run classifier on encoded datapoint and print output

Predicted_class = classifier.predict (input_data_encoded)

Print (label_encoder [-l].inverse_transform (predicted_class) [0])

(Artasanchez & Joshi, 2020).

Building an Application That Can Predict Housing Prices

In good economic times, one of the markets that tends to really get a lot of attention and become “red hot” is that of real estate. This is especially true if you are trying to “flip” a house for a higher value, or just want to sell your existing home. This application can even be used to predict the market value for a house that you wish to purchase. The opposite of this is also true. The model developed here can also be used with other financial-based models in the case of an economic downturn, in which real estate prices can greatly fluctuate.

Here is the Python source code to create this kind of application:

#Load housing data

Data = datasets.load_boston()

#Shuffle the data

X, у = Shuffle(,, random_state=32)

#Split the data into training and testing datasets Num_training = int (0.8 * len (X))

X_trai, Y_train = X[:num_training], y[:num_training]

X_test, Y_test = X(num_training;), y[num_training:]

#Create Support Vector Regression model Sv_regressor = SVR(kernel = ‘linear’, C=1.0 epsilon=0.1)

#Train Support Vector Regressor (X_train, Y_train)

#Evaluate performance of Support Vector Regressor Y_test_pred = sv_regressor.predict (X_test)

MSE=mean_squared_error (y_test, y_test_pred)

EVS = explained variance score (y_test, y_test_pred)

Print (“ ### Performance ###”)

Print (“Mean squared error =”, round (mse, 2))

Print (“Explained variance score =”, round (evs, 2))

#Test the regressor on test datapoint

Test_data = (Iterations of housing pricing datasets)

Print (“ Predicted Proce:”, sv_regressor.predict ([test_data]) [0]) (Artasanchez & Joshi, 2020).

Building an Application That Can Predict Vehicle Traffic Patterns in Large Cities

Although many people are working from home because of the COVID-19 pandemic, traffic still exists. It may not be so much in the rural areas, but in the much larger metropolitan areas, there are still large amounts of traffic. Given this and the fact that just about anything can disrupt the smooth flow of vehicle traffic, whether it is due to weather, a large accident, or even a Cyberattack, government officials need to have a way in which they can predict what traffic will look like based upon certain outcomes, such as using the permutations just described. Also, the drivers of vehicles need to be constantly updated via their mobile app (especially that of Google Maps), if there is a new route to be taken, in the case of a large scale traffic jam.

Here is the Python source code to help create such an application:

#Load input data Input_file = ‘traffic data.txt’

Data = []

With open (input_file, V) as f:

For line in f.readiness ();

Items = line [:-l], split (Y)

Data.append (items)


#Convert string data to numerical data Label_Encoder = []

X_encoded = np.empty (data.shape)

For I, item in enumerate (data[0j):

If item.isdigit ():

X_encoded (:, i) = data [:, i]


Label_encoder.append (preprocessing.LabelEncoder(;)

X_encoded [;, i] = label_encoder [-l].fit_transform(data[I, 1])

X = X_encoded [:, :-1 ] .astype(int)

Y = X_encoded [:, -l].astype(int)

#Split data into training and testing datasets X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.25, random_state=5)

#Extremely Random Forests Regressor

Params = {^estimators’: 100, ‘max_depth’: 4, ‘random_state’:0)

Regressor = ExtraTreesRegressor (**params) (X_train, y_train)

#Compute the regressor performance on test data Y_pred = regressor.predict (X_test)

Print (“Mean absolute error:”, round (mean_absolute_error (y_test, y_ pred), 2))

#Testing encoding on single data instance Test_datapoint = [‘Friday’, ‘6 PM CST’, ‘Chicago’, ‘no’] Test_datapoint_encoded = [-1] * len(test_datapoint)

#Predict the output for the test datapoint

Print (‘Predicted Traffic:”, int (regressor.predict ([test_datapoint_ encoded]) [0]))

(Artasanchez & Joshi, 2020).

< Prev   CONTENTS   Source   Next >