Pytorch delete features columns from dataset - deep-learning

I have a dataset below and would like to delete features From A - F
the dataset are converted from python dataframe
dataset = datasets.DatasetDict({"train":Dataset.from_pandas(X_train),
"test":Dataset.from_pandas(X_test),
"val":Dataset.from_pandas(X_val),
})
The dataset output like below
DatasetDict({
train: Dataset({
features: ['A', 'B', 'C', 'D', 'E', 'F', 'text', '__index_level_0__', 'label'],
num_rows: 1173
})
test: Dataset({
features: ['A', 'B', 'C', 'D', 'E', 'F', 'text', '__index_level_0__', 'label'],
num_rows: 1369
})
val: Dataset({
features: ['A', 'B', 'C', 'D', 'E', 'F', 'text', '__index_level_0__', 'label'],
num_rows: 1369
})
})
Result like below
DatasetDict({
train: Dataset({
features: ['text', '__index_level_0__', 'label'],
num_rows: 1173
})
test: Dataset({
features: ['text', '__index_level_0__', 'label'],
num_rows: 1369
})
val: Dataset({
features: ['text', '__index_level_0__', 'label'],
num_rows: 1369
})
})

What you need is the remove_columns() method from datasets. This works on any Dataset() object, if you want to remove some columns at this level and not in Pandas before.
dataset = dataset.remove_columns("label")
For your case, it would be:
dataset = dataset.remove_columns(['A', 'B', 'C', 'D', 'E', 'F'])
You can have a look here: https://huggingface.co/docs/datasets/process

Related

How to pass a Json data from one folder to another folder in react

I am working on a React project, where I am trying to pass jsondata from one folder to another folder but it's not working. It is showing an error like this
./src/Pages/Dashboard/Dashboard.js Module not found: Can't resolve
'./API/jsondata'
This is is my code
This is jsondata.js
{
user: [
{
'id': '1',
'name': 'test1',
'age': '11',
'gender': 'male',
'email': 'test1#gmail.com'
},
{
'id': '2',
'name': 'test2',
'age': '12',
'gender': 'male',
'email': 'test2#gmail.com'
}, {
'id': '3',
'name': 'test3',
'age': '13',
'gender': 'male',
'email': 'test3#gmail.com'
}, {
'id': '4',
'name': 'test4',
'age': '14',
'gender': 'male',
'email': 'test4#gmail.com'
}, {
'id': '5',
'name': 'test5',
'age': '15',
'gender': 'male',
'email': 'test5#gmail.com'
},
{
'id': '6',
'name': 'test6',
'age': '16',
'gender': 'male',
'email': 'test6#gmail.com'
},
]
}
This is Dashboard.js
import React from 'react';
import Jsondata from './API/jsondata'
import './Dashboard.css';
const Dashboard = () => {
console.log(Jsondata, 'data')
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
</div>
</div>
</div>
)
}
export default Dashboard
Any ideas to what may be causing the problem?
There can be many reasons:
First one you didn't exported your data from json file.
export const Jsondata = [
{
'id': '1',
'name': 'test1',
'age': '11',
'gender': 'male',
'email': 'test1#gmail.com'
},
{
'id': '2',
'name': 'test2',
'age': '12',
'gender': 'male',
'email': 'test2#gmail.com'
},
];
Second maybe your file path is wrong.Just double check it.Here you have given the name Jsondata but there is no such const in json file.
import {Jsondata} from './API/jsondata'
You can define a variable inside jsondata.js file.
Like;
const users = [
...
];
export { users };
And from Dashboard.js, you can import as
import { users } from './API/jsondata';
Rename the jsondata file to data.json (the file should have .json extension) and then it works. Your import can omit .json extension like this
import mydata from "./data";
Note: You don't have to export anything in data.json file

How to model class in order to serialize Dart Object (Map<String, List>) into JSON in Flutter?

I have a Map<Datetime, List> in Dart that stores events needed to fill up a calendar in my Flutter app:
final Map<DateTime, List> _events = {
DateTime(2020, 7, 7): [
{'name': 'Event A', 'isDone': true},
],
DateTime(2020, 7, 11): [
{'name': 'Event A', 'isDone': true}
],
DateTime(2020, 7, 9): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
],
DateTime(2020, 7, 10): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
],
DateTime(2020, 7, 13): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
{'name': 'Event C', 'isDone': false},
],
DateTime(2020, 7, 25): [
{'name': 'Event A', 'isDone': true},
{'name': 'Event B', 'isDone': true},
{'name': 'Event C', 'isDone': false},
],
DateTime(2020, 7, 6): [
{'name': 'Event A', 'isDone': false},
],
};
I want to convert this into JSON but I'm not entirely sure how to model the class. Anyone got any ideas?
If you simply want to convert this to a JSON you can use the jsonEncode function and use the toEncodable named parameter to allow your object to be encoded. I made an example here that just cast the keys of your Map to a String so that it can be encoded.
jsonEncode(
_events,
toEncodable: (input) {
return _events.map((key, value) {
return MapEntry(key.toString(), value);
});
}
);
When you want to decode this back to your Map<DateTime, List> you can just do the reverse.
var objectTemp = jsonDecode(
json,
);
var output = objectTemp.map((key, value) {
return MapEntry(DateTime.parse(key), value);
})
Create a model class for Event and store the event data in it (name, isDone and date). Then you put them in a List<Event>. You can search on this list for a spesific date and get the according events if any. Then you can make that data class serializable.

Invalid Json while using get requests Flask

I need to request a PG Database thru an API, I am using Flask requests package :
payload = {'key':'**', 'schema':'**', 'table':'testh','where_clause':'0', 'liste_fields':'*'}
r = requests.get('https://myapi/', params=payload, verify=False)
I need to get all the content of my testh table store in a JSON, but the function r.json() gets me this invalid JSON :
{'id': {'0': '1', '1': '2', '2': '3', '3': '4'}, 'brand': {'0': 'apple', '1': 'microsoft', '2': 'google', '3': 'amazon'}}
I need a JSON as : {0: {id:'2', brand:'apple}, 1:{id:'2', brand:'microsoft}, ....}
You can use pandas for this:
import pandas as pd
d = {'id': {'0': '1', '1': '2', '2': '3', '3': '4'}, 'brand': {'0': 'apple', '1': 'microsoft', '2': 'google', '3': 'amazon'}}
d = pd.DataFrame(d).to_dict(orient='index')
Output:
{'0': {'id': '1', 'brand': 'apple'},
'1': {'id': '2', 'brand': 'microsoft'},
'2': {'id': '3', 'brand': 'google'},
'3': {'id': '4', 'brand': 'amazon'}}
It is just a guess, but maybe the API used pandas as well and if you call to_dict() without parameter you get exactly the output you are describing.

Convert list of nested dictionary into dictionary in Python

I have a list of nested dictionary as below. There is list inside the dictionary as well. I don't want to change the inner list but to turn the entire list into dictionary. The list is as below:
[{"id" : "A", "data" : [{"key_1" : 012},{"key_2" : 123}, {"key_3" : 234}]}]
I only want to change the list itself to dictionary without changing the inner list as below:
{{"id" : "A", "data" : [{"key_1" : 012},{"key_2" : 123}, {"key_3" : 234}]}}
Appreciate your help in this.
Use a dictionary comprehension and zip the list of dictionaries up with whatever list of keys you want. Here is an example using a subset of ascii lowercase letters:
{ pair[0]: pair[1] for pair in list(zip([*string.ascii_lowercase[0:len(input)]], input))}
>>> foo = [ { 'Age': 32,
'Emp_id': 'A',
'Gender': 'M',
'Result': [ {'Incentive': 3000, 'Month': 'Aug'},
{'Incentive': 3500, 'Month': 'Sep'},
{'Incentive': 2000, 'Month': 'Oct'}]},
{ 'Age': 35,
'Emp_id': 'B',
'Gender': 'M',
'Result': [{'Incentive': 1500, 'Month': 'Aug'}]},
{ 'Age': 31,
'Emp_id': 'C',
'Gender': 'F',
'Result': [ {'Incentive': 5000, 'Month': 'Aug'},
{'Incentive': 2400, 'Month': 'Sep'}]}]
>>> { pair[0]: pair[1] for pair in list(zip([*string.ascii_lowercase[0:len(input)]], input))}
>>> { 'a': { 'Age': 32,
'Emp_id': 'A',
'Gender': 'M',
'Result': [ {'Incentive': 3000, 'Month': 'Aug'},
{'Incentive': 3500, 'Month': 'Sep'},
{'Incentive': 2000, 'Month': 'Oct'}]},
'b': { 'Age': 35,
'Emp_id': 'B',
'Gender': 'M',
'Result': [{'Incentive': 1500, 'Month': 'Aug'}]},
'c': { 'Age': 31,
'Emp_id': 'C',
'Gender': 'F',
'Result': [ {'Incentive': 5000, 'Month': 'Aug'},
{'Incentive': 2400, 'Month': 'Sep'}]}}
note: you can pretty print code as follows:
import pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)

Highcharts crashing Chrome

Highcharts seems to crash Chrome when I change the 'visible:false' attribute on a spline chart. Chrome seems to be ok if I set all the visible flags to the same thing.
This problem doesn't occur in Firefox
Is this a bug in highcharts?
Demo is here:
http://jsfiddle.net/TJ4mc/7/
$(function () {
$('#weeklyRevenue').highcharts({
chart: {
type: 'spline'
},
xAxis: {
type: 'datetime'
},
series: [{name: 'George Street', data: [[Date.UTC(2013, 5, 3), 5.4]], visible:true}, {name: 'Marrickville', data: [[Date.UTC(2013, 4, 20), 4.5],[Date.UTC(2013, 5, 10), 23.7],[Date.UTC(2013, 5, 17), 33.6]], visible:false}, {name: 'Liverpool Street', data: [[Date.UTC(2013, 4, 27), 7.3],[Date.UTC(2013, 5, 3), 10.7],[Date.UTC(2013, 5, 10), 6.8]], visible:false}, {name: 'Moving Average', dashStyle: 'ShortDash', marker:{enabled:false}, data: [[Date.UTC(2013, 5, 3), 5.4]], visible:true}]
});
});
You should replace this:
[Date.UTC(2013, 5, 3), 5.4]], visible:true}
with
{x:Date.UTC(2013, 5, 3), y: 5.4, visible:true}